Generic - Methods - 图1tip

The following list of common methods may become outdated compared to new features in the code. For more methods and examples, please refer to the code documentation: https://pkg.go.dev/github.com/gogf/gf/v2/container/gvar

New

  • Description: New creates and returns a new Var with the given value. The optional parameter safe specifies whether to use Var in a concurrent-safe manner, with a default value of false.
  • Format:
  1. func New(value interface{}, safe ...bool) *Var
  • Example:
  1. // New
  2. func ExampleVarNew() {
  3. v := gvar.New(400)
  4. g.Dump(v)
  5. // Output:
  6. // "400"
  7. }

Clone

  • Description: Clone performs a shallow copy of the current Var and returns a pointer to this Var.
  • Format:
  1. func (v *Var) Clone() *Var
  • Example
  1. // Clone
  2. func ExampleVar_Clone() {
  3. tmp := "fisrt hello"
  4. v := gvar.New(tmp)
  5. g.DumpWithType(v.Clone())
  6. fmt.Println(v == v.Clone())
  7. // Output:
  8. // *gvar.Var(11) "fisrt hello"
  9. // false
  10. }

Set

  • Description: Set sets the value of v to value and returns the old value of v.
  • Format:
  1. func (v *Var) Set(value interface{}) (old interface{})
  • Example:
  1. // Set
  2. func ExampleVar_Set() {
  3. var v = gvar.New(100.00)
  4. g.Dump(v.Set(200.00))
  5. g.Dump(v)
  6. // Output:
  7. // 100
  8. // "200"
  9. }

Val

  • Description: Val returns the current value of v, with a type of interface{}.
  • Format:
  1. func (v *Var) Val() interface{}
  • Example:
  1. // Val
  2. func ExampleVar_Val() {
  3. var v = gvar.New(100.00)
  4. g.DumpWithType(v.Val())
  5. // Output:
  6. // float64(100)
  7. }

Interface

  • Description: Interface is an alias for Val.
  • Format:
  1. func (v *Var) Interface() interface{}
  • Example:
  1. // Interface
  2. func ExampleVar_Interface() {
  3. var v = gvar.New(100.00)
  4. g.DumpWithType(v.Interface())
  5. // Output:
  6. // float64(100)
  7. }

Bytes

  • Description: Bytes converts v to a byte array.
  • Format:
  1. func (v *Var) Bytes() []byte
  • Example:
  1. // Bytes
  2. func ExampleVar_Bytes() {
  3. var v = gvar.New("GoFrame")
  4. g.DumpWithType(v.Bytes())
  5. // Output:
  6. // []byte(7) "GoFrame"
  7. }

String

  • Description: String converts v to a string.
  • Format:
  1. func (v *Var) String() string
  • Example:
  1. // String
  2. func ExampleVar_String() {
  3. var v = gvar.New("GoFrame")
  4. g.DumpWithType(v.String())
  5. // Output:
  6. // string(7) "GoFrame"
  7. }

Bool

  • Description: Bool converts v to a boolean value.
  • Format:
  1. func (v *Var) Bool() bool
  • Example:
  1. // Bool
  2. func ExampleVar_Bool() {
  3. var v = gvar.New(true)
  4. g.DumpWithType(v.Bool())
  5. // Output:
  6. // bool(true)
  7. }

Int

  • Description: Int converts v to an integer type.
  • Format:
  1. func (v *Var) Int() int
  • Example:
  1. // Int
  2. func ExampleVar_Int() {
  3. var v = gvar.New(-1000)
  4. g.DumpWithType(v.Int())
  5. // Output:
  6. // int(-1000)
  7. }

Uint

  • Description: Uint converts v to an unsigned integer type.
  • Format:
  1. func (v *Var) Uint() uint
  • Example:
  1. // Uint
  2. func ExampleVar_Uint() {
  3. var v = gvar.New(1000)
  4. g.DumpWithType(v.Uint())
  5. // Output:
  6. // uint(1000)
  7. }

