gfgconvstructgconvstruct
使用方式:
import "gitee.com/johng/gf/g/util/gconv"
// 基本类型
func Bool(i interface{}) bool
func Float32(i interface{}) float32
func Float64(i interface{}) float64
func Int(i interface{}) int
func Int16(i interface{}) int16
func Int32(i interface{}) int32
func Int64(i interface{}) int64
func Int8(i interface{}) int8
func String(i interface{}) string
func Uint(i interface{}) uint
func Uint16(i interface{}) uint16
func Uint32(i interface{}) uint32
func Uint64(i interface{}) uint64
func Uint8(i interface{}) uint8
// slice类型
func Bytes(i interface{}) []byte
func Ints(i interface{}) []int
func Floats(i interface{}) []float64
func Strings(i interface{}) []string
func Interfaces(i interface{}) []interface{}
// 时间类型
func Time(i interface{}, format ...string) time.Time
func TimeDuration(i interface{}) time.Duration
// 对象转换
func Struct(params interface{}, objPointer interface{}, attrMapping ...map[string]string) error
// 根据类型名称执行基本类型转换(非struct转换))
func Convert(i interface{}, t string, extraParams ...interface{}) interface{}
基本使用
常用基本类型的转换方法比较简单,我们这里使用一个例子来演示转换方法的使用及效果。
package main
import (
"fmt"
"gitee.com/johng/gf/g/util/gconv"
)
func main() {
i := 123
fmt.Printf("%10s %v\n", "Int:", gconv.Int(i))
fmt.Printf("%10s %v\n", "Int8:", gconv.Int8(i))
fmt.Printf("%10s %v\n", "Int16:", gconv.Int16(i))
fmt.Printf("%10s %v\n", "Int32:", gconv.Int32(i))
fmt.Printf("%10s %v\n", "Int64:", gconv.Int64(i))
fmt.Printf("%10s %v\n", "Uint:", gconv.Uint(i))
fmt.Printf("%10s %v\n", "Uint8:", gconv.Uint8(i))
fmt.Printf("%10s %v\n", "Uint16:", gconv.Uint16(i))
fmt.Printf("%10s %v\n", "Uint32:", gconv.Uint32(i))
fmt.Printf("%10s %v\n", "Uint64:", gconv.Uint64(i))
fmt.Printf("%10s %v\n", "Float32:", gconv.Float32(i))
fmt.Printf("%10s %v\n", "Float64:", gconv.Float64(i))
fmt.Printf("%10s %v\n", "Bool:", gconv.Bool(i))
fmt.Printf("%10s %v\n", "String:", gconv.String(i))
fmt.Printf("%10s %v\n", "Bytes:", gconv.Bytes(i))
fmt.Printf("%10s %v\n", "Strings:", gconv.Strings(i))
fmt.Printf("%10s %v\n", "Ints:", gconv.Ints(i))
fmt.Printf("%10s %v\n", "Floats:", gconv.Floats(i))
fmt.Printf("%10s %v\n", "Interfaces:", gconv.Interfaces(i))
}
执行后,输出结果为:
Int: 123
Int8: 123
Int16: 123
Int32: 123
Int64: 123
Uint: 123
Uint8: 123
Uint16: 123
Uint32: 123
Uint64: 123
Float32: 123
Float64: 123
Bool: true
String: 123
Bytes: [123]
Strings: [123]
Ints: [123]
Floats: [123]
Interfaces: [123]
Struct转换
jsonxmlgconv
gconvstruct
func Struct(params interface{}, objPointer interface{}, attrMapping ...map[string]string) error
其中:
paramsstructmapobjPointerstructstructattrMappingmap键名strcut属性params
转换规则
gconvstructstructattrMapping
struct公开属性paramsparamsmap不区分大小写-/_/空格structparamsstructstructslicemapstrcut
以下是几个匹配的示例:
map键名 struct属性 是否匹配
name Name match
Email Email match
nickname NickName match
NICKNAME NickName match
Nick-Name NickName match
nick_name NickName match
nick name NickName match
NickName Nick_Name match
Nick-name Nick_Name match
nick_name Nick_Name match
nick name Nick_Name match
示例1,基本使用
package main
import (
"gitee.com/johng/gf/g"
"gitee.com/johng/gf/g/util/gconv"
)
type User struct {
Uid int
Name string
Site_Url string
NickName string
Pass1 string `gconv:"password1"`
Pass2 string `gconv:"password2"`
}
func main() {
user := (*User)(nil)
// 使用默认映射规则绑定属性值到对象
user = new(User)
params1 := g.Map{
"uid" : 1,
"Name" : "john",
"siteurl" : "https://gfer.me",
"nick_name" : "johng",
"PASS1" : "123",
"PASS2" : "456",
}
if err := gconv.Struct(params1, user); err == nil {
g.Dump(user)
}
// 使用struct tag映射绑定属性值到对象
user = new(User)
params2 := g.Map {
"uid" : 2,
"name" : "smith",
"site-url" : "https://gfer.me",
"nick name" : "johng",
"password1" : "111",
"password2" : "222",
}
if err := gconv.Struct(params2, user); err == nil {
g.Dump(user)
}
}
Structstruct tagStruct
执行后,输出结果为:
{
"Uid": 1,
"Name": "john",
"Site_Url": "https://gfer.me",
"NickName": "johng",
"Pass1": "123",
"Pass2": "456"
}
{
"Uid": 2,
"Name": "smith",
"Site_Url": "https://gfer.me",
"NickName": "johng",
"Pass1": "111",
"Pass2": "222"
}
示例2,复杂类型转换
1. slice基本类型属性
package main
import (
"gitee.com/johng/gf/g/util/gconv"
"gitee.com/johng/gf/g"
"fmt"
)
// 演示slice类型属性的赋值
func main() {
type User struct {
Scores []int
}
user := new(User)
scores := []interface{}{99, 100, 60, 140}
// 通过map映射转换
if err := gconv.Struct(g.Map{"Scores" : scores}, user); err != nil {
fmt.Println(err)
} else {
g.Dump(user)
}
// 通过变量映射转换,直接slice赋值
if err := gconv.Struct(scores, user); err != nil {
fmt.Println(err)
} else {
g.Dump(user)
}
}
执行后,输出结果为:
{
"Scores": [
99,
100,
60,
140
]
}
{
"Scores": [
99,
100,
60,
140
]
}
2. struct属性为struct
package main
import (
"gitee.com/johng/gf/g/util/gconv"
"gitee.com/johng/gf/g"
"fmt"
)
func main() {
type Score struct {
Name string
Result int
}
type User struct {
Scores Score
}
user := new(User)
scores := map[string]interface{}{
"Scores" : map[string]interface{}{
"Name" : "john",
"Result" : 100,
},
}
// 嵌套struct转换
if err := gconv.Struct(scores, user); err != nil {
fmt.Println(err)
} else {
g.Dump(user)
}
}
执行后,输出结果为:
{
"Scores": {
"Name": "john",
"Result": 100
}
}
3. struct属性为slice,数值为slice
package main
import (
"gitee.com/johng/gf/g/util/gconv"
"gitee.com/johng/gf/g"
"fmt"
)
func main() {
type Score struct {
Name string
Result int
}
type User struct {
Scores []Score
}
user := new(User)
scores := map[string]interface{}{
"Scores" : []interface{}{
map[string]interface{}{
"Name" : "john",
"Result" : 100,
},
map[string]interface{}{
"Name" : "smith",
"Result" : 60,
},
},
}
// 嵌套struct转换,属性为slice类型,数值为slice map类型
if err := gconv.Struct(scores, user); err != nil {
fmt.Println(err)
} else {
g.Dump(user)
}
}
执行后,输出结果为:
{
"Scores": [
{
"Name": "john",
"Result": 100
},
{
"Name": "smith",
"Result": 60
}
]
}
4. struct属性为slice,数值为非slice
package main
import (
"gitee.com/johng/gf/g/util/gconv"
"gitee.com/johng/gf/g"
"fmt"
)
func main() {
type Score struct {
Name string
Result int
}
type User struct {
Scores []Score
}
user := new(User)
scores := map[string]interface{}{
"Scores" : map[string]interface{}{
"Name" : "john",
"Result" : 100,
},
}
// 嵌套struct转换,属性为slice类型,数值为map类型
if err := gconv.Struct(scores, user); err != nil {
fmt.Println(err)
} else {
g.Dump(user)
}
}
执行后,输出结果为:
{
"Scores": [
{
"Name": "john",
"Result": 100
}
]
}
基准性能测试
123456789int
john@john-B85M:~/Workspace/Go/GOPATH/src/gitee.com/johng/gf/g/util/gconv$ go test *.go -bench=".*" -benchmem
goos: linux
goarch: amd64
BenchmarkString-4 20000000 71.8 ns/op 24 B/op 2 allocs/op
BenchmarkInt-4 100000000 22.2 ns/op 8 B/op 1 allocs/op
BenchmarkInt8-4 100000000 24.5 ns/op 8 B/op 1 allocs/op
BenchmarkInt16-4 50000000 23.8 ns/op 8 B/op 1 allocs/op
BenchmarkInt32-4 100000000 24.1 ns/op 8 B/op 1 allocs/op
BenchmarkInt64-4 100000000 21.7 ns/op 8 B/op 1 allocs/op
BenchmarkUint-4 100000000 22.2 ns/op 8 B/op 1 allocs/op
BenchmarkUint8-4 50000000 25.6 ns/op 8 B/op 1 allocs/op
BenchmarkUint16-4 50000000 32.1 ns/op 8 B/op 1 allocs/op
BenchmarkUint32-4 50000000 27.7 ns/op 8 B/op 1 allocs/op
BenchmarkUint64-4 50000000 28.1 ns/op 8 B/op 1 allocs/op
BenchmarkFloat32-4 10000000 155 ns/op 24 B/op 2 allocs/op
BenchmarkFloat64-4 10000000 177 ns/op 24 B/op 2 allocs/op
BenchmarkTime-4 5000000 240 ns/op 72 B/op 4 allocs/op
BenchmarkTimeDuration-4 50000000 26.2 ns/op 8 B/op 1 allocs/op
BenchmarkBytes-4 10000000 149 ns/op 128 B/op 3 allocs/op
BenchmarkStrings-4 10000000 223 ns/op 40 B/op 3 allocs/op
BenchmarkInts-4 20000000 55.0 ns/op 16 B/op 2 allocs/op
BenchmarkFloats-4 10000000 186 ns/op 32 B/op 3 allocs/op
BenchmarkInterfaces-4 20000000 66.6 ns/op 24 B/op 2 allocs/op
PASS
ok command-line-arguments 35.356s