Files
gf/container/gvar/gvar_z_example_test.go

770 lines
12 KiB
Go
Raw Permalink Normal View History

2021-11-17 17:02:49 +08:00
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
2021-11-26 10:25:25 +08:00
2021-11-17 17:02:49 +08:00
package gvar_test
import (
"fmt"
2021-11-17 17:02:49 +08:00
"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/internal/json"
)
// New
func ExampleNew() {
2021-11-17 17:02:49 +08:00
v := gvar.New(400)
2022-03-10 22:29:47 +08:00
fmt.Println(v)
2021-11-17 17:02:49 +08:00
// Output:
2022-03-10 22:29:47 +08:00
// 400
2021-11-17 17:02:49 +08:00
}
// 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
func ExampleVar_Set() {
var v = gvar.New(100.00)
g.Dump(v.Set(200.00))
g.Dump(v)
// Output:
// 100
// "200"
}
// Val
func ExampleVar_Val() {
var v = gvar.New(100.00)
g.DumpWithType(v.Val())
// Output:
// float64(100)
}
// Interface
func ExampleVar_Interface() {
var v = gvar.New(100.00)
g.DumpWithType(v.Interface())
// Output:
// float64(100)
}
// Bytes
func ExampleVar_Bytes() {
var v = gvar.New("GoFrame")
g.DumpWithType(v.Bytes())
// Output:
// []byte(7) "GoFrame"
}
// String
func ExampleVar_String() {
var v = gvar.New("GoFrame")
g.DumpWithType(v.String())
// Output:
// string(7) "GoFrame"
}
// Bool
func ExampleVar_Bool() {
var v = gvar.New(true)
g.DumpWithType(v.Bool())
// Output:
// bool(true)
}
// Int
func ExampleVar_Int() {
var v = gvar.New(-1000)
g.DumpWithType(v.Int())
// Output:
// int(-1000)
}
// Uint
func ExampleVar_Uint() {
var v = gvar.New(1000)
g.DumpWithType(v.Uint())
// Output:
// uint(1000)
}
// Float32
func ExampleVar_Float32() {
var price = gvar.New(100.00)
g.DumpWithType(price.Float32())
// Output:
// float32(100)
}
// 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
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
func ExampleVar_Duration() {
var v = gvar.New("300s")
g.DumpWithType(v.Duration())
// Output:
// time.Duration(4) "5m0s"
}
// 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
func ExampleVar_UnmarshalJSON() {
tmp := []byte(`{
"Code": "0003",
"Name": "Golang Book3",
"Quantity": 3000,
"Price": 300,
"OnSale": true
}`)
refactor: interface{} to any and reflect.Ptr to reflect.Pointer (#4395) This pull request standardizes the use of the Go 1.18+ `any` type alias instead of `interface{}` throughout the codebase. The change improves code readability and aligns with modern Go best practices. The update touches many files, including core data structures, code generation templates, logging utilities, and test data, ensuring consistency across all usages. **Type alias migration to `any`:** * Replaced all instances of `interface{}` with `any` in core data structures such as `garray` and in generated model structs (e.g., `TableUser`, `User1`, `User2`) to modernize type usage. [[1]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L31-R31) [[2]](diffhunk://#diff-6c19859cb32c7516ea95ddc8f8235460818eb2f24d2204308e0d9e1b19e7d90fL15-R19) [[3]](diffhunk://#diff-a15ba2f5e830b4833c47b902515a4f9e5a4f83a3707698f3229b307ec3776b41L15-R18) [[4]](diffhunk://#diff-52e0837e84d49221d1b810d88fdf78221f36cffcd664fb42f8aba49a79b974dcL15-R19) [[5]](diffhunk://#diff-11c3457d1a23a4ca6ecd00d6b856289774936b6a708384cf03aff164044e7546L15-R19) [[6]](diffhunk://#diff-2cff9cf8e6a0cc34087326d8c8149c3bbaf74c76fdbdf5a73daed13cc04249e1L15-R19) * Updated function signatures, method parameters, and return types from `interface{}` to `any` in various parts of the codebase, including code generation, service logic, and logging utilities (e.g., `mlog`). [[1]](diffhunk://#diff-175edfeea54490b8fe4e18ffcbea5835efaf8f0b8acf623359073987cae7eb76L48-R55) [[2]](diffhunk://#diff-2b1953fb78cf3593d8c2c7d911e95b65fd0b847c30ed0b4d167d16fe6d781235L54-R74) [[3]](diffhunk://#diff-e001b7a4b63603b9b14f00de78a4d570bb76c5f57d856a24643f071032e12356L66-R73) [[4]](diffhunk://#diff-5582954e8a9983988dc8854ad82067fb2ac6269b988e07357ad8db1dfec5f1a0L39-R41) [[5]](diffhunk://#diff-c5d51d56f487779a2b6207c7ad26c7a20bbadcc846ce094fe60ab4cabff58c51L107-R107) [[6]](diffhunk://#diff-f96e6a9fdb416eb1804ceaba1fe0ac637bff22c43837f8bb849c2366ce72d4a1L116-R121) [[7]](diffhunk://#diff-f94c83a1b08ae060d9346f4a6031fc4a7b9a0b894e02d9afaa09018b6598eac0L112-R112) [[8]](diffhunk://#diff-748b11dbe8828dd4c040ec23cae0b8fe57ecf0a2d1b7694ea39102294e633c64L36-R36) [[9]](diffhunk://#diff-748b11dbe8828dd4c040ec23cae0b8fe57ecf0a2d1b7694ea39102294e633c64L74-R74) [[10]](diffhunk://#diff-748b11dbe8828dd4c040ec23cae0b8fe57ecf0a2d1b7694ea39102294e633c64L96-R96) **Generated code and templates:** * Adjusted generated files and code generation templates to output `any` instead of `interface{}` for relevant struct fields and function signatures, ensuring that new code generation aligns with the updated convention. [[1]](diffhunk://#diff-6c19859cb32c7516ea95ddc8f8235460818eb2f24d2204308e0d9e1b19e7d90fL15-R19) [[2]](diffhunk://#diff-a15ba2f5e830b4833c47b902515a4f9e5a4f83a3707698f3229b307ec3776b41L15-R18) [[3]](diffhunk://#diff-52e0837e84d49221d1b810d88fdf78221f36cffcd664fb42f8aba49a79b974dcL15-R19) [[4]](diffhunk://#diff-11c3457d1a23a4ca6ecd00d6b856289774936b6a708384cf03aff164044e7546L15-R19) [[5]](diffhunk://#diff-2cff9cf8e6a0cc34087326d8c8149c3bbaf74c76fdbdf5a73daed13cc04249e1L15-R19) [[6]](diffhunk://#diff-175edfeea54490b8fe4e18ffcbea5835efaf8f0b8acf623359073987cae7eb76L48-R55) [[7]](diffhunk://#diff-e001b7a4b63603b9b14f00de78a4d570bb76c5f57d856a24643f071032e12356L66-R73) [[8]](diffhunk://#diff-5582954e8a9983988dc8854ad82067fb2ac6269b988e07357ad8db1dfec5f1a0L39-R41) **Container and utility updates:** * Refactored the `garray` container implementation and related constructors/methods to use `[]any` instead of `[]interface{}`, along with corresponding function signatures. [[1]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L31-R31) [[2]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L52-R52) [[3]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L62-R62) [[4]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L73-R86) [[5]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L96-R97) [[6]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L107-R114) [[7]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L124-R124) [[8]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L135-R143) [[9]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L167-R167) These changes collectively modernize the codebase and prepare it for future Go developments by using the idiomatic `any` type.
2025-08-28 16:53:19 +08:00
var v = gvar.New(map[string]any{})
2021-11-17 17:02:49 +08:00
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
func ExampleVar_UnmarshalValue() {
tmp := g.Map{
"code": "00002",
"name": "GoFrame",
"price": 100,
"sale": true,
}
refactor: interface{} to any and reflect.Ptr to reflect.Pointer (#4395) This pull request standardizes the use of the Go 1.18+ `any` type alias instead of `interface{}` throughout the codebase. The change improves code readability and aligns with modern Go best practices. The update touches many files, including core data structures, code generation templates, logging utilities, and test data, ensuring consistency across all usages. **Type alias migration to `any`:** * Replaced all instances of `interface{}` with `any` in core data structures such as `garray` and in generated model structs (e.g., `TableUser`, `User1`, `User2`) to modernize type usage. [[1]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L31-R31) [[2]](diffhunk://#diff-6c19859cb32c7516ea95ddc8f8235460818eb2f24d2204308e0d9e1b19e7d90fL15-R19) [[3]](diffhunk://#diff-a15ba2f5e830b4833c47b902515a4f9e5a4f83a3707698f3229b307ec3776b41L15-R18) [[4]](diffhunk://#diff-52e0837e84d49221d1b810d88fdf78221f36cffcd664fb42f8aba49a79b974dcL15-R19) [[5]](diffhunk://#diff-11c3457d1a23a4ca6ecd00d6b856289774936b6a708384cf03aff164044e7546L15-R19) [[6]](diffhunk://#diff-2cff9cf8e6a0cc34087326d8c8149c3bbaf74c76fdbdf5a73daed13cc04249e1L15-R19) * Updated function signatures, method parameters, and return types from `interface{}` to `any` in various parts of the codebase, including code generation, service logic, and logging utilities (e.g., `mlog`). [[1]](diffhunk://#diff-175edfeea54490b8fe4e18ffcbea5835efaf8f0b8acf623359073987cae7eb76L48-R55) [[2]](diffhunk://#diff-2b1953fb78cf3593d8c2c7d911e95b65fd0b847c30ed0b4d167d16fe6d781235L54-R74) [[3]](diffhunk://#diff-e001b7a4b63603b9b14f00de78a4d570bb76c5f57d856a24643f071032e12356L66-R73) [[4]](diffhunk://#diff-5582954e8a9983988dc8854ad82067fb2ac6269b988e07357ad8db1dfec5f1a0L39-R41) [[5]](diffhunk://#diff-c5d51d56f487779a2b6207c7ad26c7a20bbadcc846ce094fe60ab4cabff58c51L107-R107) [[6]](diffhunk://#diff-f96e6a9fdb416eb1804ceaba1fe0ac637bff22c43837f8bb849c2366ce72d4a1L116-R121) [[7]](diffhunk://#diff-f94c83a1b08ae060d9346f4a6031fc4a7b9a0b894e02d9afaa09018b6598eac0L112-R112) [[8]](diffhunk://#diff-748b11dbe8828dd4c040ec23cae0b8fe57ecf0a2d1b7694ea39102294e633c64L36-R36) [[9]](diffhunk://#diff-748b11dbe8828dd4c040ec23cae0b8fe57ecf0a2d1b7694ea39102294e633c64L74-R74) [[10]](diffhunk://#diff-748b11dbe8828dd4c040ec23cae0b8fe57ecf0a2d1b7694ea39102294e633c64L96-R96) **Generated code and templates:** * Adjusted generated files and code generation templates to output `any` instead of `interface{}` for relevant struct fields and function signatures, ensuring that new code generation aligns with the updated convention. [[1]](diffhunk://#diff-6c19859cb32c7516ea95ddc8f8235460818eb2f24d2204308e0d9e1b19e7d90fL15-R19) [[2]](diffhunk://#diff-a15ba2f5e830b4833c47b902515a4f9e5a4f83a3707698f3229b307ec3776b41L15-R18) [[3]](diffhunk://#diff-52e0837e84d49221d1b810d88fdf78221f36cffcd664fb42f8aba49a79b974dcL15-R19) [[4]](diffhunk://#diff-11c3457d1a23a4ca6ecd00d6b856289774936b6a708384cf03aff164044e7546L15-R19) [[5]](diffhunk://#diff-2cff9cf8e6a0cc34087326d8c8149c3bbaf74c76fdbdf5a73daed13cc04249e1L15-R19) [[6]](diffhunk://#diff-175edfeea54490b8fe4e18ffcbea5835efaf8f0b8acf623359073987cae7eb76L48-R55) [[7]](diffhunk://#diff-e001b7a4b63603b9b14f00de78a4d570bb76c5f57d856a24643f071032e12356L66-R73) [[8]](diffhunk://#diff-5582954e8a9983988dc8854ad82067fb2ac6269b988e07357ad8db1dfec5f1a0L39-R41) **Container and utility updates:** * Refactored the `garray` container implementation and related constructors/methods to use `[]any` instead of `[]interface{}`, along with corresponding function signatures. [[1]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L31-R31) [[2]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L52-R52) [[3]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L62-R62) [[4]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L73-R86) [[5]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L96-R97) [[6]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L107-R114) [[7]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L124-R124) [[8]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L135-R143) [[9]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L167-R167) These changes collectively modernize the codebase and prepare it for future Go developments by using the idiomatic `any` type.
2025-08-28 16:53:19 +08:00
var v = gvar.New(map[string]any{})
2021-11-17 17:02:49 +08:00
if err := v.UnmarshalValue(tmp); err != nil {
panic(err)
}
g.Dump(v)
// Output:
// "{\"code\":\"00002\",\"name\":\"GoFrame\",\"price\":100,\"sale\":true}"
}
// 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
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
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
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
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
func ExampleVar_IsSlice() {
g.Dump(g.NewVar(0).IsSlice())
g.Dump(g.NewVar(g.Slice{0}).IsSlice())
// Output:
// false
// true
}
// 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
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
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
func ExampleVar_ListItemValuesUnique() {
2021-11-25 16:13:12 +08:00
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)
)
2021-11-17 17:02:49 +08:00
fmt.Println(v.ListItemValuesUnique("id"))
fmt.Println(v.ListItemValuesUnique("price"))
// Output:
// [1 2 3]
// [100 <nil>]
}
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",
// }
}
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
func ExampleVar_Ints() {
2021-11-25 16:13:12 +08:00
var (
arr = []int{1, 2, 3, 4, 5}
obj = gvar.New(arr)
)
2021-11-17 17:02:49 +08:00
fmt.Println(obj.Ints())
// Output:
// [1 2 3 4 5]
}
// Int64s
func ExampleVar_Int64s() {
2021-11-25 16:13:12 +08:00
var (
arr = []int64{1, 2, 3, 4, 5}
obj = gvar.New(arr)
)
2021-11-17 17:02:49 +08:00
fmt.Println(obj.Int64s())
// Output:
// [1 2 3 4 5]
}
// Uints
func ExampleVar_Uints() {
2021-11-25 16:13:12 +08:00
var (
arr = []uint{1, 2, 3, 4, 5}
obj = gvar.New(arr)
)
2021-11-17 17:02:49 +08:00
fmt.Println(obj.Uints())
// Output:
// [1 2 3 4 5]
}
// Uint64s
func ExampleVar_Uint64s() {
2021-11-25 16:13:12 +08:00
var (
arr = []uint64{1, 2, 3, 4, 5}
obj = gvar.New(arr)
)
2021-11-17 17:02:49 +08:00
fmt.Println(obj.Uint64s())
// Output:
// [1 2 3 4 5]
}
// Floats
func ExampleVar_Floats() {
2021-11-25 16:13:12 +08:00
var (
arr = []float64{1, 2, 3, 4, 5}
obj = gvar.New(arr)
)
2021-11-17 17:02:49 +08:00
fmt.Println(obj.Floats())
// Output:
// [1 2 3 4 5]
}
// Float32s
func ExampleVar_Float32s() {
2021-11-25 16:13:12 +08:00
var (
arr = []float32{1, 2, 3, 4, 5}
obj = gvar.New(arr)
)
2021-11-17 17:02:49 +08:00
fmt.Println(obj.Float32s())
// Output:
// [1 2 3 4 5]
}
// Float64s
func ExampleVar_Float64s() {
2021-11-25 16:13:12 +08:00
var (
arr = []float64{1, 2, 3, 4, 5}
obj = gvar.New(arr)
)
2021-11-17 17:02:49 +08:00
fmt.Println(obj.Float64s())
// Output:
// [1 2 3 4 5]
}
// Strings
func ExampleVar_Strings() {
2021-11-25 16:13:12 +08:00
var (
arr = []string{"GoFrame", "Golang"}
obj = gvar.New(arr)
)
2021-11-17 17:02:49 +08:00
fmt.Println(obj.Strings())
// Output:
// [GoFrame Golang]
}
// Interfaces
func ExampleVar_Interfaces() {
2021-11-25 16:13:12 +08:00
var (
arr = []string{"GoFrame", "Golang"}
obj = gvar.New(arr)
)
2021-11-17 17:02:49 +08:00
fmt.Println(obj.Interfaces())
// Output:
// [GoFrame Golang]
}
// Slice
func ExampleVar_Slice() {
2021-11-25 16:13:12 +08:00
var (
arr = []string{"GoFrame", "Golang"}
obj = gvar.New(arr)
)
2021-11-17 17:02:49 +08:00
fmt.Println(obj.Slice())
// Output:
// [GoFrame Golang]
}
// Array
func ExampleVar_Array() {
2021-11-25 16:13:12 +08:00
var (
arr = []string{"GoFrame", "Golang"}
obj = gvar.New(arr)
)
2021-11-17 17:02:49 +08:00
fmt.Println(obj.Array())
// Output:
// [GoFrame Golang]
}
// Vars
func ExampleVar_Vars() {
2021-11-25 16:13:12 +08:00
var (
arr = []string{"GoFrame", "Golang"}
obj = gvar.New(arr)
)
2021-11-17 17:02:49 +08:00
fmt.Println(obj.Vars())
// Output:
// [GoFrame Golang]
}
// Map
func ExampleVar_Map() {
2021-11-25 16:13:12 +08:00
var (
m = g.Map{"id": 1, "price": 100.00}
v = gvar.New(m)
res = v.Map()
)
fmt.Println(res["id"], res["price"])
2021-11-17 17:02:49 +08:00
// Output:
// 1 100
}
// MapStrAny
func ExampleVar_MapStrAny() {
2021-11-25 16:13:12 +08:00
var (
m1 = g.Map{"id": 1, "price": 100}
v = gvar.New(m1)
v2 = v.MapStrAny()
)
2021-11-17 17:02:49 +08:00
fmt.Println(v2["price"], v2["id"])
// Output:
// 100 1
}
// MapStrStr
func ExampleVar_MapStrStr() {
2021-11-25 16:13:12 +08:00
var (
m1 = g.Map{"id": 1, "price": 100}
v = gvar.New(m1)
v2 = v.MapStrStr()
)
2021-11-17 17:02:49 +08:00
fmt.Println(v2["price"] + "$")
// Output:
// 100$
}
// MapStrVar
func ExampleVar_MapStrVar() {
2021-11-25 16:13:12 +08:00
var (
m1 = g.Map{"id": 1, "price": 100}
v = gvar.New(m1)
v2 = v.MapStrVar()
)
2021-11-17 17:02:49 +08:00
fmt.Println(v2["price"].Float64() * 100)
// Output:
// 10000
}
// MapDeep
func ExampleVar_MapDeep() {
2021-11-25 16:13:12 +08:00
var (
m1 = g.Map{"id": 1, "price": 100}
m2 = g.Map{"product": m1}
v = gvar.New(m2)
v2 = v.MapDeep()
)
2021-11-17 17:02:49 +08:00
fmt.Println(v2["product"])
// Output:
// map[id:1 price:100]
}
// MapStrStrDeep
func ExampleVar_MapStrStrDeep() {
2021-11-25 16:13:12 +08:00
var (
m1 = g.Map{"id": 1, "price": 100}
m2 = g.Map{"product": m1}
v = gvar.New(m2)
v2 = v.MapStrStrDeep()
)
2021-11-17 17:02:49 +08:00
fmt.Println(v2["product"])
// Output:
// {"id":1,"price":100}
}
// MapStrVarDeep
func ExampleVar_MapStrVarDeep() {
2021-11-25 16:13:12 +08:00
var (
m1 = g.Map{"id": 1, "price": 100}
m2 = g.Map{"product": m1}
m3 = g.Map{}
2021-11-25 16:13:12 +08:00
v = gvar.New(m2)
v2 = v.MapStrVarDeep()
v3 = gvar.New(m3).MapStrVarDeep()
2021-11-25 16:13:12 +08:00
)
2021-11-17 17:02:49 +08:00
fmt.Println(v2["product"])
fmt.Println(v3)
2021-11-17 17:02:49 +08:00
// Output:
// {"id":1,"price":100}
// map[]
2021-11-17 17:02:49 +08:00
}
// 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
func ExampleVar_MapsDeep() {
2021-11-25 16:13:12 +08:00
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()
)
2021-11-17 17:02:49 +08:00
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
func ExampleVar_MapToMap() {
2021-11-25 16:13:12 +08:00
var (
m1 = gvar.New(g.MapIntInt{0: 100, 1: 200})
m2 = g.MapStrStr{}
)
2021-11-17 17:02:49 +08:00
err := m1.MapToMap(&m2)
if err != nil {
panic(err)
}
fmt.Printf("%#v", m2)
// Output:
// map[string]string{"0":"100", "1":"200"}
}
// MapToMaps
func ExampleVar_MapToMaps() {
2021-11-25 16:13:12 +08:00
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
)
2021-11-17 17:02:49 +08:00
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
func ExampleVar_MapToMapsDeep() {
2021-11-25 16:13:12 +08:00
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
2021-11-25 16:13:12 +08:00
)
err := v.MapToMapsDeep(&v2)
2021-11-17 17:02:49 +08:00
if err != nil {
panic(err)
}
fmt.Printf("%#v", v2)
2021-11-17 17:02:49 +08:00
// Output:
// []map[string]string{map[string]string{"product":"{\"id\":1,\"price\":100}"}, map[string]string{"product":"{\"id\":2,\"price\":200}"}}
2021-11-17 17:02:49 +08:00
}
// 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},
}
)
v := gvar.New(m)
if err := v.Scan(&s); err == nil {
g.DumpWithType(s)
2021-11-17 17:02:49 +08:00
}
// Output:
// gvar_test.Student(3) {
// Id: *gvar.Var(1) "1",
// Name: *gvar.Var(4) "john",
// Scores: *gvar.Var(11) "[100,99,98]",
// }
}