基本使用
package main
import (
"github.com/gogf/gf/v2/container/gtype"
"fmt"
)
func main() {
// 创建一个Int型的并发安全基本类型对象
i := gtype.NewInt()
// 设置值
fmt.Println(i.Set(10))
// 获取值
fmt.Println(i.Val())
// 数值-1,并返回修改之后的数值
fmt.Println(i.Add(-1))
执行后,输出结果为:
序列化/反序列
1、Marshal
package main
import (
"encoding/json"
"fmt"
"github.com/gogf/gf/v2/container/gtype"
)
func main() {
type Student struct {
Id *gtype.Int
Name *gtype.String
Scores *gtype.Interface
}
s := Student{
Id: gtype.NewInt(1),
Name: gtype.NewString("john"),
Scores: gtype.NewInterface([]int{100, 99, 98}),
}
}
2、Unmarshal
package main
import (
"encoding/json"
"fmt"
"github.com/gogf/gf/v2/container/gtype"
)
func main() {
b := []byte(`{"Id":1,"Name":"john","Scores":[100,99,98]}`)
type Student struct {
Id *gtype.Int
Name *gtype.String
Scores *gtype.Interface
}
s := Student{}
json.Unmarshal(b, &s)
}