How to hash a string using MD5 in Go?

In Go, you can hash a string using the crypto/md5 package. Here's an example of how you can hash a string using MD5:

package main import ( "crypto/md5" "fmt" "io" ) func main() { str := "Hello, World!" // The string to be hashed hash := md5.New() // Create a new MD5 hash instance _, _ = io.WriteString(hash, str) // Write the string to the hash hashedStr := fmt.Sprintf("%x", hash.Sum(nil)) // Get the hashed string fmt.Println(hashedStr) // Output: 65a8e27d8879283831b664bd8b7f0ad4 }

In this example, we first import the crypto/md5, fmt, and io packages. We then define the string to be hashed (str) as "Hello, World!"

Next, we create a new MD5 hash instance by calling md5.New().

We then write the string to the hash using io.WriteString(). This converts the string to a byte slice and writes it to the hash.

Finally, we get the hashed string by calling hash.Sum(nil), which returns a byte slice of the hash. We then use fmt.Sprintf() to convert the byte slice to a hexadecimal string representation.

The output of the above example will be 65a8e27d8879283831b664bd8b7f0ad4, which is the MD5 hash of the string "Hello, World!".