Float32

  • Description: Float32 converts v to a 32-bit float type.
  • Format:
  1. func (v *Var) Float32() float32
  • Example:
  1. // Float32
  2. func ExampleVar_Float32() {
  3. var price = gvar.New(100.00)
  4. g.DumpWithType(price.Float32())
  5. // Output:
  6. // float32(100)
  7. }

Float64

  • Description: Float64 converts v to a 64-bit float type.
  • Format:
  1. func (v *Var) Float64() float64
  • Example:
  1. // Float32
  2. func ExampleVar_Float64() {
  3. var price = gvar.New(100.00)
  4. g.DumpWithType(price.Float64())
  5. // Output:
  6. // float64(100)
  7. }

Time

  • Description: Time converts v to time.Time. The format parameter is used to specify the time string format with gtime, e.g., Y-m-d H:i:s.
  • Format:
  1. func (v *Var) Time(format ...string) time.Time
  • Example:
  1. // Time
  2. func ExampleVar_Time() {
  3. var v = gvar.New("2021-11-11 00:00:00")
  4. g.DumpWithType(v.Time())
  5. // Output:
  6. // time.Time(29) "2021-11-11 00:00:00 +0800 CST"
  7. }

GTime

  • Description: G``Time converts v to *gtime.Time. The format parameter is used to specify the time string format with gtime, e.g., Y-m-d H:i:s.
  • Format:
  1. func (v *Var) GTime(format ...string) *gtime.Time
  • Example:
  1. // GTime
  2. func ExampleVar_GTime() {
  3. var v = gvar.New("2021-11-11 00:00:00")
  4. g.DumpWithType(v.GTime())
  5. // Output:
  6. // *gtime.Time(19) "2021-11-11 00:00:00"
  7. }

Duration

  • Description: Duration converts v to time.Duration. If the value of v is a string, it is converted using time.ParseDuration.
  • Format:
  1. func (v *Var) Duration() time.Duration
  • Example:
  1. // Duration
  2. func ExampleVar_Duration() {
  3. var v = gvar.New("300s")
  4. g.DumpWithType(v.Duration())
  5. // Output:
  6. // time.Duration(4) "5m0s"
  7. }

MarshalJSON

  • Description: MarshalJSON implements the MarshalJSON method of the json interface.
  • Format:
  1. func (v *Var) MarshalJSON() ([]byte, error)
  • Example:
  1. // MarshalJSON
  2. func ExampleVar_MarshalJSON() {
  3. testMap := g.Map{
  4. "code": "0001",
  5. "name": "Golang",
  6. "count": 10,
  7. }
  8. var v = gvar.New(testMap)
  9. res, err := json.Marshal(&v)
  10. if err != nil {
  11. panic(err)
  12. }
  13. g.DumpWithType(res)
  14. // Output:
  15. // []byte(42) "{"code":"0001","count":10,"name":"Golang"}"
  16. }

UnmarshalJSON

  • Description: UnmarshalJSON implements the UnmarshalJSON method of the json interface.
  • Format:
  1. func (v *Var) UnmarshalJSON(b []byte) error
  • Example:
  1. // UnmarshalJSON
  2. func ExampleVar_UnmarshalJSON() {
  3. tmp := []byte(`{
  4. "Code": "0003",
  5. "Name": "Golang Book3",
  6. "Quantity": 3000,
  7. "Price": 300,
  8. "OnSale": true
  9. }`)
  10. var v = gvar.New(map[string]interface{}{})
  11. if err := json.Unmarshal(tmp, &v); err != nil {
  12. panic(err)
  13. }
  14. g.Dump(v)
  15. // Output:
  16. // "{\"Code\":\"0003\",\"Name\":\"Golang Book3\",\"OnSale\":true,\"Price\":300,\"Quantity\":3000}"
  17. }

