基本使用

    1. package main
    2. import (
    3. "github.com/gogf/gf/v2/container/gtype"
    4. "fmt"
    5. )
    6. func main() {
    7. // 创建一个Int型的并发安全基本类型对象
    8. i := gtype.NewInt()
    9. // 设置值
    10. fmt.Println(i.Set(10))
    11. // 获取值
    12. fmt.Println(i.Val())
    13. // 数值-1,并返回修改之后的数值
    14. fmt.Println(i.Add(-1))

    执行后,输出结果为:

    序列化/反序列

    1、Marshal

    1. package main
    2. import (
    3. "encoding/json"
    4. "fmt"
    5. "github.com/gogf/gf/v2/container/gtype"
    6. )
    7. func main() {
    8. type Student struct {
    9. Id *gtype.Int
    10. Name *gtype.String
    11. Scores *gtype.Interface
    12. }
    13. s := Student{
    14. Id: gtype.NewInt(1),
    15. Name: gtype.NewString("john"),
    16. Scores: gtype.NewInterface([]int{100, 99, 98}),
    17. }
    18. }

    2、Unmarshal

    1. package main
    2. import (
    3. "encoding/json"
    4. "fmt"
    5. "github.com/gogf/gf/v2/container/gtype"
    6. )
    7. func main() {
    8. b := []byte(`{"Id":1,"Name":"john","Scores":[100,99,98]}`)
    9. type Student struct {
    10. Id *gtype.Int
    11. Name *gtype.String
    12. Scores *gtype.Interface
    13. }
    14. s := Student{}
    15. json.Unmarshal(b, &s)
    16. }