In the Go language, struct is a common data type that can be used to represent complex data structures. In struct, we can define multiple fields, each field can have different types and names.
In addition to these basic information, Go also provides struct tags, which can be used to specify meta-information for each field in the struct.
In this article, we will explore why struct tags are needed in the Go language, as well as the usage scenarios and advantages of struct tags.
Use of struct tags
Struct tags are still widely used, especially in json serialization or database ORM mapping.
In terms of definition, it appears in the form of key:value, following the struct field. In addition, there are the following points to note:
Use backticks
When declaring a struct tag, use backticks ` to surround the value of the tag to prevent the impact of escape characters and make the tag easier to read and understand. For example:
type User struct {
ID int `json:"id" db:"id"`
Name string `json:"name" db:"name"`
Email string `json:"email" db:"email"`
}
作者:yongxinz
链接:https://juejin.cn/post/7209102613345566776
来源:稀土掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
avoid using spaces
In struct tags, you should avoid using spaces, especially between the tag name and the tag value. Using spaces can cause encoding or decoding errors and make the code more difficult to maintain. For example:
// 不规范的写法
type User struct {
ID int `json: "id" db: "id"`
Name string `json: "name" db: "name"`
Email string `json: "email" db: "email"`
}
// 规范的写法
type User struct {
ID int `json:"id" db:"id"`
Name string `json:"name" db:"name"`
Email string `json:"email" db:"email"`
}
avoid duplication
In a struct, repeated use of the same tag name should be avoided. If the same tag name is used repeatedly, the compiler may not recognize the tag, resulting in encoding or decoding errors. For example:
// 不规范的写法
type User struct {
ID int `json:"id" db:"id"`
Name string `json:"name" db:"name"`
Email string `json:"email" db:"name"`
}
// 规范的写法
type User struct {
ID int `json:"id" db:"id"`
Name string `json:"name" db:"name"`
Email string `json:"email" db:"email"`
}
Use standardized tag names
In order to make struct tags more standardized and easier to maintain, some standardized tag names should be used.
For example, for serialization and deserialization, you can use json, xml, yaml, etc.; for database operations, you can use db.
type User struct {
ID int `json:"id" db:"id"`
Name string `json:"name" db:"name"`
Password string `json:"-" db:"password"` // 忽略该字段
Email string `json:"email" db:"email"`
}
Among them, the - after the Password field indicates that the field is ignored, which means that the field will not be serialized or deserialized.
Multiple tag values
If a field needs to specify multiple tag values, you can use, to separate the multiple tag values. For example:
type User struct {
ID int `json:"id" db:"id"`
Name string `json:"name" db:"name"`
Email string `json:"email,omitempty" db:"email,omitempty"`
}
Among them, omitempty means that if the field value is empty, the field will not be serialized.