掃二維碼與項(xiàng)目經(jīng)理溝通
我們?cè)谖⑿派?4小時(shí)期待你的聲音
解答本文疑問(wèn)/技術(shù)咨詢(xún)/運(yùn)營(yíng)咨詢(xún)/技術(shù)建議/互聯(lián)網(wǎng)交流
?gtype?并發(fā)安全基本類(lèi)型的使用非常簡(jiǎn)單,往往就類(lèi)似以下幾個(gè)方法(以?gtype.Int?類(lèi)型舉例):

func NewInt(value ...int) *Int
func (v *Int) Add(delta int) (new int)
func (v *Int) Cas(old, new int) bool
func (v *Int) Clone() *Int
func (v *Int) Set(value int) (old int)
func (v *Int) String() string
func (v *Int) Val() intpackage main
import (
"github.com/GOgf/gf/v2/container/gtype"
"fmt"
)
func main() {
// 創(chuàng)建一個(gè)Int型的并發(fā)安全基本類(lèi)型對(duì)象
i := gtype.NewInt()
// 設(shè)置值
fmt.Println(i.Set(10))
// 獲取值
fmt.Println(i.Val())
// 數(shù)值-1,并返回修改之后的數(shù)值
fmt.Println(i.Add(-1))
}執(zhí)行后,輸出結(jié)果為:
0
10
9?gtype?模塊下的所有容器類(lèi)型均實(shí)現(xiàn)了標(biāo)準(zhǔn)庫(kù)?json?數(shù)據(jù)格式的序列化/反序列化接口。
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}),
}
b, _ := json.Marshal(s)
fmt.Println(string(b))
}執(zhí)行后,輸出結(jié)果:
{"Id":1,"Name":"john","Scores":[100,99,98]}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)
fmt.Println(s)
}執(zhí)行后,輸出結(jié)果:
{1 john [100,99,98]} 
我們?cè)谖⑿派?4小時(shí)期待你的聲音
解答本文疑問(wèn)/技術(shù)咨詢(xún)/運(yùn)營(yíng)咨詢(xún)/技術(shù)建議/互聯(lián)網(wǎng)交流