框架提供了非常强大的类型转换包gconv,可以实现将任何数据类型转换为指定的数据类型,对常用基本数据类型之间的无缝转换,同时也支持任意类型到struct对象的属性赋值。

使用方式:

  1. import "gitee.com/johng/gf/g/util/gconv"

方法列表: godoc.org/github.com/johng-cn/gf/g/util/gconv

  1. // 基本类型
  2. func Bool(i interface{}) bool
  3. func Float32(i interface{}) float32
  4. func Float64(i interface{}) float64
  5. func Int(i interface{}) int
  6. func Int16(i interface{}) int16
  7. func Int32(i interface{}) int32
  8. func Int64(i interface{}) int64
  9. func Int8(i interface{}) int8
  10. func String(i interface{}) string
  11. func Uint(i interface{}) uint
  12. func Uint16(i interface{}) uint16
  13. func Uint32(i interface{}) uint32
  14. func Uint64(i interface{}) uint64
  15. func Uint8(i interface{}) uint8
  16. // slice类型
  17. func Bytes(i interface{}) []byte
  18. func Ints(i interface{}) []int
  19. func Floats(i interface{}) []float64
  20. func Strings(i interface{}) []string
  21. func Interfaces(i interface{}) []interface{}
  22. // 时间类型
  23. func Time(i interface{}, format ...string) time.Time
  24. func TimeDuration(i interface{}) time.Duration
  25. // 对象转换
  26. func Struct(params interface{}, objPointer interface{}, attrMapping ...map[string]string) error
  27. // 类型名称转换
  28. func Convert(i interface{}, t string, extraParams ...interface{}) interface{}

基本使用

使用示例:

  1. package main
  2. import (
  3. "fmt"
  4. "gitee.com/johng/gf/g/util/gconv"
  5. )
  6. func main() {
  7. i := 123
  8. fmt.Printf("%10s %v\n", "Int:", gconv.Int(i))
  9. fmt.Printf("%10s %v\n", "Int8:", gconv.Int8(i))
  10. fmt.Printf("%10s %v\n", "Int16:", gconv.Int16(i))
  11. fmt.Printf("%10s %v\n", "Int32:", gconv.Int32(i))
  12. fmt.Printf("%10s %v\n", "Int64:", gconv.Int64(i))
  13. fmt.Printf("%10s %v\n", "Uint:", gconv.Uint(i))
  14. fmt.Printf("%10s %v\n", "Uint8:", gconv.Uint8(i))
  15. fmt.Printf("%10s %v\n", "Uint16:", gconv.Uint16(i))
  16. fmt.Printf("%10s %v\n", "Uint32:", gconv.Uint32(i))
  17. fmt.Printf("%10s %v\n", "Uint64:", gconv.Uint64(i))
  18. fmt.Printf("%10s %v\n", "Float32:", gconv.Float32(i))
  19. fmt.Printf("%10s %v\n", "Float64:", gconv.Float64(i))
  20. fmt.Printf("%10s %v\n", "Bool:", gconv.Bool(i))
  21. fmt.Printf("%10s %v\n", "String:", gconv.String(i))
  22. fmt.Printf("%10s %v\n", "Bytes:", gconv.Bytes(i))
  23. fmt.Printf("%10s %v\n", "Strings:", gconv.Strings(i))
  24. fmt.Printf("%10s %v\n", "Ints:", gconv.Ints(i))
  25. fmt.Printf("%10s %v\n", "Floats:", gconv.Floats(i))
  26. fmt.Printf("%10s %v\n", "Interfaces:", gconv.Interfaces(i))
  27. }

执行后,输出结果为:

  1. Int: 123
  2. Int8: 123
  3. Int16: 123
  4. Int32: 123
  5. Int64: 123
  6. Uint: 123
  7. Uint8: 123
  8. Uint16: 123
  9. Uint32: 123
  10. Uint64: 123
  11. Float32: 123
  12. Float64: 123
  13. Bool: true
  14. String: 123
  15. Bytes: [123]
  16. Strings: [123]
  17. Ints: [123]
  18. Floats: [123]
  19. Interfaces: [123]

Struct转换

示例1,基本使用

  1. package main
  2. import (
  3. "fmt"
  4. "gitee.com/johng/gf/g"
  5. "gitee.com/johng/gf/g/util/gconv"
  6. )
  7. type User struct {
  8. Uid int
  9. Name string
  10. Pass1 string `gconv:"password1"`
  11. Pass2 string `gconv:"password2"`
  12. }
  13. func main() {
  14. user := (*User)(nil)
  15. // 使用map直接映射绑定属性值到对象
  16. user = new(User)
  17. params1 := g.Map{
  18. "uid" : 1,
  19. "name" : "john",
  20. "pass1" : "123",
  21. "pass2" : "123",
  22. }
  23. if err := gconv.Struct(params1, user); err == nil {
  24. fmt.Println(user)
  25. }
  26. // 使用struct tag映射绑定属性值到对象
  27. user = new(User)
  28. params2 := g.Map {
  29. "uid" : 2,
  30. "name" : "smith",
  31. "password1" : "456",
  32. "password2" : "456",
  33. }
  34. if err := gconv.Struct(params2, user); err == nil {
  35. fmt.Println(user)
  36. }
  37. }

