最近在看 golang 的代码,看到这么一段
type ContainerStats struct { // The time of this stat point. Timestamp time.Time `json:"timestamp"` Cpu CpuStats `json:"cpu,omitempty"` DiskIo DiskIoStats `json:"diskio,omitempty"` Memory MemoryStats `json:"memory,omitempty"` Network NetworkStats `json:"network,omitempty"` }
请问 类型后 json:"…"
的形式指的是什么?
1 lostarry 2015-09-05 13:30:39 +08:00 ![]() |
![]() | 2 beyondsoft 2015-09-05 14:31:20 +08:00 Field appears in JSON as key "myName". |
![]() | 3 wayn3h0 2015-09-05 14:43:19 +08:00 ![]() @zeroday JSON 序列化和反序列化 的 key Timestamp time.Time `json:"timestamp"` 指定了 key, 序列化后 key 为 timestamp Timestamp time.Time 未指定 key, 序列化后 key 为 Timestamp |
![]() | 4 Zzzzzzzzz 2015-09-05 14:50:00 +08:00 反射 Tag, Go 在运行时可以取到, 然后根据这个再处理具体业务, 比如表单的 field 、 数据表的 column 以及这个例子里的 json 字段, 但是这个特性一旦滥用拉性能拉得厉害. |
5 lynx 2015-09-05 15:11:24 +08:00 |
6 shawngao 2015-09-05 15:37:52 +08:00 via Android 变量实际对应的 json 段的 key |
7 mengzhuo 2015-09-05 22:59:52 +08:00 struct 的 tag 可以自己随便编的 比如我写的 sdk 里 ChargeType string `ucloud:"optional"` |
8 datou552211 2015-09-06 10:51:03 +08:00 via iPhone ![]() 顺带一句, tag 的性能很拖后退 |