Golang 常用包
名称 | 地址 | 说明 |
---|---|---|
数据库 | github.com/go-sql-driver/mysql | 数据库连接 |
Log | github.com/jeanphorn/log4go | Log日志 |
json | simplejson “github.com/bitly/go-simplejson” | json |
json | jsoniter “github.com/json-iterator/go” | json |
time | https://goframe.org/pages/viewpage.action?pageId=3670633 | time |
字符类型转换 | https://goframe.org/pages/viewpage.action?pageId=1114317 | 字符类型转换 |
SQL 生成 Go结构体 | https://www.devtool.com/sql2go.html | |
JSON 生成 Go结构体 | https://oktools.net/json2go |
GoFrame (php中的laravel)
https://pkg.go.dev/github.com/gogf/gf#pkg-constants
https://goframe.org/pages/viewpage.action?pageId=1114119
加密函数(crc32、md5、sha1函数)
package main
import (
"crypto/md5"
"crypto/sha1"
"encoding/hex"
"fmt"
"hash/crc32"
)
// 生成md5
func MD5(str string) string {
c := md5.New()
c.Write([]byte(str))
return hex.EncodeToString(c.Sum(nil))
}
//生成sha1
func SHA1(str string) string{
c:=sha1.New()
c.Write([]byte(str))
return hex.EncodeToString(c.Sum(nil))
}
func CRC32(str string) uint32{
return crc32.ChecksumIEEE([]byte(str))
}
func main() {
fmt.Println(CRC32("123456"))
fmt.Println(MD5("123456"))
fmt.Println(SHA1("123456"))
}
编码转换
//string到int
value_int,err:=strconv.Atoi(string)
//int到string
str:=strconv.Itoa(value_int)
//string到int64
value_int64, err := strconv.ParseInt(string, 10, 64)
//int64到string,需注意下面转换规定
//FormatInt returns the string representation of i in the given base, for 2 <= base <= 36.
//The result uses the lower-case letters 'a' to 'z' for digit values >= 10
str:=strconv.FormatInt(value_int64,10)
//interface{}到float64-------接口后加上 .(float64)
//interface{}到string-------接口后加上 .(string)
时间
//获取当前时间戳的函数 , 默认有秒和纳秒 , 毫秒需要处理一下 , 转成字符串需要转换一下
fmt.Printf("时间戳(秒):%v;\n", time.Now().Unix())
fmt.Printf("时间戳(纳秒):%v;\n",time.Now().UnixNano())
fmt.Printf("时间戳(毫秒):%v;\n",time.Now().UnixNano() / 1e6)
fmt.Printf("时间戳(纳秒转换为秒):%v;\n",time.Now().UnixNano() / 1e9)
//将毫秒时间戳转换成字符串string
timestamp := strconv.FormatInt(time.Now().UnixNano()/1e6, 10)