Structs
Structs can embed another. Example of defining:
type Animal struct {
Name string
Origin string
}
type Bird struct {
// This is embeding
Animal
CanFly bool
Speed float32
}
func main() {
b := Bird{
Animal: Animal{Name: "Emu", Origin: "Australia"},
CanFly: false,
Speed: 48,
}
// Output: {{Emu Australia} false 48}
fmt.Println(b)
// Output: Emu
fmt.Println(b.Name)
}
Structs can include field tags. To access that tag, need to use reflection package
import (
"fmt"
"reflect"
)
type Animal struct {
Name string `required max:"100"`
}
func main() {
t := reflect.TypeOf(Animal{})
field, _ := t.FieldByName("Name")
fmt.Println(field.Tag)
}