package main
import (
"fmt"
//提供了json对象的解析码
"encoding/json"
)
type student struct {
name string
age int
score int
}
type student2 struct {
//加入tag 相当于起别名
Name string `json:"name"`
Age int `json:"age"`
Score int `json:"score"`
}
func main () {
//结构体中的tag
s1 := &student{
name: "zhangsan",
age: 18,
score: 100,
}
//json.Marsha1 把参数json 化
a, err := json.Marshal(s1)
if err != nil {
fmt.Println("json化失败")
return
}
fmt.Println(string(a)) // {} 因为struct 里面全为小写,在json包里面访问不了
s2 := &student2{
Name: "zhangsan",
Age: 18,
Score: 100,
}
b, err := json.Marshal(s2)
if err != nil {
fmt.Println("json化失败")
return
}
fmt.Println(string(b)) //{"name":"zhangsan","age":18,"score":100}
}
tag 就是别名在打包的时候使用。