UnmarshalValue

  • Description: UnmarshalValue is an interface implementation that sets any type of value for Var.
  • Format:
  1. func (v *Var) UnmarshalValue(value interface{}) error
  • Example:
  1. // UnmarshalValue
  2. func ExampleVar_UnmarshalValue() {
  3. tmp := g.Map{
  4. "code": "00002",
  5. "name": "GoFrame",
  6. "price": 100,
  7. "sale": true,
  8. }
  9. var v = gvar.New(map[string]interface{}{})
  10. if err := v.UnmarshalValue(tmp); err != nil {
  11. panic(err)
  12. }
  13. g.Dump(v)
  14. // Output:
  15. // "{\"code\":\"00002\",\"name\":\"GoFrame\",\"price\":100,\"sale\":true}"
  16. }

IsNil

  • Description: IsNil checks if v is nil, returning true if it is nil, and false otherwise.
  • Format:
  1. func (v *Var) IsNil() bool
  • Example:
  1. /// IsNil
  2. func ExampleVar_IsNil() {
  3. g.Dump(gvar.New(0).IsNil())
  4. g.Dump(gvar.New(0.1).IsNil())
  5. // true
  6. g.Dump(gvar.New(nil).IsNil())
  7. g.Dump(gvar.New("").IsNil())
  8. // Output:
  9. // false
  10. // false
  11. // true
  12. // false
  13. }

IsEmpty

  • Description: IsEmpty checks if v is empty, returning true if it is, and false otherwise.
  • Format:
  1. func (v *Var) IsEmpty() bool
  • Example:
  1. // IsEmpty
  2. func ExampleVar_IsEmpty() {
  3. g.Dump(gvar.New(0).IsEmpty())
  4. g.Dump(gvar.New(nil).IsEmpty())
  5. g.Dump(gvar.New("").IsEmpty())
  6. g.Dump(gvar.New(g.Map{"k": "v"}).IsEmpty())
  7. // Output:
  8. // true
  9. // true
  10. // true
  11. // false
  12. }

IsInt

  • Description: IsInt checks if v is of int type, returning true if it is, and false otherwise.
  • Format:
  1. func (v *Var) IsInt() bool
  • Example:
  1. // IsInt
  2. func ExampleVar_IsInt() {
  3. g.Dump(gvar.New(0).IsInt())
  4. g.Dump(gvar.New(0.1).IsInt())
  5. g.Dump(gvar.New(nil).IsInt())
  6. g.Dump(gvar.New("").IsInt())
  7. // Output:
  8. // true
  9. // false
  10. // false
  11. // false
  12. }

IsUint

  • Description: IsUint checks if v is of uint type, returning true if it is, and false otherwise.
  • Format:
  1. func (v *Var) IsUint() bool
  • Example:
  1. // IsUint
  2. func ExampleVar_IsUint() {
  3. g.Dump(gvar.New(0).IsUint())
  4. g.Dump(gvar.New(uint8(8)).IsUint())
  5. g.Dump(gvar.New(nil).IsUint())
  6. // Output:
  7. // false
  8. // true
  9. // false
  10. }

IsFloat

  • Description: IsFloat checks if v is of float type, returning true if it is, and false otherwise.
  • Format:
  1. func (v *Var) IsFloat() bool
  • Example:
  1. // IsFloat
  2. func ExampleVar_IsFloat() {
  3. g.Dump(g.NewVar(uint8(8)).IsFloat())
  4. g.Dump(g.NewVar(float64(8)).IsFloat())
  5. g.Dump(g.NewVar(0.1).IsFloat())
  6. // Output:
  7. // false
  8. // true
  9. // true
  10. }

IsSlice

  • Description: IsSlice checks if v is of slice type, returning true if it is, and false otherwise.
  • Format:
  1. func (v *Var) IsSlice() bool
  • Example:
  1. // IsSlice
  2. func ExampleVar_IsSlice() {
  3. g.Dump(g.NewVar(0).IsSlice())
  4. g.Dump(g.NewVar(g.Slice{0}).IsSlice())
  5. // Output:
  6. // false
  7. // true
  8. }

