结构体成员加了第三个位置的标签,在转换指定类型时,key 会使用指定的名字。
package main import ( "encoding/json" "log" ) type Person struct { Age int FirstName string LastName string `json:"last_name"` } func main() { p := Person{30, "Bob", "Bran"} log.Println(p) // {30 Bob Bran} p.Age = 20 log.Println(p) // {20 Bob Bran} j, _ := json.Marshal(p) log.Println(string(j)) // {"Age":20,"FirstName":"Bob","last_name":"Bran"} }
Source: https://github.com/farwish/go-lab/blob/master/go-syntax/struct-tag.go
Link:https://www.cnblogs.com/farwish/p/12916970.html