新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)GoFrame教程:GoFramegvar-基本使用
基本使用
package main
import (
"github.com/GOgf/gf/v2/frame/g"
"fmt"
)
func main() {
var v g.Var
v.Set("123")
fmt.Println(v.Val())
// 基本類型轉(zhuǎn)換
fmt.Println(v.Int())
fmt.Println(v.Uint())
fmt.Println(v.Float64())
// slice轉(zhuǎn)換
fmt.Println(v.Ints())
fmt.Println(v.Floats())
fmt.Println(v.Strings())
}執(zhí)行后,輸出結(jié)果為:

123
123
123
123
[123]
[123]
[123]
JSON序列化/反序列
?gvar.Var?容器實現(xiàn)了標準庫?json?數(shù)據(jù)格式的序列化/反序列化接口。
Marshal
package main
import (
"encoding/json"
"fmt"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
type Student struct {
Id *g.Var
Name *g.Var
Scores *g.Var
}
s := Student{
Id: g.NewVar(1),
Name: g.NewVar("john"),
Scores: g.NewVar([]int{100, 99, 98}),
}
b, _ := json.Marshal(s)
fmt.Println(string(b))
}執(zhí)行后,輸出結(jié)果:
{"Id":1,"Name":"john","Scores":[100,99,98]}
Unmarshal
package main
import (
"encoding/json"
"fmt"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
b := []byte(`{"Id":1,"Name":"john","Scores":[100,99,98]}`)
type Student struct {
Id *g.Var
Name *g.Var
Scores *g.Var
}
s := Student{}
json.Unmarshal(b, &s)
fmt.Println(s)
}執(zhí)行后,輸出結(jié)果:
{1 john [100,99,98]} 網(wǎng)站標題:創(chuàng)新互聯(lián)GoFrame教程:GoFramegvar-基本使用
網(wǎng)頁網(wǎng)址:http://www.dlmjj.cn/article/dhpdjsg.html


咨詢
建站咨詢
