Go编码相关
结构体标签
1 | package main |
1 | Tokey@DESKTOP-7UHT0GC MINGW64 /e/GoPath/src/day05 |
总结
对于结构体进行编码(json)时,首字母必须大写,否则不参与编码。
如果json格式要求key小写,那么可以通过标签实现。
标签(tag)说明:
1
2
3
4
5
6
7
8
9
10
11type Teacher struct {
//表示 在使用json编码时,这个编码不参与
Id int `json:"-"`
//表示 在使用json编码时,key会使用Teacher_name
Name string `json:"Teacher_name"`
//表示 在使用json编码时,将age转成string类型,切记中间是逗号(不能省略),不能有空格
Age int `json:",string"`
//表示 在使用json编码时,如果这个字段是空值,那么忽略掉,不参与编码
Address string `json:",omitempty"`
gender string
}