Golang struct
Struct
在Go语言中,对结构体进行&
取地址操作时,视为对该类型进行一次 new 的实例化操作,取地址格式如下:
Go |
---|
1
2
3
4
5
6
7
8
9
10
11
12 | type T struct{
field int
}
p := &T{}
v := T{}
p = &v
---
// I much more like this, wtf is the `:`? It's esay to forget and hard to mention
var p = &T{}
var v = T{}
p = &p
|
Tag
which is supported by the golang's reflect
This will make a json marshal auto convert the UserName
field into user_name
Go |
---|
| type User struct {
UserName string `json:"user_name"`
}
|
Reflect example
Go |
---|
| var field, ok = reflect.TypeOf(User).Elem().FieldByName("UserName")
if !ok {
panic("Field not found")
}
var json = field.Tag.Get("json")
println(json) //"user_name"
|