持续更新
类型转换
int转string
s := strconv.Itoa(i)
// 等价于
s := strconv.FormatInt(int64(i), 10)
int64转string
i := int64(123)
s := strconv.FormatInt(i, 10)
// 第二个参数为基数,可选2~36
// 注:对于无符号整形,可以使用FormatUint(i uint64, base int)
string转int
i, err := strconv.Atoi(s)
string转int64
i, err := strconv.ParseInt(s, 10, 64)
// 第二个参数为基数(2~36),第三个参数位大小表示期望转换的结果类型,其值可以为0, 8, 16, 32和64,分别对应 int, int8, int16, int32和int64
float转string
v := 3.1415926535
s1 := strconv.FormatFloat(v, 'E', -1, 32) //float32
s2 := strconv.FormatFloat(v, 'E', -1, 64) //float64
string转float
s := "3.1415926535"
v1, err := strconv.ParseFloat(v, 32)
v2, err := strconv.ParseFloat(v, 64)
map/struct/json转换
Struct转Map
func Struct2Map(src interface{}) map[string]interface{} {
if reflect.TypeOf(src).Kind() == reflect.Struct {
str := structs.New(src)
str.TagName = "json"
return str.Map()
}
switch s := src.(type) {
case map[string]interface{}:
return s
}
b, err := json.Marshal(src)
if err != nil {
log.Error("Struct2Map:%v Error:%v", src, err.Error())
return make(map[string]interface{})
}
return Byte2Map(&b)
}
Struct转json
func Struct2String(src interface{}) string {
b, err := json.Marshal(src)
if err != nil {
log.Error("Struct2String:%v Error:%v", src, err.Error())
return ""
}
return string(b)
}
Map转json
func Map2Json(){
m := map[string]interface{}{"name": "zhangsan", "age": 10}
jsonStr, err := json.Marshal(m)
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(string(jsonStr))
}
Map转struct
type People struct {
Name string `json:"name"`
Age string `json:"age"`
}
func MapToStructDemo(){
m := map[string]interface{}{"name": "zhangsan", "age": 10}
var people People
err := mapstructure.Decode(m, &people)
if err != nil {
fmt.Println(err)
}
fmt.Println(people)
}
json转map
func JsonToMapDemo(){
jsonStr := `{"name": "zhangsan", "age": 10}`
var m map[string]interface{}
err := json.Unmarshal([]byte(jsonStr), &m)
if err != nil {
fmt.Println("JsonToMapDemo err: ", err)
}
fmt.Println(m)
}
json转struct
type People struct {
Name string `json:"name"`
Age string `json:"age"`
}
func JsonToMapDemo(){
jsonStr := `{"name": "zhangsan", "age": 10}`
var people People
json.Unmarshal([]byte(jsonStr), &people)
fmt.Println(people)
}
时间
时间运算
package main
import (
"testing"
"time"
"fmt"
)
func main() {
now := time.Now()
fmt.Println("当前时间:", now)
// 两小时之后的时间
h,_ := time.ParseDuration("2h")
nowAfter2Hour := now.Add(h)
fmt.Println("两小时之后的时间:", nowAfter2Hour)
// 两小时之前的时间
negativeH,_ := time.ParseDuration("-2h")
nowBefore2Hour := now.Add(negativeH)
fmt.Println("两小时之前的时间:", nowBefore2Hour)
// 十分钟之后的时间
m,_ := time.ParseDuration("10m")
nowAfter10Minute := now.Add(m)
fmt.Println("十分钟之后的时间:", nowAfter10Minute)
// 十分钟之前的时间
negativeM,_ := time.ParseDuration("-10m")
nowBefore10Minute := now.Add(negativeM)
fmt.Println("十分钟之前的时间:", nowBefore10Minute)
// 30s之后的时间
s,_ := time.ParseDuration("30s")
nowAfter30Second := now.Add(s)
fmt.Println("30s之后的时间:", nowAfter30Second)
// 30s之前的时间
negativeS,_ := time.ParseDuration("-30s")
nowBefore30Second := now.Add(negativeS)
fmt.Println("30s之前的时间:", nowBefore30Second)
// 1年1个月1天 之后的时间
fmt.Println("1年2个月3天之后的时间:", now.AddDate(1, 2, 3))
// 2年2个月2天之前的时间
fmt.Println("2年3个月4天之前的时间:", now.AddDate(-2, -3, -4))
// Sub 计算两个时间差
subM := nowAfter10Minute.Sub(now)
fmt.Println(subM.Minutes(), "分钟")
sumH := nowAfter10Minute.Sub(now)
fmt.Println(sumH.Hours(), "小时")
sumD := nowAfter10Minute.Sub(now)
fmt.Printf("%v 天\n", sumD.Hours()/24)
}