type T struct { f1 string"f one" f2 string f3 string`f three` f4, f5 int64`f four and five` f6 string`key1:"value1" key1:"value2"` f7 string"json:\"f7\""// 可读性很低 }
type T struct { f1 string"f one" f2 string f3 string`f three` f4, f5 int64`f four and five` f6 string`key1: "value1" key1:"value2"` f7 string"json:\"f7\"" }
funcmain() { t := reflect.TypeOf(T{})
f1, _ := t.FieldByName("f1") fmt.Println(f1.Tag) // f one fmt.Println(f1.Tag.Get("f1")) //
f3, _ := t.FieldByName("f3") fmt.Println(f3.Tag) // f three fmt.Println(f3.Tag.Get("f1")) //
f4, _ := t.FieldByName("f4") fmt.Println(f4.Tag) // f four and five fmt.Println(f4.Tag.Get("f4")) //
f5, _ := t.FieldByName("f5") fmt.Println(f5.Tag) // f four and five fmt.Println(f5.Tag.Get("f5")) // // 可以看到上面部分的实例代码,通过结构体方法获取不到有效的值 // 这也是为什么不会直接跟一个字符串的原因,因为结构体标签的方法在这种情况下作用体现不出来