gvar

package
Version: v2.1.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 26, 2022 License: MIT Imports: 8 Imported by: 44

Documentation

Overview

Package gvar provides an universal variable type, like generics.

Index

Examples

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

func New(value interface{}, safe ...bool) *Var

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.

func (*Var) Array

func (v *Var) Array() []interface{}

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

func (v *Var) Bool() 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

func (v *Var) Bytes() []byte

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

func (v *Var) Clone() *Var

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

func (v *Var) Copy() *Var

Copy does a deep copy of current Var and returns a pointer to this Var.

func (*Var) DeepCopy added in v2.1.0

func (v *Var) DeepCopy() interface{}

DeepCopy implements interface for deep copy of current type.

func (*Var) Duration

func (v *Var) Duration() time.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

func (v *Var) Float32() 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

func (v *Var) Float32s() []float32

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) Float64

func (v *Var) Float64() float64

Float64 converts and returns `v` as float64.

func (*Var) Float64s

func (v *Var) Float64s() []float64

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

func (v *Var) Floats() []float64

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

func (v *Var) GTime(format ...string) *gtime.Time

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

func (v *Var) Int() 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) Int16

func (v *Var) Int16() int16

Int16 converts and returns `v` as int16.

func (*Var) Int32

func (v *Var) Int32() int32

Int32 converts and returns `v` as int32.

func (*Var) Int64

func (v *Var) Int64() int64

Int64 converts and returns `v` as int64.

func (*Var) Int64s

func (v *Var) Int64s() []int64

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) Int8

func (v *Var) Int8() int8

Int8 converts and returns `v` as int8.

func (*Var) Interface

func (v *Var) Interface() 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

func (v *Var) Interfaces() []interface{}

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

func (v *Var) Ints() []int

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

func (v *Var) IsEmpty() bool

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

func (v *Var) IsFloat() bool

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

func (v *Var) IsInt() bool

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

func (v *Var) IsMap() bool

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

func (v *Var) IsNil() bool

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

func (v *Var) IsSlice() bool

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

func (v *Var) IsStruct() bool

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

func (v *Var) IsUint() bool

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

func (v *Var) ListItemValues(key interface{}) (values []interface{})

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

func (v *Var) ListItemValuesUnique(key string) []interface{}

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

func (v *Var) Map(tags ...string) map[string]interface{}

Map converts and returns `v` as map[string]interface{}.

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

func (v *Var) MapDeep(tags ...string) map[string]interface{}

MapDeep converts and returns `v` as map[string]interface{} recursively.

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

func (v *Var) MapStrAny() map[string]interface{}

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

func (v *Var) MapStrStr(tags ...string) map[string]string

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

func (v *Var) MapStrStrDeep(tags ...string) map[string]string

MapStrStrDeep converts and returns `v` as map[string]string recursively.

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

func (v *Var) MapStrVar(tags ...string) map[string]*Var

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

func (v *Var) MapStrVarDeep(tags ...string) map[string]*Var

MapStrVarDeep converts and returns `v` as map[string]*Var recursively.

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

func (v *Var) MapToMap(pointer interface{}, mapping ...map[string]string) (err error)

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

func (v *Var) MapToMaps(pointer interface{}, mapping ...map[string]string) (err error)

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

func (v *Var) MapToMapsDeep(pointer interface{}, mapping ...map[string]string) (err error)

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

func (v *Var) Maps(tags ...string) []map[string]interface{}

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

func (v *Var) MapsDeep(tags ...string) []map[string]interface{}

MapsDeep converts `value` to []map[string]interface{} recursively. See gconv.MapsDeep.

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

func (v Var) MarshalJSON() ([]byte, error)

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

func (v *Var) Scan(pointer interface{}, mapping ...map[string]string) error

Scan automatically checks the type of `pointer` and converts `params` to `pointer`. It supports `pointer` with type of `*map/*[]map/*[]*map/*struct/**struct/*[]struct/*[]*struct` for converting.

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

func (v *Var) Set(value interface{}) (old interface{})

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

func (v *Var) Slice() []interface{}

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

func (v *Var) String() 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

func (v *Var) Strings() []string

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

func (v *Var) Struct(pointer interface{}, mapping ...map[string]string) error

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

func (v *Var) Structs(pointer interface{}, mapping ...map[string]string) error

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

func (v *Var) Time(format ...string) time.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

func (v *Var) Uint() 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) Uint16

func (v *Var) Uint16() uint16

Uint16 converts and returns `v` as uint16.

func (*Var) Uint32

func (v *Var) Uint32() uint32

Uint32 converts and returns `v` as uint32.

func (*Var) Uint64

func (v *Var) Uint64() uint64

Uint64 converts and returns `v` as uint64.

func (*Var) Uint64s

func (v *Var) Uint64s() []uint64

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) Uint8

func (v *Var) Uint8() uint8

Uint8 converts and returns `v` as uint8.

func (*Var) Uints

func (v *Var) Uints() []uint

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

func (v *Var) UnmarshalJSON(b []byte) error

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]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}"

func (*Var) UnmarshalValue

func (v *Var) UnmarshalValue(value interface{}) error

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]interface{}{})
	if err := v.UnmarshalValue(tmp); err != nil {
		panic(err)
	}
	g.Dump(v)

}
Output:

"{\"code\":\"00002\",\"name\":\"GoFrame\",\"price\":100,\"sale\":true}"

func (*Var) Val

func (v *Var) Val() interface{}

Val returns the current value of `v`.

Example

Val

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.Val())

}
Output:

float64(100)

func (*Var) Vars

func (v *Var) Vars() []*Var

Vars converts and returns `v` as []Var.

Example

Vars

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.Vars())

}
Output:

[GoFrame Golang]

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL