Documentation
¶
Overview ¶
Package gvar provides an universal variable type, like runtime generics.
Index ¶
- type MapOption
- type Var
- func (v *Var) Array() []any
- func (v *Var) Bool() bool
- func (v *Var) Bytes() []byte
- func (v *Var) Clone() *Var
- func (v *Var) Copy() *Var
- func (v *Var) DeepCopy() any
- func (v *Var) Duration() time.Duration
- func (v *Var) Float32() float32
- func (v *Var) Float32s() []float32
- func (v *Var) Float64() float64
- func (v *Var) Float64s() []float64
- func (v *Var) Floats() []float64
- func (v *Var) GTime(format ...string) *gtime.Time
- func (v *Var) Int() int
- func (v *Var) Int16() int16
- func (v *Var) Int32() int32
- func (v *Var) Int64() int64
- func (v *Var) Int64s() []int64
- func (v *Var) Int8() int8
- func (v *Var) Interface() any
- func (v *Var) Interfaces() []any
- func (v *Var) Ints() []int
- func (v *Var) IsEmpty() bool
- func (v *Var) IsFloat() bool
- func (v *Var) IsInt() bool
- func (v *Var) IsMap() bool
- func (v *Var) IsNil() bool
- func (v *Var) IsSlice() bool
- func (v *Var) IsStruct() bool
- func (v *Var) IsUint() bool
- func (v *Var) ListItemValues(key any) (values []any)
- func (v *Var) ListItemValuesUnique(key string) []any
- func (v *Var) Map(option ...MapOption) map[string]any
- func (v *Var) MapDeep(tags ...string) map[string]any
- func (v *Var) MapStrAny(option ...MapOption) map[string]any
- func (v *Var) MapStrStr(option ...MapOption) map[string]string
- func (v *Var) MapStrStrDeep(tags ...string) map[string]string
- func (v *Var) MapStrVar(option ...MapOption) map[string]*Var
- func (v *Var) MapStrVarDeep(tags ...string) map[string]*Var
- func (v *Var) MapToMap(pointer any, mapping ...map[string]string) (err error)
- func (v *Var) MapToMaps(pointer any, mapping ...map[string]string) (err error)
- func (v *Var) MapToMapsDeep(pointer any, mapping ...map[string]string) (err error)
- func (v *Var) Maps(option ...MapOption) []map[string]any
- func (v *Var) MapsDeep(tags ...string) []map[string]any
- func (v *Var) MarshalJSON() ([]byte, error)
- func (v *Var) Scan(pointer any, mapping ...map[string]string) error
- func (v *Var) Set(value any) (old any)
- func (v *Var) Slice() []any
- func (v *Var) String() string
- func (v *Var) Strings() []string
- func (v *Var) Struct(pointer any, mapping ...map[string]string) error
- func (v *Var) Structs(pointer any, mapping ...map[string]string) error
- func (v *Var) Time(format ...string) time.Time
- func (v *Var) Uint() uint
- func (v *Var) Uint16() uint16
- func (v *Var) Uint32() uint32
- func (v *Var) Uint64() uint64
- func (v *Var) Uint64s() []uint64
- func (v *Var) Uint8() uint8
- func (v *Var) Uints() []uint
- func (v *Var) UnmarshalJSON(b []byte) error
- func (v *Var) UnmarshalValue(value any) error
- func (v *Var) Val() any
- func (v *Var) Vars() []*Var
- type Vars
- func (vs Vars) Bools() (s []bool)
- func (vs Vars) Float32s() (s []float32)
- func (vs Vars) Float64s() (s []float64)
- func (vs Vars) Int16s() (s []int16)
- func (vs Vars) Int32s() (s []int32)
- func (vs Vars) Int64s() (s []int64)
- func (vs Vars) Int8s() (s []int8)
- func (vs Vars) Interfaces() (s []any)
- func (vs Vars) Ints() (s []int)
- func (vs Vars) Scan(pointer any, mapping ...map[string]string) error
- func (vs Vars) Strings() (s []string)
- func (vs Vars) Uint16s() (s []uint16)
- func (vs Vars) Uint32s() (s []uint32)
- func (vs Vars) Uint64s() (s []uint64)
- func (vs Vars) Uint8s() (s []uint8)
- func (vs Vars) Uints() (s []uint)
Examples ¶
- New
- Var.Array
- Var.Bool
- Var.Bytes
- Var.Clone
- Var.Duration
- Var.Float32
- Var.Float32s
- Var.Float64s
- Var.Floats
- Var.GTime
- Var.Int
- Var.Int64s
- Var.Interface
- Var.Interfaces
- Var.Ints
- Var.IsEmpty
- Var.IsFloat
- Var.IsInt
- Var.IsMap
- Var.IsNil
- Var.IsSlice
- Var.IsStruct
- Var.IsUint
- Var.ListItemValues
- Var.ListItemValuesUnique
- Var.Map
- Var.MapDeep
- Var.MapStrAny
- Var.MapStrStr
- Var.MapStrStrDeep
- Var.MapStrVar
- Var.MapStrVarDeep
- Var.MapToMap
- Var.MapToMaps
- Var.MapToMapsDeep
- Var.Maps
- Var.MapsDeep
- Var.MarshalJSON
- Var.Scan
- Var.Set
- Var.Slice
- Var.String
- Var.Strings
- Var.Struct
- Var.Structs
- Var.Time
- Var.Uint
- Var.Uint64s
- Var.Uints
- Var.UnmarshalJSON
- Var.UnmarshalValue
- Var.Val
- Var.Vars
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Var ¶
type Var struct {
// contains filtered or unexported fields
}
Var is an universal variable type implementer.
func New ¶
New creates and returns a new Var with given `value`. The optional parameter `safe` specifies whether Var is used in concurrent-safety, which is false in default.
Example ¶
New
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gvar"
)
func main() {
v := gvar.New(400)
fmt.Println(v)
}
Output: 400
func (*Var) Array ¶
Array is alias of Interfaces.
Example ¶
Array
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gvar"
)
func main() {
var (
arr = []string{"GoFrame", "Golang"}
obj = gvar.New(arr)
)
fmt.Println(obj.Array())
}
Output: [GoFrame Golang]
func (*Var) Bool ¶
Bool converts and returns `v` as bool.
Example ¶
Bool
package main
import (
"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
var v = gvar.New(true)
g.DumpWithType(v.Bool())
}
Output: bool(true)
func (*Var) Bytes ¶
Bytes converts and returns `v` as []byte.
Example ¶
Bytes
package main
import (
"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
var v = gvar.New("GoFrame")
g.DumpWithType(v.Bytes())
}
Output: []byte(7) "GoFrame"
func (*Var) Clone ¶
Clone does a shallow copy of current Var and returns a pointer to this Var.
Example ¶
Clone
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
tmp := "fisrt hello"
v := gvar.New(tmp)
g.DumpWithType(v.Clone())
fmt.Println(v == v.Clone())
}
Output: *gvar.Var(11) "fisrt hello" false
func (*Var) Copy ¶ added in v2.1.0
Copy does a deep copy of current Var and returns a pointer to this Var.
func (*Var) Duration ¶
Duration converts and returns `v` as time.Duration. If value of `v` is string, then it uses time.ParseDuration for conversion.
Example ¶
Duration
package main
import (
"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
var v = gvar.New("300s")
g.DumpWithType(v.Duration())
}
Output: time.Duration(4) "5m0s"
func (*Var) Float32 ¶
Float32 converts and returns `v` as float32.
Example ¶
Float32
package main
import (
"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
var price = gvar.New(100.00)
g.DumpWithType(price.Float32())
}
Output: float32(100)
func (*Var) Float32s ¶
Float32s converts and returns `v` as []float32.
Example ¶
Float32s
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gvar"
)
func main() {
var (
arr = []float32{1, 2, 3, 4, 5}
obj = gvar.New(arr)
)
fmt.Println(obj.Float32s())
}
Output: [1 2 3 4 5]
func (*Var) Float64s ¶
Float64s converts and returns `v` as []float64.
Example ¶
Float64s
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gvar"
)
func main() {
var (
arr = []float64{1, 2, 3, 4, 5}
obj = gvar.New(arr)
)
fmt.Println(obj.Float64s())
}
Output: [1 2 3 4 5]
func (*Var) Floats ¶
Floats is alias of Float64s.
Example ¶
Floats
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gvar"
)
func main() {
var (
arr = []float64{1, 2, 3, 4, 5}
obj = gvar.New(arr)
)
fmt.Println(obj.Floats())
}
Output: [1 2 3 4 5]
func (*Var) GTime ¶
GTime converts and returns `v` as *gtime.Time. The parameter `format` specifies the format of the time string using gtime, eg: Y-m-d H:i:s.
Example ¶
GTime
package main
import (
"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
var v = gvar.New("2021-11-11 00:00:00")
g.DumpWithType(v.GTime())
}
Output: *gtime.Time(19) "2021-11-11 00:00:00"
func (*Var) Int ¶
Int converts and returns `v` as int.
Example ¶
Int
package main
import (
"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
var v = gvar.New(-1000)
g.DumpWithType(v.Int())
}
Output: int(-1000)
func (*Var) Int64s ¶
Int64s converts and returns `v` as []int64.
Example ¶
Int64s
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gvar"
)
func main() {
var (
arr = []int64{1, 2, 3, 4, 5}
obj = gvar.New(arr)
)
fmt.Println(obj.Int64s())
}
Output: [1 2 3 4 5]
func (*Var) Interface ¶
Interface is alias of Val.
Example ¶
Interface
package main
import (
"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
var v = gvar.New(100.00)
g.DumpWithType(v.Interface())
}
Output: float64(100)
func (*Var) Interfaces ¶
Interfaces converts and returns `v` as []interfaces{}.
Example ¶
Interfaces
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gvar"
)
func main() {
var (
arr = []string{"GoFrame", "Golang"}
obj = gvar.New(arr)
)
fmt.Println(obj.Interfaces())
}
Output: [GoFrame Golang]
func (*Var) Ints ¶
Ints converts and returns `v` as []int.
Example ¶
Ints
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gvar"
)
func main() {
var (
arr = []int{1, 2, 3, 4, 5}
obj = gvar.New(arr)
)
fmt.Println(obj.Ints())
}
Output: [1 2 3 4 5]
func (*Var) IsEmpty ¶
IsEmpty checks whether `v` is empty.
Example ¶
IsEmpty
package main
import (
"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
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
func (*Var) IsFloat ¶
IsFloat checks whether `v` is type of float.
Example ¶
IsFloat
package main
import (
"github.com/gogf/gf/v2/frame/g"
)
func main() {
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
func (*Var) IsInt ¶
IsInt checks whether `v` is type of int.
Example ¶
IsInt
package main
import (
"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
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
func (*Var) IsMap ¶
IsMap checks whether `v` is type of map.
Example ¶
IsMap
package main
import (
"github.com/gogf/gf/v2/frame/g"
)
func main() {
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
func (*Var) IsNil ¶
IsNil checks whether `v` is nil.
Example ¶
IsNil
package main
import (
"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
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
func (*Var) IsSlice ¶
IsSlice checks whether `v` is type of slice.
Example ¶
IsSlice
package main
import (
"github.com/gogf/gf/v2/frame/g"
)
func main() {
g.Dump(g.NewVar(0).IsSlice())
g.Dump(g.NewVar(g.Slice{0}).IsSlice())
}
Output: false true
func (*Var) IsStruct ¶
IsStruct checks whether `v` is type of struct.
Example ¶
IsStruct
package main
import (
"github.com/gogf/gf/v2/frame/g"
)
func main() {
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
func (*Var) IsUint ¶
IsUint checks whether `v` is type of uint.
Example ¶
IsUint
package main
import (
"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
g.Dump(gvar.New(0).IsUint())
g.Dump(gvar.New(uint8(8)).IsUint())
g.Dump(gvar.New(nil).IsUint())
}
Output: false true false
func (*Var) ListItemValues ¶
ListItemValues retrieves and returns the elements of all item struct/map with key `key`. Note that the parameter `list` should be type of slice which contains elements of map or struct, or else it returns an empty slice.
Example ¶
ListItemValues
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
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>]
func (*Var) ListItemValuesUnique ¶
ListItemValuesUnique retrieves and returns the unique elements of all struct/map with key `key`. Note that the parameter `list` should be type of slice which contains elements of map or struct, or else it returns an empty slice.
Example ¶
ListItemValuesUnique
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
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>]
func (*Var) Map ¶
Map converts and returns `v` as map[string]any.
Example ¶
Map
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
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
func (*Var) MapDeep ¶
MapDeep converts and returns `v` as map[string]any recursively. Deprecated: used Map instead.
Example ¶
MapDeep
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
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]
func (*Var) MapStrAny ¶
MapStrAny is like function Map, but implements the interface of MapStrAny.
Example ¶
MapStrAny
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
var (
m1 = g.Map{"id": 1, "price": 100}
v = gvar.New(m1)
v2 = v.MapStrAny()
)
fmt.Println(v2["price"], v2["id"])
}
Output: 100 1
func (*Var) MapStrStr ¶
MapStrStr converts and returns `v` as map[string]string.
Example ¶
MapStrStr
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
var (
m1 = g.Map{"id": 1, "price": 100}
v = gvar.New(m1)
v2 = v.MapStrStr()
)
fmt.Println(v2["price"] + "$")
}
Output: 100$
func (*Var) MapStrStrDeep ¶
MapStrStrDeep converts and returns `v` as map[string]string recursively. Deprecated: used MapStrStr instead.
Example ¶
MapStrStrDeep
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
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}
func (*Var) MapStrVar ¶
MapStrVar converts and returns `v` as map[string]Var.
Example ¶
MapStrVar
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
var (
m1 = g.Map{"id": 1, "price": 100}
v = gvar.New(m1)
v2 = v.MapStrVar()
)
fmt.Println(v2["price"].Float64() * 100)
}
Output: 10000
func (*Var) MapStrVarDeep ¶
MapStrVarDeep converts and returns `v` as map[string]*Var recursively. Deprecated: used MapStrVar instead.
Example ¶
MapStrVarDeep
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
var (
m1 = g.Map{"id": 1, "price": 100}
m2 = g.Map{"product": m1}
m3 = g.Map{}
v = gvar.New(m2)
v2 = v.MapStrVarDeep()
v3 = gvar.New(m3).MapStrVarDeep()
)
fmt.Println(v2["product"])
fmt.Println(v3)
}
Output: {"id":1,"price":100} map[]
func (*Var) MapToMap ¶
MapToMap converts any map type variable `params` to another map type variable `pointer`. See gconv.MapToMap.
Example ¶
MapToMap
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
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"}
func (*Var) MapToMaps ¶
MapToMaps converts any map type variable `params` to another map type variable `pointer`. See gconv.MapToMaps.
Example ¶
MapToMaps
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
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}"}}
func (*Var) MapToMapsDeep ¶
MapToMapsDeep converts any map type variable `params` to another map type variable `pointer` recursively. See gconv.MapToMapsDeep.
Example ¶
MapToMapsDeep
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
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.MapToMapsDeep(&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}"}}
func (*Var) Maps ¶
Maps converts and returns `v` as map[string]string. See gconv.Maps.
Example ¶
Maps
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
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}}
func (*Var) MapsDeep ¶
MapsDeep converts `value` to []map[string]any recursively. Deprecated: used Maps instead.
Example ¶
MapsDeep
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
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}}}
func (*Var) MarshalJSON ¶
MarshalJSON implements the interface MarshalJSON for json.Marshal.
Example ¶
MarshalJSON
package main
import (
"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/internal/json"
)
func main() {
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"}"
func (*Var) Scan ¶
Scan automatically checks the type of `pointer` and converts value of Var to `pointer`.
See gconv.Scan.
Example ¶
Scan
package main
import (
"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
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},
}
)
v := gvar.New(m)
if err := v.Scan(&s); err == nil {
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]", }
func (*Var) Set ¶
Set sets `value` to `v`, and returns the old value.
Example ¶
Set
package main
import (
"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
var v = gvar.New(100.00)
g.Dump(v.Set(200.00))
g.Dump(v)
}
Output: 100 "200"
func (*Var) Slice ¶
Slice is alias of Interfaces.
Example ¶
Slice
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gvar"
)
func main() {
var (
arr = []string{"GoFrame", "Golang"}
obj = gvar.New(arr)
)
fmt.Println(obj.Slice())
}
Output: [GoFrame Golang]
func (*Var) String ¶
String converts and returns `v` as string.
Example ¶
String
package main
import (
"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
var v = gvar.New("GoFrame")
g.DumpWithType(v.String())
}
Output: string(7) "GoFrame"
func (*Var) Strings ¶
Strings converts and returns `v` as []string.
Example ¶
Strings
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gvar"
)
func main() {
var (
arr = []string{"GoFrame", "Golang"}
obj = gvar.New(arr)
)
fmt.Println(obj.Strings())
}
Output: [GoFrame Golang]
func (*Var) Struct ¶
Struct maps value of `v` to `pointer`. The parameter `pointer` should be a pointer to a struct instance. The parameter `mapping` is used to specify the key-to-attribute mapping rules.
Example ¶
package main
import (
"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
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", }
func (*Var) Structs ¶
Structs converts and returns `v` as given struct slice.
Example ¶
package main
import (
"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
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", }, ]
func (*Var) Time ¶
Time converts and returns `v` as time.Time. The parameter `format` specifies the format of the time string using gtime, eg: Y-m-d H:i:s.
Example ¶
Time
package main
import (
"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
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"
func (*Var) Uint ¶
Uint converts and returns `v` as uint.
Example ¶
Uint
package main
import (
"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
var v = gvar.New(1000)
g.DumpWithType(v.Uint())
}
Output: uint(1000)
func (*Var) Uint64s ¶
Uint64s converts and returns `v` as []uint64.
Example ¶
Uint64s
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gvar"
)
func main() {
var (
arr = []uint64{1, 2, 3, 4, 5}
obj = gvar.New(arr)
)
fmt.Println(obj.Uint64s())
}
Output: [1 2 3 4 5]
func (*Var) Uints ¶
Uints converts and returns `v` as []uint.
Example ¶
Uints
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gvar"
)
func main() {
var (
arr = []uint{1, 2, 3, 4, 5}
obj = gvar.New(arr)
)
fmt.Println(obj.Uints())
}
Output: [1 2 3 4 5]
func (*Var) UnmarshalJSON ¶
UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
Example ¶
UnmarshalJSON
package main
import (
"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/internal/json"
)
func main() {
tmp := []byte(`{
"Code": "0003",
"Name": "Golang Book3",
"Quantity": 3000,
"Price": 300,
"OnSale": true
}`)
var v = gvar.New(map[string]any{})
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}"
func (*Var) UnmarshalValue ¶
UnmarshalValue is an interface implement which sets any type of value for Var.
Example ¶
UnmarshalValue
package main
import (
"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
tmp := g.Map{
"code": "00002",
"name": "GoFrame",
"price": 100,
"sale": true,
}
var v = gvar.New(map[string]any{})
if err := v.UnmarshalValue(tmp); err != nil {
panic(err)
}
g.Dump(v)
}
Output: "{\"code\":\"00002\",\"name\":\"GoFrame\",\"price\":100,\"sale\":true}"
type Vars ¶ added in v2.3.0
type Vars []*Var
Vars is a slice of *Var.
func (Vars) Interfaces ¶ added in v2.3.0
Interfaces converts and returns `vs` as []any.