方案一
```
func md5V(str string) string {
h := md5.New()
h.Write([]byte(str))
return hex.EncodeToString(h.Sum(nil))
}
```
方案二
```
func md5V2(str string) string {
data := []byte(str)
has := md5.Sum(data)
md5str := fmt.Sprintf("%x", has)
return md5str
}
```
方案三
```
func md5V3(str string) string {
w := md5.New()
io.WriteString(w, str)
md5str := fmt.Sprintf("%x", w.Sum(nil))
return md5str
}
```
整体测试代码
```
package main
import (
"crypto/md5"
"encoding/hex"
"fmt"
"io"
)
func main() {
str := "MD5testing"
md5Str := md5V(str)
fmt.Println(md5Str)
fmt.Println(md5V2(str))
fmt.Println(md5V3(str))
}
// 输出结果:
f7bb96d1dcd6cfe0e5ce1f03e35f84bf
f7bb96d1dcd6cfe0e5ce1f03e35f84bf
f7bb96d1dcd6cfe0e5ce1f03e35f84bf
```
文章来源:https://www.jianshu.com/p/58dcbf490ef3
有疑问加站长微信联系(非本文作者))