IsMap

  • Description: IsMap checks if v is of map type, returning true if it is, and false otherwise.
  • Format:
  1. func (v *Var) IsMap() bool
  • Example:
  1. // IsMap
  2. func ExampleVar_IsMap() {
  3. g.Dump(g.NewVar(0).IsMap())
  4. g.Dump(g.NewVar(g.Map{"k": "v"}).IsMap())
  5. g.Dump(g.NewVar(g.Slice{}).IsMap())
  6. // Output:
  7. // false
  8. // true
  9. // false
  10. }

IsStruct

  • Description: IsStruct checks if v is of struct type, returning true if it is, and false otherwise.
  • Format:
  1. func (v *Var) IsStruct() bool
  • Example:
  1. // IsStruct
  2. func ExampleVar_IsStruct() {
  3. g.Dump(g.NewVar(0).IsStruct())
  4. g.Dump(g.NewVar(g.Map{"k": "v"}).IsStruct())
  5. a := struct{}{}
  6. g.Dump(g.NewVar(a).IsStruct())
  7. g.Dump(g.NewVar(&a).IsStruct())
  8. // Output:
  9. // false
  10. // false
  11. // true
  12. // true
  13. }

ListItemValues

  • Description: ListItemValues retrieves and returns all item elements of the struct/map with the key key. Note that the list parameter should be a slice type containing elements of map or struct, otherwise it will return an empty slice.
  • Format:
  1. func (v *Var) ListItemValues(key interface{}) (values []interface{})
  • Example:
  1. // ListItemValues
  2. func ExampleVar_ListItemValues() {
  3. var goods1 = g.List{
  4. g.Map{"id": 1, "price": 100.00},
  5. g.Map{"id": 2, "price": 0},
  6. g.Map{"id": 3, "price": nil},
  7. }
  8. var v = gvar.New(goods1)
  9. fmt.Println(v.ListItemValues("id"))
  10. fmt.Println(v.ListItemValues("price"))
  11. // Output:
  12. // [1 2 3]
  13. // [100 0 <nil>]
  14. }

ListItemValuesUnique

  • Description: ListItemValuesUnique retrieves and returns all unique elements of the struct/map with the specified key. Note that the list parameter should be a slice type containing elements of map or struct, otherwise it will return an empty slice.
  • Format:
  1. func (v *Var) ListItemValuesUnique(key string) []interface{}
  • Example:
  1. // ListItemValuesUnique
  2. func ExampleVar_ListItemValuesUnique() {
  3. var (
  4. goods1 = g.List{
  5. g.Map{"id": 1, "price": 100.00},
  6. g.Map{"id": 2, "price": 100.00},
  7. g.Map{"id": 3, "price": nil},
  8. }
  9. v = gvar.New(goods1)
  10. )
  11. fmt.Println(v.ListItemValuesUnique("id"))
  12. fmt.Println(v.ListItemValuesUnique("price"))
  13. // Output:
  14. // [1 2 3]
  15. // [100 <nil>]
  16. }

Struct

  • Description: Struct maps the value of v to the pointer. The pointer parameter should point to an instance of a struct. The mapping parameter is used to specify key-to-field mapping rules.
  • Format:
  1. func (v *Var) Struct(pointer interface{}, mapping ...map[string]string) error
  • Example:
  1. func ExampleVar_Struct() {
  2. params1 := g.Map{
  3. "uid": 1,
  4. "Name": "john",
  5. }
  6. v := gvar.New(params1)
  7. type tartget struct {
  8. Uid int
  9. Name string
  10. }
  11. t := new(tartget)
  12. if err := v.Struct(&t); err != nil {
  13. panic(err)
  14. }
  15. g.Dump(t)
  16. // Output:
  17. // {
  18. // Uid: 1,
  19. // Name: "john",
  20. // }
  21. }

