n:=grand.N(0,100) 返回0-100随机数支持负数
n:=grand.S(4) 返回4个随机字符串
n:=grand.Digits(4) 返回随机四位数字
n:=grand.Letters(4) 返回随机4位字符串
n:=guid.S() 生成32位长唯一字符串
n:=guuid.New().String() 返回db535f10-6f78-4aee-a651-84747694785d
n:=guuid.New().ID() 长数字
n:=guuid.New().Time() 特长数字
n:=gconv.String(12.34) 类型转换支持一些基本类型
m:=map[string]string{“ok”:”wo”,”ii”:”中国”}
s:=gconv.Map(m) map转换map[string]interface{}
type User struct {
Uid int c:"uid"
Name string c:"name"
}
// 对象
g.Dump(gconv.Map(User{
Uid: 1,
Name: “john”,
}))
// 对象指针,结构体转map[string]interface{}
g.Dump(gconv.Map(&User{
Uid: 1,
Name: “john”,
}))
m2 := gconv.MapDeep(user) 支持递归转换
map转换成结构体
params := g.Map{
“uid”: 1,
“name”: []string{“john”,”li”},
}
type User struct {
Uid int
Name []string
}
user := new(User) //等价 var user *User 调用 &user
gconv.Scan(params, user)
slice map转换结构体
type User struct {
Uid int
Name string
}
params := g.Slice{
g.Map{
“uid”: 1,
“name”: “john”,
},
g.Map{
“uid”: 2,
“name”: “smith”,
},
}
users:=new([]User)
gconv.Scan(params, users)
map转换成标准map
var user map[string]string
params := g.Map{
“uid”: 1,
“name”: “john”,
}
gconv.Scan(params, &user)
var user []map[string]string
params := g.Slice{
g.Map{
“uid”: 1,
“name”: “john”,
},
g.Map{
“uid”: 2,
“name”: “smith”,
},
}
gconv.Scan(params, &user)
更新时间:2024-12-05 13:26