Get md5 of a string in Golang

We can use the crypto/md5 and encoding/hex library to convert a string to its md5 hash. Below is an example code.

package main

import (
	"crypto/md5"
	"encoding/hex"
	"fmt"
)

func main() {
	var City string
	City = "New York"
	HashObj := md5.New()

	HashObj.Write([]byte(City))
	Hash := hex.EncodeToString(HashObj.Sum(nil))
	fmt.Println(Hash)

}

We can run this file on the command line using “go run <filename.go>”

This will output “87809c954948d8a20507bee3648281b3”.

Leave a Reply