Structs

  • Description: Structs converts v to a slice type of the given struct. The pointer parameter should point to an instance of a struct. The mapping parameter is used to specify key-to-field mapping rules.
  • Format:
  1. func (v *Var) Structs(pointer interface{}, mapping ...map[string]string) error
  • Example:
  1. func ExampleVar_Structs() {
  2. paramsArray := []g.Map{}
  3. params1 := g.Map{
  4. "uid": 1,
  5. "Name": "golang",
  6. }
  7. params2 := g.Map{
  8. "uid": 2,
  9. "Name": "java",
  10. }
  11. paramsArray = append(paramsArray, params1, params2)
  12. v := gvar.New(paramsArray)
  13. type tartget struct {
  14. Uid int
  15. Name string
  16. }
  17. var t []tartget
  18. if err := v.Structs(&t); err != nil {
  19. panic(err)
  20. }
  21. g.DumpWithType(t)
  22. // Output:
  23. // []gvar_test.tartget(2) [
  24. // gvar_test.tartget(2) {
  25. // Uid: int(1),
  26. // Name: string(6) "golang",
  27. // },
  28. // gvar_test.tartget(2) {
  29. // Uid: int(2),
  30. // Name: string(4) "java",
  31. // },
  32. // ]
  33. }

Ints

  • Description: Ints converts v to []int.
  • Format:
  1. func (v *Var) Ints() []int
  • Example:
  1. // Ints
  2. func ExampleVar_Ints() {
  3. var (
  4. arr = []int{1, 2, 3, 4, 5}
  5. obj = gvar.New(arr)
  6. )
  7. fmt.Println(obj.Ints())
  8. // Output:
  9. // [1 2 3 4 5]
  10. }

Int64s

  • Description: Int64s converts v to []int64.
  • Format:
  1. func (v *Var) Int64s() []int64
  • Example:
  1. // Int64s
  2. func ExampleVar_Int64s() {
  3. var (
  4. arr = []int64{1, 2, 3, 4, 5}
  5. obj = gvar.New(arr)
  6. )
  7. fmt.Println(obj.Int64s())
  8. // Output:
  9. // [1 2 3 4 5]
  10. }

Uints

  • Description: Uints converts v to []uint.
  • Format:
  1. func (v *Var) Uints() []uint
  • Example:
  1. // Uints
  2. func ExampleVar_Uints() {
  3. var (
  4. arr = []uint{1, 2, 3, 4, 5}
  5. obj = gvar.New(arr)
  6. )
  7. fmt.Println(obj.Uints())
  8. // Output:
  9. // [1 2 3 4 5]
  10. }

Uint64s

  • Description: Uint64s converts v to []uint64.
  • Format:
  1. func (v *Var) Uint64s() []uint64
  • Example:
  1. // Uint64s
  2. func ExampleVar_Uint64s() {
  3. var (
  4. arr = []uint64{1, 2, 3, 4, 5}
  5. obj = gvar.New(arr)
  6. )
  7. fmt.Println(obj.Uint64s())
  8. // Output:
  9. // [1 2 3 4 5]
  10. }

Floats

  • Description: Floats is an alias for Float64s.
  • Format:
  1. func (v *Var) Floats() []float64
  • Example:
  1. // Floats
  2. func ExampleVar_Floats() {
  3. var (
  4. arr = []float64{1, 2, 3, 4, 5}
  5. obj = gvar.New(arr)
  6. )
  7. fmt.Println(obj.Floats())
  8. // Output:
  9. // [1 2 3 4 5]
  10. }

Float64s

  • Description: Float64s converts v to []float64.
  • Format:
  1. func (v *Var) Float64s() []float64
  • Example:
  1. // Float64s
  2. func ExampleVar_Float64s() {
  3. var (
  4. arr = []float64{1, 2, 3, 4, 5}
  5. obj = gvar.New(arr)
  6. )
  7. fmt.Println(obj.Float64s())
  8. // Output:
  9. // [1 2 3 4 5]
  10. }

