go struct 自定义标签获取

package main

import (
"fmt"
"reflect"
)

type Test struct {
Id int `json:"user_id"`
Name string "this is name"
}

func main() {
t := Test{Id: 1, Name: "xiaochuan"}
r := reflect.TypeOf(t)

fmt.Println()
fmt.Printf("Id Tag is %s \n", r.Field(0).Tag)
fmt.Println()
fmt.Printf("Name Tag is %s \n", r.Field(1).Tag)
}

猜你喜欢

转载自www.cnblogs.com/mafeng/p/10907494.html