You can use the crypto/md5 [docs] package in Go to perform MD5 encoding on a string. We have an example function below, it will take your string and write it as a hash. Then convert that binary back into a hexadecimal string using Sprintf.
Note: There are more modern approaches than md5 these days - and it isn’t recommended for many things, but definitely not password hashing.
packagemainimport("crypto/md5""fmt")funcmain(){input:="welcome to gophercoding.com"output:=md5Encode(input)fmt.Println(input,"=",output)}// md5Encode will take a string and encode it as md5.
funcmd5Encode(inputstring)string{// Create a new hash & write input string
hash:=md5.New()_,_=hash.Write([]byte(input))// Get the resulting encoded byte slice
md5:=hash.Sum(nil)// Convert the encoded byte slice to a string
returnfmt.Sprintf("%x",md5)}
Edd is a PHP and Go developer who enjoys blogging about his experiences, mostly about creating and coding new things he's working on and is a big beliver in open-source and Linux.