Float32s

  • Description: Float32s converts v to []float32.
  • Format:
  1. func (v *Var) Float32s() []float32
  • Example:
  1. // Float32s
  2. func ExampleVar_Float32s() {
  3. var (
  4. arr = []float32{1, 2, 3, 4, 5}
  5. obj = gvar.New(arr)
  6. )
  7. fmt.Println(obj.Float32s())
  8. // Output:
  9. // [1 2 3 4 5]
  10. }

Strings

  • Description: Strings converts v to []string.
  • Format:
  1. func (v *Var) Strings() []string
  • Example:
  1. // Strings
  2. func ExampleVar_Strings() {
  3. var (
  4. arr = []string{"GoFrame", "Golang"}
  5. obj = gvar.New(arr)
  6. )
  7. fmt.Println(obj.Strings())
  8. // Output:
  9. // [GoFrame Golang]
  10. }

Interfaces

  • Description: Interfaces converts v to []interface{}.
  • Format:
  1. func (v *Var) Interfaces() []interface{}
  • Example:
  1. // Interfaces
  2. func ExampleVar_Interfaces() {
  3. var (
  4. arr = []string{"GoFrame", "Golang"}
  5. obj = gvar.New(arr)
  6. )
  7. fmt.Println(obj.Interfaces())
  8. // Output:
  9. // [GoFrame Golang]
  10. }

Slice

  • Description: Slice is an alias for Interfaces.
  • Format:
  1. func (v *Var) Slice() []interface{}
  • Example:
  1. // Slice
  2. func ExampleVar_Slice() {
  3. var (
  4. arr = []string{"GoFrame", "Golang"}
  5. obj = gvar.New(arr)
  6. )
  7. fmt.Println(obj.Slice())
  8. // Output:
  9. // [GoFrame Golang]
  10. }

Array

  • Description: Array is an alias for Interfaces.
  • Format:
  1. func (v *Var) Array() []interface{}
  • Example:
  1. // Array
  2. func ExampleVar_Array() {
  3. var (
  4. arr = []string{"GoFrame", "Golang"}
  5. obj = gvar.New(arr)
  6. )
  7. fmt.Println(obj.Array())
  8. // Output:
  9. // [GoFrame Golang]
  10. }

Vars

  • Description: Vars converts v to []var.
  • Format:
  1. func (v *Var) Vars() []*Var
  • Example:
  1. // Vars
  2. func ExampleVar_Vars() {
  3. var (
  4. arr = []string{"GoFrame", "Golang"}
  5. obj = gvar.New(arr)
  6. )
  7. fmt.Println(obj.Vars())
  8. // Output:
  9. // [GoFrame Golang]
  10. }

Map

  • Description: Map converts v to map[string]interface{}.
  • Format:
  1. func (v *Var) Map(tags ...string) map[string]interface{}
  • Example:
  1. // Map
  2. func ExampleVar_Map() {
  3. var (
  4. m = g.Map{"id": 1, "price": 100.00}
  5. v = gvar.New(m)
  6. res = v.Map()
  7. )
  8. fmt.Println(res["id"], res["price"])
  9. // Output:
  10. // 1 100
  11. }

MapStrAny

  • Description: MapStrAny is similar to the Map function but implements the MapStrAny interface.
  • Format:
  1. func (v *Var) MapStrAny() map[string]interface{}
  • Example:
  1. // MapStrAny
  2. func ExampleVar_MapStrAny() {
  3. var (
  4. m1 = g.Map{"id": 1, "price": 100}
  5. v = gvar.New(m1)
  6. v2 = v.MapStrAny()
  7. )
  8. fmt.Println(v2["price"], v2["id"])
  9. // Output:
  10. // 100 1
  11. }

MapStrStr

  • Description: MapStrStr converts v to map[string]string.
  • Format:
  1. func (v *Var) MapStrStr(tags ...string) map[string]string
  • Example:
  1. // MapStrStr
  2. func ExampleVar_MapStrStr() {
  3. var (
  4. m1 = g.Map{"id": 1, "price": 100}
  5. v = gvar.New(m1)
  6. v2 = v.MapStrStr()
  7. )
  8. fmt.Println(v2["price"] + "$")
  9. // Output:
  10. // 100$
  11. }

