Golang-web开发好用的依赖库
读取配置库Viper
能够读取很多格式的配置文件,并帮我们格式化。
github文档地址
go get github.com/spf13/viper
日志记录Zap
go get -u go.uber.org/zap
zap 包提供快速、结构化、分级的日志记录。
文档地址
生成UUID的库
谷歌UUID文档
加密的库
crypto文档地址
golang自带加密库
"crypto/md5"
使用示例
package utils
import (
"crypto/md5"
"encoding/hex"
"golang.org/x/crypto/bcrypt"
)
// BcryptHash 使用 bcrypt 对数据进行加密
func BcryptHash(password string) string {
bytes, _ := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
return string(bytes)
}
// BcryptCheck 对比哈希值是否相同
func BcryptCheck(password, hash string) bool {
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
return err == nil
}
func MD5V(str []byte, b ...byte) string {
h := md5.New()
h.Write(str)
return hex.EncodeToString(h.Sum(b))
}