0Pricing
Go Academy · Lesson

Custom Tag Parsing

Read tags via reflection.

Reading Tags Yourself

You can define your own tag keys and read them at runtime with the reflect package. This lets you build custom behavior driven by metadata.

reflect.TypeOf

reflect.TypeOf returns a Type describing a value. From a struct Type you can inspect its fields.

package main

import (
	"fmt"
	"reflect"
)

type User struct {
	Name string
}

func main() {
	t := reflect.TypeOf(User{})
	fmt.Println(t.Kind(), t.NumField())
}

All lessons in this course

  1. What Are Struct Tags
  2. JSON Tags
  3. Validation Libraries
  4. Custom Tag Parsing
← Back to Go Academy