New
Clone
Set
Val
Interface
Bytes
String
Bool
Int
Uint
Float32
Float64
Time
GTime
Duration
MarshalJSON
UnmarshalJSON
UnmarshalValue
IsNil
IsEmpty
IsInt
IsUint
IsFloat
IsSlice
IsMap
IsStruct
ListItemValues
ListItemValuesUnique
Struct
Structs
Ints
Int64s
Uints
Uint64s
Floats
Float64s
Float32s
Strings
Interfaces
Slice
Array
Vars
Map
MapStrAny
MapStrStr
MapStrVar
MapDeep
MapStrStrDeep
MapStrVarDeep
Maps
MapsDeep
MapToMap
MapToMaps
MapToMapsDeep
Scan
tip
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 newVar
with the givenvalue
. The optional parametersafe
specifies whether to useVar
in a concurrent-safe manner, with a default value offalse
. - Format:
func New(value interface{}, safe ...bool) *Var
- Example:
// New
func ExampleVarNew() {
v := gvar.New(400)
g.Dump(v)
// Output:
// "400"
}
Clone
- Description:
Clone
performs a shallow copy of the currentVar
and returns a pointer to thisVar
. - Format:
func (v *Var) Clone() *Var
- Example
// Clone
func ExampleVar_Clone() {
tmp := "fisrt hello"
v := gvar.New(tmp)
g.DumpWithType(v.Clone())
fmt.Println(v == v.Clone())
// Output:
// *gvar.Var(11) "fisrt hello"
// false
}
Set
- Description:
Set
sets the value ofv
tovalue
and returns the old value ofv
. - Format:
func (v *Var) Set(value interface{}) (old interface{})
- Example:
// Set
func ExampleVar_Set() {
var v = gvar.New(100.00)
g.Dump(v.Set(200.00))
g.Dump(v)
// Output:
// 100
// "200"
}
Val
- Description:
Val
returns the current value ofv
, with a type ofinterface{}
. - Format:
func (v *Var) Val() interface{}
- Example:
// Val
func ExampleVar_Val() {
var v = gvar.New(100.00)
g.DumpWithType(v.Val())
// Output:
// float64(100)
}
Interface
- Description:
Interface
is an alias forVal
. - Format:
func (v *Var) Interface() interface{}
- Example:
// Interface
func ExampleVar_Interface() {
var v = gvar.New(100.00)
g.DumpWithType(v.Interface())
// Output:
// float64(100)
}
Bytes
- Description:
Bytes
convertsv
to a byte array. - Format:
func (v *Var) Bytes() []byte
- Example:
// Bytes
func ExampleVar_Bytes() {
var v = gvar.New("GoFrame")
g.DumpWithType(v.Bytes())
// Output:
// []byte(7) "GoFrame"
}
String
- Description:
String
convertsv
to a string. - Format:
func (v *Var) String() string
- Example:
// String
func ExampleVar_String() {
var v = gvar.New("GoFrame")
g.DumpWithType(v.String())
// Output:
// string(7) "GoFrame"
}
Bool
- Description:
Bool
convertsv
to a boolean value. - Format:
func (v *Var) Bool() bool
- Example:
// Bool
func ExampleVar_Bool() {
var v = gvar.New(true)
g.DumpWithType(v.Bool())
// Output:
// bool(true)
}
Int
- Description:
Int
convertsv
to an integer type. - Format:
func (v *Var) Int() int
- Example:
// Int
func ExampleVar_Int() {
var v = gvar.New(-1000)
g.DumpWithType(v.Int())
// Output:
// int(-1000)
}
Uint
- Description:
Uint
convertsv
to an unsigned integer type. - Format:
func (v *Var) Uint() uint
- Example:
// Uint
func ExampleVar_Uint() {
var v = gvar.New(1000)
g.DumpWithType(v.Uint())
// Output:
// uint(1000)
}
Float32
- Description:
Float32
convertsv
to a32-bit
float type. - Format:
func (v *Var) Float32() float32
- Example:
// Float32
func ExampleVar_Float32() {
var price = gvar.New(100.00)
g.DumpWithType(price.Float32())
// Output:
// float32(100)
}
Float64
- Description:
Float64
convertsv
to a64-bit
float type. - Format:
func (v *Var) Float64() float64
- Example:
// Float32
func ExampleVar_Float64() {
var price = gvar.New(100.00)
g.DumpWithType(price.Float64())
// Output:
// float64(100)
}
Time
- Description:
Time
convertsv
totime.Time
. Theformat
parameter is used to specify the time string format withgtime
, e.g.,Y-m-d H:i:s
. - Format:
func (v *Var) Time(format ...string) time.Time
- Example:
// Time
func ExampleVar_Time() {
var v = gvar.New("2021-11-11 00:00:00")
g.DumpWithType(v.Time())
// Output:
// time.Time(29) "2021-11-11 00:00:00 +0800 CST"
}
GTime
- Description:
G``Time
convertsv
to*gtime.Time
. Theformat
parameter is used to specify the time string format withgtime
, e.g.,Y-m-d H:i:s
. - Format:
func (v *Var) GTime(format ...string) *gtime.Time
- Example:
// GTime
func ExampleVar_GTime() {
var v = gvar.New("2021-11-11 00:00:00")
g.DumpWithType(v.GTime())
// Output:
// *gtime.Time(19) "2021-11-11 00:00:00"
}
Duration
- Description:
Duration
convertsv
totime.Duration
. If the value ofv
is a string, it is converted usingtime.ParseDuration
. - Format:
func (v *Var) Duration() time.Duration
- Example:
// Duration
func ExampleVar_Duration() {
var v = gvar.New("300s")
g.DumpWithType(v.Duration())
// Output:
// time.Duration(4) "5m0s"
}
MarshalJSON
- Description:
MarshalJSON
implements theMarshalJSON
method of thejson
interface. - Format:
func (v *Var) MarshalJSON() ([]byte, error)
- Example:
// MarshalJSON
func ExampleVar_MarshalJSON() {
testMap := g.Map{
"code": "0001",
"name": "Golang",
"count": 10,
}
var v = gvar.New(testMap)
res, err := json.Marshal(&v)
if err != nil {
panic(err)
}
g.DumpWithType(res)
// Output:
// []byte(42) "{"code":"0001","count":10,"name":"Golang"}"
}
UnmarshalJSON
- Description:
UnmarshalJSON
implements theUnmarshalJSON
method of thejson
interface. - Format:
func (v *Var) UnmarshalJSON(b []byte) error
- Example:
// UnmarshalJSON
func ExampleVar_UnmarshalJSON() {
tmp := []byte(`{
"Code": "0003",
"Name": "Golang Book3",
"Quantity": 3000,
"Price": 300,
"OnSale": true
}`)
var v = gvar.New(map[string]interface{}{})
if err := json.Unmarshal(tmp, &v); err != nil {
panic(err)
}
g.Dump(v)
// Output:
// "{\"Code\":\"0003\",\"Name\":\"Golang Book3\",\"OnSale\":true,\"Price\":300,\"Quantity\":3000}"
}
UnmarshalValue
- Description:
UnmarshalValue
is an interface implementation that sets any type of value forVar
. - Format:
func (v *Var) UnmarshalValue(value interface{}) error
- Example:
// UnmarshalValue
func ExampleVar_UnmarshalValue() {
tmp := g.Map{
"code": "00002",
"name": "GoFrame",
"price": 100,
"sale": true,
}
var v = gvar.New(map[string]interface{}{})
if err := v.UnmarshalValue(tmp); err != nil {
panic(err)
}
g.Dump(v)
// Output:
// "{\"code\":\"00002\",\"name\":\"GoFrame\",\"price\":100,\"sale\":true}"
}
IsNil
- Description:
IsNil
checks ifv
isnil
, returningtrue
if it isnil
, andfalse
otherwise. - Format:
func (v *Var) IsNil() bool
- Example:
/// IsNil
func ExampleVar_IsNil() {
g.Dump(gvar.New(0).IsNil())
g.Dump(gvar.New(0.1).IsNil())
// true
g.Dump(gvar.New(nil).IsNil())
g.Dump(gvar.New("").IsNil())
// Output:
// false
// false
// true
// false
}
IsEmpty
- Description:
IsEmpty
checks ifv
is empty, returningtrue
if it is, andfalse
otherwise. - Format:
func (v *Var) IsEmpty() bool
- Example:
// IsEmpty
func ExampleVar_IsEmpty() {
g.Dump(gvar.New(0).IsEmpty())
g.Dump(gvar.New(nil).IsEmpty())
g.Dump(gvar.New("").IsEmpty())
g.Dump(gvar.New(g.Map{"k": "v"}).IsEmpty())
// Output:
// true
// true
// true
// false
}
IsInt
- Description:
IsInt
checks ifv
is ofint
type, returningtrue
if it is, andfalse
otherwise. - Format:
func (v *Var) IsInt() bool
- Example:
// IsInt
func ExampleVar_IsInt() {
g.Dump(gvar.New(0).IsInt())
g.Dump(gvar.New(0.1).IsInt())
g.Dump(gvar.New(nil).IsInt())
g.Dump(gvar.New("").IsInt())
// Output:
// true
// false
// false
// false
}
IsUint
- Description:
IsUint
checks ifv
is ofuint
type, returningtrue
if it is, andfalse
otherwise. - Format:
func (v *Var) IsUint() bool
- Example:
// IsUint
func ExampleVar_IsUint() {
g.Dump(gvar.New(0).IsUint())
g.Dump(gvar.New(uint8(8)).IsUint())
g.Dump(gvar.New(nil).IsUint())
// Output:
// false
// true
// false
}
IsFloat
- Description:
IsFloat
checks ifv
is offloat
type, returningtrue
if it is, andfalse
otherwise. - Format:
func (v *Var) IsFloat() bool
- Example:
// IsFloat
func ExampleVar_IsFloat() {
g.Dump(g.NewVar(uint8(8)).IsFloat())
g.Dump(g.NewVar(float64(8)).IsFloat())
g.Dump(g.NewVar(0.1).IsFloat())
// Output:
// false
// true
// true
}
IsSlice
- Description:
IsSlice
checks ifv
is of slice type, returningtrue
if it is, andfalse
otherwise. - Format:
func (v *Var) IsSlice() bool
- Example:
// IsSlice
func ExampleVar_IsSlice() {
g.Dump(g.NewVar(0).IsSlice())
g.Dump(g.NewVar(g.Slice{0}).IsSlice())
// Output:
// false
// true
}
IsMap
- Description:
IsMap
checks ifv
is of map type, returningtrue
if it is, andfalse
otherwise. - Format:
func (v *Var) IsMap() bool
- Example:
// IsMap
func ExampleVar_IsMap() {
g.Dump(g.NewVar(0).IsMap())
g.Dump(g.NewVar(g.Map{"k": "v"}).IsMap())
g.Dump(g.NewVar(g.Slice{}).IsMap())
// Output:
// false
// true
// false
}
IsStruct
- Description:
IsStruct
checks ifv
is of struct type, returningtrue
if it is, andfalse
otherwise. - Format:
func (v *Var) IsStruct() bool
- Example:
// IsStruct
func ExampleVar_IsStruct() {
g.Dump(g.NewVar(0).IsStruct())
g.Dump(g.NewVar(g.Map{"k": "v"}).IsStruct())
a := struct{}{}
g.Dump(g.NewVar(a).IsStruct())
g.Dump(g.NewVar(&a).IsStruct())
// Output:
// false
// false
// true
// true
}
ListItemValues
- Description:
ListItemValues
retrieves and returns all item elements of the struct/map with the keykey
. Note that thelist
parameter should be a slice type containing elements ofmap
orstruct
, otherwise it will return an empty slice. - Format:
func (v *Var) ListItemValues(key interface{}) (values []interface{})
- Example:
// ListItemValues
func ExampleVar_ListItemValues() {
var goods1 = g.List{
g.Map{"id": 1, "price": 100.00},
g.Map{"id": 2, "price": 0},
g.Map{"id": 3, "price": nil},
}
var v = gvar.New(goods1)
fmt.Println(v.ListItemValues("id"))
fmt.Println(v.ListItemValues("price"))
// Output:
// [1 2 3]
// [100 0 <nil>]
}
ListItemValuesUnique
- Description:
ListItemValuesUnique
retrieves and returns all unique elements of the struct/map with the specifiedkey
. Note that thelist
parameter should be a slice type containing elements ofmap
orstruct
, otherwise it will return an empty slice. - Format:
func (v *Var) ListItemValuesUnique(key string) []interface{}
- Example:
// ListItemValuesUnique
func ExampleVar_ListItemValuesUnique() {
var (
goods1 = g.List{
g.Map{"id": 1, "price": 100.00},
g.Map{"id": 2, "price": 100.00},
g.Map{"id": 3, "price": nil},
}
v = gvar.New(goods1)
)
fmt.Println(v.ListItemValuesUnique("id"))
fmt.Println(v.ListItemValuesUnique("price"))
// Output:
// [1 2 3]
// [100 <nil>]
}
Struct
- Description:
Struct
maps the value ofv
to thepointer
. Thepointer
parameter should point to an instance of a struct. Themapping
parameter is used to specify key-to-field mapping rules. - Format:
func (v *Var) Struct(pointer interface{}, mapping ...map[string]string) error
- Example:
func ExampleVar_Struct() {
params1 := g.Map{
"uid": 1,
"Name": "john",
}
v := gvar.New(params1)
type tartget struct {
Uid int
Name string
}
t := new(tartget)
if err := v.Struct(&t); err != nil {
panic(err)
}
g.Dump(t)
// Output:
// {
// Uid: 1,
// Name: "john",
// }
}
Structs
- Description:
Structs
convertsv
to a slice type of the given struct. Thepointer
parameter should point to an instance of a struct. Themapping
parameter is used to specify key-to-field mapping rules. - Format:
func (v *Var) Structs(pointer interface{}, mapping ...map[string]string) error
- Example:
func ExampleVar_Structs() {
paramsArray := []g.Map{}
params1 := g.Map{
"uid": 1,
"Name": "golang",
}
params2 := g.Map{
"uid": 2,
"Name": "java",
}
paramsArray = append(paramsArray, params1, params2)
v := gvar.New(paramsArray)
type tartget struct {
Uid int
Name string
}
var t []tartget
if err := v.Structs(&t); err != nil {
panic(err)
}
g.DumpWithType(t)
// Output:
// []gvar_test.tartget(2) [
// gvar_test.tartget(2) {
// Uid: int(1),
// Name: string(6) "golang",
// },
// gvar_test.tartget(2) {
// Uid: int(2),
// Name: string(4) "java",
// },
// ]
}
Ints
- Description:
Ints
convertsv
to[]int
. - Format:
func (v *Var) Ints() []int
- Example:
// Ints
func ExampleVar_Ints() {
var (
arr = []int{1, 2, 3, 4, 5}
obj = gvar.New(arr)
)
fmt.Println(obj.Ints())
// Output:
// [1 2 3 4 5]
}
Int64s
- Description:
Int64s
convertsv
to[]int64
. - Format:
func (v *Var) Int64s() []int64
- Example:
// Int64s
func ExampleVar_Int64s() {
var (
arr = []int64{1, 2, 3, 4, 5}
obj = gvar.New(arr)
)
fmt.Println(obj.Int64s())
// Output:
// [1 2 3 4 5]
}
Uints
- Description:
Uints
convertsv
to[]uint
. - Format:
func (v *Var) Uints() []uint
- Example:
// Uints
func ExampleVar_Uints() {
var (
arr = []uint{1, 2, 3, 4, 5}
obj = gvar.New(arr)
)
fmt.Println(obj.Uints())
// Output:
// [1 2 3 4 5]
}
Uint64s
- Description:
Uint64s
convertsv
to[]uint64
. - Format:
func (v *Var) Uint64s() []uint64
- Example:
// Uint64s
func ExampleVar_Uint64s() {
var (
arr = []uint64{1, 2, 3, 4, 5}
obj = gvar.New(arr)
)
fmt.Println(obj.Uint64s())
// Output:
// [1 2 3 4 5]
}
Floats
- Description:
Floats
is an alias forFloat64s
. - Format:
func (v *Var) Floats() []float64
- Example:
// Floats
func ExampleVar_Floats() {
var (
arr = []float64{1, 2, 3, 4, 5}
obj = gvar.New(arr)
)
fmt.Println(obj.Floats())
// Output:
// [1 2 3 4 5]
}
Float64s
- Description:
Float64s
convertsv
to[]float64
. - Format:
func (v *Var) Float64s() []float64
- Example:
// Float64s
func ExampleVar_Float64s() {
var (
arr = []float64{1, 2, 3, 4, 5}
obj = gvar.New(arr)
)
fmt.Println(obj.Float64s())
// Output:
// [1 2 3 4 5]
}
Float32s
- Description:
Float32s
convertsv
to[]float32
. - Format:
func (v *Var) Float32s() []float32
- Example:
// Float32s
func ExampleVar_Float32s() {
var (
arr = []float32{1, 2, 3, 4, 5}
obj = gvar.New(arr)
)
fmt.Println(obj.Float32s())
// Output:
// [1 2 3 4 5]
}
Strings
- Description:
Strings
convertsv
to[]string
. - Format:
func (v *Var) Strings() []string
- Example:
// Strings
func ExampleVar_Strings() {
var (
arr = []string{"GoFrame", "Golang"}
obj = gvar.New(arr)
)
fmt.Println(obj.Strings())
// Output:
// [GoFrame Golang]
}
Interfaces
- Description:
Interfaces
convertsv
to[]interface{}
. - Format:
func (v *Var) Interfaces() []interface{}
- Example:
// Interfaces
func ExampleVar_Interfaces() {
var (
arr = []string{"GoFrame", "Golang"}
obj = gvar.New(arr)
)
fmt.Println(obj.Interfaces())
// Output:
// [GoFrame Golang]
}
Slice
- Description:
Slice
is an alias forInterfaces
. - Format:
func (v *Var) Slice() []interface{}
- Example:
// Slice
func ExampleVar_Slice() {
var (
arr = []string{"GoFrame", "Golang"}
obj = gvar.New(arr)
)
fmt.Println(obj.Slice())
// Output:
// [GoFrame Golang]
}
Array
- Description:
Array
is an alias forInterfaces
. - Format:
func (v *Var) Array() []interface{}
- Example:
// Array
func ExampleVar_Array() {
var (
arr = []string{"GoFrame", "Golang"}
obj = gvar.New(arr)
)
fmt.Println(obj.Array())
// Output:
// [GoFrame Golang]
}
Vars
- Description:
Vars
convertsv
to[]var
. - Format:
func (v *Var) Vars() []*Var
- Example:
// Vars
func ExampleVar_Vars() {
var (
arr = []string{"GoFrame", "Golang"}
obj = gvar.New(arr)
)
fmt.Println(obj.Vars())
// Output:
// [GoFrame Golang]
}
Map
- Description:
Map
convertsv
tomap[string]interface{}
. - Format:
func (v *Var) Map(tags ...string) map[string]interface{}
- Example:
// Map
func ExampleVar_Map() {
var (
m = g.Map{"id": 1, "price": 100.00}
v = gvar.New(m)
res = v.Map()
)
fmt.Println(res["id"], res["price"])
// Output:
// 1 100
}
MapStrAny
- Description:
MapStrAny
is similar to theMap
function but implements theMapStrAny
interface. - Format:
func (v *Var) MapStrAny() map[string]interface{}
- Example:
// MapStrAny
func ExampleVar_MapStrAny() {
var (
m1 = g.Map{"id": 1, "price": 100}
v = gvar.New(m1)
v2 = v.MapStrAny()
)
fmt.Println(v2["price"], v2["id"])
// Output:
// 100 1
}
MapStrStr
- Description:
MapStrStr
convertsv
tomap[string]string
. - Format:
func (v *Var) MapStrStr(tags ...string) map[string]string
- Example:
// MapStrStr
func ExampleVar_MapStrStr() {
var (
m1 = g.Map{"id": 1, "price": 100}
v = gvar.New(m1)
v2 = v.MapStrStr()
)
fmt.Println(v2["price"] + "$")
// Output:
// 100$
}
MapStrVar
- Description:
MapStrVar
convertsv
tomap[string]*Var
. - Format:
func (v *Var) MapStrVar(tags ...string) map[string]*Var
- Example:
// MapStrVar
func ExampleVar_MapStrVar() {
var (
m1 = g.Map{"id": 1, "price": 100}
v = gvar.New(m1)
v2 = v.MapStrVar()
)
fmt.Println(v2["price"].Float64() * 100)
// Output:
// 10000
}
MapDeep
- Description:
MapDeep
recursively convertsv
tomap[string]interface{}
. - Format:
func (v *Var) MapDeep(tags ...string) map[string]interface{}
- Example:
// MapDeep
func ExampleVar_MapDeep() {
var (
m1 = g.Map{"id": 1, "price": 100}
m2 = g.Map{"product": m1}
v = gvar.New(m2)
v2 = v.MapDeep()
)
fmt.Println(v2["product"])
// Output:
// map[id:1 price:100]
}
MapStrStrDeep
- Description:
MapStrStrDeep
recursively convertsv
tomap[string]string
. - Format:
func (v *Var) MapStrStrDeep(tags ...string) map[string]string
- Example:
// MapStrStrDeep
func ExampleVar_MapStrStrDeep() {
var (
m1 = g.Map{"id": 1, "price": 100}
m2 = g.Map{"product": m1}
v = gvar.New(m2)
v2 = v.MapStrStrDeep()
)
fmt.Println(v2["product"])
// Output:
// {"id":1,"price":100}
}
MapStrVarDeep
- Description:
MapStrVarDeep
recursively convertsv
tomap[string]*Var
. - Format:
func (v *Var) MapStrVarDeep(tags ...string) map[string]*Var
- Example:
// MapStrVarDeep
func ExampleVar_MapStrVarDeep() {
var (
m1 = g.Map{"id": 1, "price": 100}
m2 = g.Map{"product": m1}
v = gvar.New(m2)
v2 = v.MapStrVarDeep()
)
fmt.Println(v2["product"])
// Output:
// {"id":1,"price":100}
}
Maps
- Description:
Maps
convertsv
tomap[string]interface{}
. - Format:
func (v *Var) Maps(tags ...string) []map[string]interface{}
- Example:
// Maps
func ExampleVar_Maps() {
var m = gvar.New(g.ListIntInt{g.MapIntInt{0: 100, 1: 200}, g.MapIntInt{0: 300, 1: 400}})
fmt.Printf("%#v", m.Maps())
// Output:
// []map[string]interface {}{map[string]interface {}{"0":100, "1":200}, map[string]interface {}{"0":300, "1":400}}
}
MapsDeep
- Description:
MapsDeep
recursively convertsv
to[]map[string]interface{}
. - Format:
func (v *Var) MapsDeep(tags ...string) []map[string]interface{}
- Example:
// MapsDeep
func ExampleVar_MapsDeep() {
var (
p1 = g.MapStrAny{"product": g.Map{"id": 1, "price": 100}}
p2 = g.MapStrAny{"product": g.Map{"id": 2, "price": 200}}
v = gvar.New(g.ListStrAny{p1, p2})
v2 = v.MapsDeep()
)
fmt.Printf("%#v", v2)
// Output:
// []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}}}
}
MapToMap
- Description:
MapToMap
convertsv
to themap
type specified bypointer
, withmapping
as the specified mapping rules. - Format:
func (v *Var) MapToMap(pointer interface{}, mapping ...map[string]string) (err error)
- Example:
// MapToMap
func ExampleVar_MapToMap() {
var (
m1 = gvar.New(g.MapIntInt{0: 100, 1: 200})
m2 = g.MapStrStr{}
)
err := m1.MapToMap(&m2)
if err != nil {
panic(err)
}
fmt.Printf("%#v", m2)
// Output:
// map[string]string{"0":"100", "1":"200"}
}
MapToMaps
- Description:
MapToMaps
convertsv
to themap
type specified bypointer
, withmapping
as the specified mapping rules. - Format:
func (v *Var) MapToMaps(pointer interface{}, mapping ...map[string]string) (err error)
- Example:
// MapToMaps
func ExampleVar_MapToMaps() {
var (
p1 = g.MapStrAny{"product": g.Map{"id": 1, "price": 100}}
p2 = g.MapStrAny{"product": g.Map{"id": 2, "price": 200}}
v = gvar.New(g.ListStrAny{p1, p2})
v2 []g.MapStrStr
)
err := v.MapToMaps(&v2)
if err != nil {
panic(err)
}
fmt.Printf("%#v", v2)
// Output:
// []map[string]string{map[string]string{"product":"{\"id\":1,\"price\":100}"}, map[string]string{"product":"{\"id\":2,\"price\":200}"}}
}
MapToMapsDeep
- Description:
MapToMapsDeep
recursively convertsv
to themap
type specified bypointer
, withmapping
as the designated mapping rules. - Format:
func (v *Var) MapToMapsDeep(pointer interface{}, mapping ...map[string]string) (err error)
- Example:
// MapToMapDeep
func ExampleVar_MapToMapDeep() {
var (
p1 = gvar.New(g.MapStrAny{"product": g.Map{"id": 1, "price": 100}})
p2 = g.MapStrAny{}
)
err := p1.MapToMap(&p2)
if err != nil {
panic(err)
}
fmt.Printf("%#v", p2)
// Output:
// map[string]interface {}{"product":map[string]interface {}{"id":1, "price":100}}
}
Scan
- Description:
Scan
automatically checks the type ofpointer
and convertsparams
topointer
. Supported types forpointer
are:*map, *[]map, *[]*map, *struct, **struct, *[]struct, *[]*struct
- Format:
func (v *Var) Scan(pointer interface{}, mapping ...map[string]string) error
- Example:
// Scan
func ExampleVar_Scan() {
type Student struct {
Id *g.Var
Name *g.Var
Scores *g.Var
}
var (
s Student
m = g.Map{
"Id": 1,
"Name": "john",
"Scores": []int{100, 99, 98},
}
)
if err := gconv.Scan(m, &s); err != nil {
panic(err)
}
g.DumpWithType(s)
// Output:
// gvar_test.Student(3) {
// Id: *gvar.Var(1) "1",
// Name: *gvar.Var(4) "john",
// Scores: *gvar.Var(11) "[100,99,98]",
// }
}