ZhangZhihui's Blog  

Problem: You want to define metadata to describe the struct fields.


Solution: Use struct tags to define metadata and the reflect package to access the tags.

 

One very common place you find this is in the json package:

type Person struct {
    Id         int    `json:"id"`
    GivenName  string `json:"given_name"`
    FamilyName string `json:"family_name"`
    Email      string `json:"email"`
}

Many packages that process structs use struct tags. For example, xml package, the protobuf package, and the sqlx package all use struct tags. You can also roll out your support for struct tags using the reflect package.

Struct tags are defined in name-value pairs within the string literal. In the preceding example, where the string literal is json:"given_name" , the name is json and the value is a string "given_name".

Here’s how you can extract the values:

func main() {
    person := Person{
        Id:         1,
        GivenName:  "Sau Sheong",
        FamilyName: "Chang",
        Email:      "sausheong@email.com",
    }

    p := reflect.TypeOf(person)
    k := fmt.Sprint(reflect.ValueOf(person).Kind())
    if k == "struct" {
        for i := 0; i < p.NumField(); i++ {
            field := p.Field(i)
            fmt.Println(field.Tag.Get("json"))
        }
    }
}

To get the values from the struct tags, you use the reflect package. Start with getting the Type of the person variable, which is a struct. The NumField method on the struct Type tells you how many fields are in this struct , which is 4. Then you call the Field method on the struct Type with a given index to get the StructField . Call the Tag method on the StructField to get the StructTag , and finally call the Get method with the name to get the value.

Each struct tag can contain more than a single name-value pair, so you can call Get on different names to get to the corresponding values.

 

posted on 2023-10-07 22:32  ZhangZhihuiAAA  阅读(5)  评论(0编辑  收藏  举报