Tags and introspection (reflect)
-
reflect
-
TypeOf
-
We can add "free-text" tags to the elements of the struct, but it is better to use key-value pairs in there.
-
We can use introspection to look at the content of the tags.
package main
import (
"fmt"
"reflect"
)
type aPerson struct {
id int `unique:"true"`
name string `required:"true" max:"100"`
children []string
}
func main() {
t := reflect.TypeOf(aPerson{})
field, ok := t.FieldByName("id")
if ok {
fmt.Printf("id: %v\n", field.Tag)
fmt.Println(field.Tag.Get("unique"))
fmt.Println(field.Tag.Get("required"))
value, ok := field.Tag.Lookup("unique")
if ok {
fmt.Printf("unique value: %v\n", value)
}
}
field, ok = t.FieldByName("children")
if ok {
fmt.Printf("children: %v\n", field.Tag)
}
a := aPerson{
id: 1,
}
fmt.Println(a)
}
id: unique:"true"
true
unique value: true
children:
{1 []}