MapStrVar

  • Description: MapStrVar converts v to map[string]*Var.
  • Format:
  1. func (v *Var) MapStrVar(tags ...string) map[string]*Var
  • Example:
  1. // MapStrVar
  2. func ExampleVar_MapStrVar() {
  3. var (
  4. m1 = g.Map{"id": 1, "price": 100}
  5. v = gvar.New(m1)
  6. v2 = v.MapStrVar()
  7. )
  8. fmt.Println(v2["price"].Float64() * 100)
  9. // Output:
  10. // 10000
  11. }

MapDeep

  • Description: MapDeep recursively converts v to map[string]interface{}.
  • Format:
  1. func (v *Var) MapDeep(tags ...string) map[string]interface{}
  • Example:
  1. // MapDeep
  2. func ExampleVar_MapDeep() {
  3. var (
  4. m1 = g.Map{"id": 1, "price": 100}
  5. m2 = g.Map{"product": m1}
  6. v = gvar.New(m2)
  7. v2 = v.MapDeep()
  8. )
  9. fmt.Println(v2["product"])
  10. // Output:
  11. // map[id:1 price:100]
  12. }

MapStrStrDeep

  • Description: MapStrStrDeep recursively converts v to map[string]string.
  • Format:
  1. func (v *Var) MapStrStrDeep(tags ...string) map[string]string
  • Example:
  1. // MapStrStrDeep
  2. func ExampleVar_MapStrStrDeep() {
  3. var (
  4. m1 = g.Map{"id": 1, "price": 100}
  5. m2 = g.Map{"product": m1}
  6. v = gvar.New(m2)
  7. v2 = v.MapStrStrDeep()
  8. )
  9. fmt.Println(v2["product"])
  10. // Output:
  11. // {"id":1,"price":100}
  12. }

MapStrVarDeep

  • Description: MapStrVarDeep recursively converts v to map[string]*Var.
  • Format:
  1. func (v *Var) MapStrVarDeep(tags ...string) map[string]*Var
  • Example:
  1. // MapStrVarDeep
  2. func ExampleVar_MapStrVarDeep() {
  3. var (
  4. m1 = g.Map{"id": 1, "price": 100}
  5. m2 = g.Map{"product": m1}
  6. v = gvar.New(m2)
  7. v2 = v.MapStrVarDeep()
  8. )
  9. fmt.Println(v2["product"])
  10. // Output:
  11. // {"id":1,"price":100}
  12. }

Maps

  • Description: Maps converts v to map[string]interface{}.
  • Format:
  1. func (v *Var) Maps(tags ...string) []map[string]interface{}
  • Example:
  1. // Maps
  2. func ExampleVar_Maps() {
  3. var m = gvar.New(g.ListIntInt{g.MapIntInt{0: 100, 1: 200}, g.MapIntInt{0: 300, 1: 400}})
  4. fmt.Printf("%#v", m.Maps())
  5. // Output:
  6. // []map[string]interface {}{map[string]interface {}{"0":100, "1":200}, map[string]interface {}{"0":300, "1":400}}
  7. }

MapsDeep

  • Description: MapsDeep recursively converts v to []map[string]interface{}.
  • Format:
  1. func (v *Var) MapsDeep(tags ...string) []map[string]interface{}
  • Example:
  1. // MapsDeep
  2. func ExampleVar_MapsDeep() {
  3. var (
  4. p1 = g.MapStrAny{"product": g.Map{"id": 1, "price": 100}}
  5. p2 = g.MapStrAny{"product": g.Map{"id": 2, "price": 200}}
  6. v = gvar.New(g.ListStrAny{p1, p2})
  7. v2 = v.MapsDeep()
  8. )
  9. fmt.Printf("%#v", v2)
  10. // Output:
  11. // []map[string]interface {}{map[string]interface {}{"product":map[string]interface {}{"id":1, "price":100}}, map[string]interface {}{"product":map[string]interface {}{"id":2, "price":200}}}
  12. }

