0Pricing
Go Academy · Lesson

Custom Tag Parsing

Read tags via reflection.

Custom Tag Parsing is a free Go Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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())
}

Iterating Fields

NumField and Field(i) let you loop over every field of a struct type.

package main

import (
	"fmt"
	"reflect"
)

type User struct {
	Name string
	Age  int
}

func main() {
	t := reflect.TypeOf(User{})
	for i := 0; i < t.NumField(); i++ {
		fmt.Println(t.Field(i).Name)
	}
}

Accessing a Field Tag

Each field has a Tag of type reflect.StructTag. Call .Get(key) to read a specific tag value.

package main

import (
	"fmt"
	"reflect"
)

type User struct {
	Name string "label:\"Full Name\""
}

func main() {
	f := reflect.TypeOf(User{}).Field(0)
	fmt.Println(f.Tag.Get("label"))
}

Defining a Custom Tag

You can invent any tag key. Here a custom label tag describes a human-friendly name for each field.

package main

import (
	"fmt"
	"reflect"
)

type Form struct {
	Email string "label:\"Email Address\""
	City  string "label:\"City\""
}

func main() {
	t := reflect.TypeOf(Form{})
	for i := 0; i < t.NumField(); i++ {
		f := t.Field(i)
		fmt.Println(f.Name, "->", f.Tag.Get("label"))
	}
}

Tag.Lookup for Optional Tags

Tag.Lookup(key) returns the value and a bool. Use it to handle fields that may not have your tag.

package main

import (
	"fmt"
	"reflect"
)

type Form struct {
	Email string "label:\"Email\""
	Token string
}

func main() {
	t := reflect.TypeOf(Form{})
	for i := 0; i < t.NumField(); i++ {
		v, ok := t.Field(i).Tag.Lookup("label")
		fmt.Println(t.Field(i).Name, v, ok)
	}
}

Reading Field Values Too

Combine reflect.ValueOf with the type to read both the tag and the live value of each field.

package main

import (
	"fmt"
	"reflect"
)

type Form struct {
	Email string "label:\"Email\""
}

func main() {
	f := Form{Email: "a@b.com"}
	t := reflect.TypeOf(f)
	v := reflect.ValueOf(f)
	fmt.Println(t.Field(0).Tag.Get("label"), "=", v.Field(0))
}

Parsing Tag Options

If your tag value packs options (like name,opt), split it yourself with strings.Split on the comma.

package main

import (
	"fmt"
	"strings"
)

func main() {
	tag := "name,omitempty"
	parts := strings.Split(tag, ",")
	fmt.Println("name:", parts[0], "opts:", parts[1:])
}

A Mini Use Case

Putting it together: build a simple label map from a struct, which a UI layer could use to render form labels.

package main

import (
	"fmt"
	"reflect"
)

type Profile struct {
	Name string "label:\"Your Name\""
	Bio  string "label:\"About You\""
}

func main() {
	t := reflect.TypeOf(Profile{})
	labels := map[string]string{}
	for i := 0; i < t.NumField(); i++ {
		f := t.Field(i)
		labels[f.Name] = f.Tag.Get("label")
	}
	fmt.Println(labels)
}

Reflection Costs

Reflection is powerful but slower than direct code and bypasses compile-time checks. Use it for framework-style features, not in hot loops.

Pointers and reflect

If you have a pointer, call reflect.TypeOf(v).Elem() to reach the underlying struct type before inspecting fields.

package main

import (
	"fmt"
	"reflect"
)

type User struct {
	Name string "label:\"Name\""
}

func main() {
	u := &User{}
	t := reflect.TypeOf(u).Elem()
	fmt.Println(t.Field(0).Tag.Get("label"))
}

Quick Check

Which method reads the value of a custom tag key from a struct field obtained via reflection?

Recap

Custom tag parsing:

  • Use reflect.TypeOf and iterate fields
  • field.Tag.Get / Lookup read tag values
  • Split values to parse options
  • Powerful but slower; use for framework features

Frequently asked questions

Is the “Custom Tag Parsing” lesson free?

Yes — the full text of “Custom Tag Parsing” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.

What will I learn in “Custom Tag Parsing”?

Read tags via reflection. You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Go Academy?

No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Custom Tag Parsing” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Go Academy lesson?

Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

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