json:"东西"
这种tag标签在go语言里配合反射而生。描述这个结构体字段/属性field.
常见用法有以下几种:
The something:"something"
syntax in Go struct tags is used for various purposes, primarily to provide metadata about struct fields to libraries and frameworks. Here are some common use cases:
1. JSON Encoding/Decoding
The json
tag is used to specify the name of the field when the struct is marshaled or unmarshaled to/from JSON.
type Person struct {
Name string `json:"name"`
Age int `json:"age"`
}
// Example usage
person := Person{Name: "Alice", Age: 30}
jsonData, _ := json.Marshal(person)
fmt.Println(string(jsonData))
// Output: {"name":"Alice","age":30} 这里是小写的,因为tag标签表明了Name是name小写。
// Struct Name之类的field必须大写,否则不会输出exported。encoding,jsonData也就不会有这个字段
如果
type Person struct {
Name string
Age int `json:"age"`
}
则 Output:{"Name":"Alice","age":30} 输出默认的大写
2. XML Encoding/Decoding
The xml
tag is used to specify the name of the field when the struct is marshaled or unmarshaled to/from XML.
type Person struct {
XMLName xml.Name `xml:"person"`
Name string `xml:"name"`
Age int `xml:"age"`
}
// Example usage
person := Person{Name: "Alice", Age: 30}
xmlData, _ := xml.Marshal(person)
fmt.Println(string(xmlData))
// <person><name>Alice</name><age>30</age></person>
// Output: <person><name>Alice</name><age>30</age></person>
3. Database Tags
The db
or gorm
tags are used in database-related libraries to map struct fields to database columns.
type Person struct {
ID int `db:"id" gorm:"primaryKey"`
Name string `db:"name"`
Age int `db:"age"`
}
// Example usage with a database library like sqlx or GORM
4. Form Tags
The form
tag is used to map struct fields to form values in web frameworks.
type Person struct {
Name string `form:"name"`
Age int `form:"age"`
}
// Example usage in a web framework
5. Validation Tags
The validate
tag is used in validation libraries to specify validation rules for struct fields.
type Person struct {
Name string `validate:"required"`
Age int `validate:"min=0"`
}
// Example usage with a validation library like go-playground/validator
6. YAML Encoding/Decoding
The yaml
tag is used to specify the name of the field when the struct is marshaled or unmarshaled to/from YAML.
type Person struct {
Name string `yaml:"name"`
Age int `yaml:"age"`
}
// Example usage
person := Person{Name: "Alice", Age: 30}
yamlData, _ := yaml.Marshal(person)
// Output: name: Alice
// age: 30
7. Custom Tags
You can define your own tags for custom parsing or processing.
type Person struct {
Name string `mytag:"customName"`
Age int `mytag:"customAge"`
}
// Example custom processing
func processTags(v interface{}) {
val := reflect.ValueOf(v).Elem()
for i := 0; i < val.NumField(); i++ {
field := val.Type().Field(i)
tag := field.Tag.Get("mytag")
fmt.Printf("Field: %s, Tag: %s\n", field.Name, tag)
}
}
// Example usage
func main() {
person := Person{Name: "Alice", Age: 30}
processTags(&person)
//Field: Name, Tag: customName
//Field: Age, Tag: customAge
}
Summary
Struct tags are a powerful feature in Go that allow you to add metadata to struct fields for various purposes, including:
- JSON, XML, YAML encoding/decoding
- Database ORM mappings
- Form value mappings
- Validation rules
- Custom processing
Each library or framework may define its own set of tags, and you can also define custom tags for your specific needs. The general syntax is fieldName typeName 'tagName:"tagValue"'
, and the tag value can include multiple key-value pairs separated by spaces.