MapToMap

  • Description: MapToMap converts v to the map type specified by pointer, with mapping as the specified mapping rules.
  • Format:
  1. func (v *Var) MapToMap(pointer interface{}, mapping ...map[string]string) (err error)
  • Example:
  1. // MapToMap
  2. func ExampleVar_MapToMap() {
  3. var (
  4. m1 = gvar.New(g.MapIntInt{0: 100, 1: 200})
  5. m2 = g.MapStrStr{}
  6. )
  7. err := m1.MapToMap(&m2)
  8. if err != nil {
  9. panic(err)
  10. }
  11. fmt.Printf("%#v", m2)
  12. // Output:
  13. // map[string]string{"0":"100", "1":"200"}
  14. }

MapToMaps

  • Description: MapToMaps converts v to the map type specified by pointer, with mapping as the specified mapping rules.
  • Format:
  1. func (v *Var) MapToMaps(pointer interface{}, mapping ...map[string]string) (err error)
  • Example:
  1. // MapToMaps
  2. func ExampleVar_MapToMaps() {
  3. var (
  4. p1 = g.MapStrAny{"product": g.Map{"id": 1, "price": 100}}
  5. p2 = g.MapStrAny{"product": g.Map{"id": 2, "price": 200}}
  6. v = gvar.New(g.ListStrAny{p1, p2})
  7. v2 []g.MapStrStr
  8. )
  9. err := v.MapToMaps(&v2)
  10. if err != nil {
  11. panic(err)
  12. }
  13. fmt.Printf("%#v", v2)
  14. // Output:
  15. // []map[string]string{map[string]string{"product":"{\"id\":1,\"price\":100}"}, map[string]string{"product":"{\"id\":2,\"price\":200}"}}
  16. }

MapToMapsDeep

  • Description: MapToMapsDeep recursively converts v to the map type specified by pointer, with mapping as the designated mapping rules.
  • Format:
  1. func (v *Var) MapToMapsDeep(pointer interface{}, mapping ...map[string]string) (err error)
  • Example:
  1. // MapToMapDeep
  2. func ExampleVar_MapToMapDeep() {
  3. var (
  4. p1 = gvar.New(g.MapStrAny{"product": g.Map{"id": 1, "price": 100}})
  5. p2 = g.MapStrAny{}
  6. )
  7. err := p1.MapToMap(&p2)
  8. if err != nil {
  9. panic(err)
  10. }
  11. fmt.Printf("%#v", p2)
  12. // Output:
  13. // map[string]interface {}{"product":map[string]interface {}{"id":1, "price":100}}
  14. }

Scan

  • Description: Scan automatically checks the type of pointer and converts params to pointer. Supported types for pointer are: *map, *[]map, *[]*map, *struct, **struct, *[]struct, *[]*struct
  • Format:
  1. func (v *Var) Scan(pointer interface{}, mapping ...map[string]string) error
  • Example:
  1. // Scan
  2. func ExampleVar_Scan() {
  3. type Student struct {
  4. Id *g.Var
  5. Name *g.Var
  6. Scores *g.Var
  7. }
  8. var (
  9. s Student
  10. m = g.Map{
  11. "Id": 1,
  12. "Name": "john",
  13. "Scores": []int{100, 99, 98},
  14. }
  15. )
  16. if err := gconv.Scan(m, &s); err != nil {
  17. panic(err)
  18. }
  19. g.DumpWithType(s)
  20. // Output:
  21. // gvar_test.Student(3) {
  22. // Id: *gvar.Var(1) "1",
  23. // Name: *gvar.Var(4) "john",
  24. // Scores: *gvar.Var(11) "[100,99,98]",
  25. // }
  26. }