可以看到,我们可以直接通过Struct方法将map按照默认规则绑定到struct上,也可以使用struct tag的方式进行灵活的设置。此外,Struct方法有第三个map参数,用于指定自定义的参数名称到属性名称的映射关系。

执行后,输出结果为:

  1. &{1 john 123 123}
  2. &{2 smith 456 456}

示例2,slice基本类型属性

  1. package main
  2. import (
  3. "gitee.com/johng/gf/g/util/gconv"
  4. "gitee.com/johng/gf/g"
  5. "fmt"
  6. )
  7. // 演示slice类型属性的赋值
  8. func main() {
  9. type User struct {
  10. Scores []int
  11. }
  12. user := new(User)
  13. scores := []interface{}{99, 100, 60, 140}
  14. // 通过map映射转换
  15. if err := gconv.Struct(g.Map{"Scores" : scores}, user); err != nil {
  16. fmt.Println(err)
  17. } else {
  18. g.Dump(user)
  19. }
  20. // 通过变量映射转换,直接slice赋值
  21. if err := gconv.Struct(scores, user); err != nil {
  22. fmt.Println(err)
  23. } else {
  24. g.Dump(user)
  25. }
  26. }

执行后,输出结果为:

  1. {
  2. "Scores": [
  3. 99,
  4. 100,
  5. 60,
  6. 140
  7. ]
  8. }
  9. {
  10. "Scores": [
  11. 99,
  12. 100,
  13. 60,
  14. 140
  15. ]
  16. }

示例3,struct属性为struct

  1. package main
  2. import (
  3. "gitee.com/johng/gf/g/util/gconv"
  4. "gitee.com/johng/gf/g"
  5. "fmt"
  6. )
  7. func main() {
  8. type Score struct {
  9. Name string
  10. Result int
  11. }
  12. type User struct {
  13. Scores Score
  14. }
  15. user := new(User)
  16. scores := map[string]interface{}{
  17. "Scores" : map[string]interface{}{
  18. "Name" : "john",
  19. "Result" : 100,
  20. },
  21. }
  22. // 嵌套struct转换
  23. if err := gconv.Struct(scores, user); err != nil {
  24. fmt.Println(err)
  25. } else {
  26. g.Dump(user)
  27. }
  28. }

执行后,输出结果为:

  1. {
  2. "Scores": {
  3. "Name": "john",
  4. "Result": 100
  5. }
  6. }

示例4,struct属性为slice,数值为slice

  1. package main
  2. import (
  3. "gitee.com/johng/gf/g/util/gconv"
  4. "gitee.com/johng/gf/g"
  5. "fmt"
  6. )
  7. func main() {
  8. type Score struct {
  9. Name string
  10. Result int
  11. }
  12. type User struct {
  13. Scores []Score
  14. }
  15. user := new(User)
  16. scores := map[string]interface{}{
  17. "Scores" : []interface{}{
  18. map[string]interface{}{
  19. "Name" : "john",
  20. "Result" : 100,
  21. },
  22. map[string]interface{}{
  23. "Name" : "smith",
  24. "Result" : 60,
  25. },
  26. },
  27. }
  28. // 嵌套struct转换,属性为slice类型,数值为slice map类型
  29. if err := gconv.Struct(scores, user); err != nil {
  30. fmt.Println(err)
  31. } else {
  32. g.Dump(user)
  33. }
  34. }

执行后,输出结果为:

  1. {
  2. "Scores": [
  3. {
  4. "Name": "john",
  5. "Result": 100
  6. },
  7. {
  8. "Name": "smith",
  9. "Result": 60
  10. }
  11. ]
  12. }

示例5,struct属性为slice,数值为非slice

  1. package main
  2. import (
  3. "gitee.com/johng/gf/g/util/gconv"
  4. "gitee.com/johng/gf/g"
  5. "fmt"
  6. )
  7. func main() {
  8. type Score struct {
  9. Name string
  10. Result int
  11. }
  12. type User struct {
  13. Scores []Score
  14. }
  15. user := new(User)
  16. scores := map[string]interface{}{
  17. "Scores" : map[string]interface{}{
  18. "Name" : "john",
  19. "Result" : 100,
  20. },
  21. }
  22. // 嵌套struct转换,属性为slice类型,数值为map类型
  23. if err := gconv.Struct(scores, user); err != nil {
  24. fmt.Println(err)
  25. } else {
  26. g.Dump(user)
  27. }
  28. }

执行后,输出结果为:

  1. {
  2. "Scores": [
  3. {
  4. "Name": "john",
  5. "Result": 100
  6. }
  7. ]
  8. }