0Pricing
Go Academy · Lesson

What Are Struct Tags

Attach metadata to fields.

What Are Struct Tags is a free Go Academy lesson on CoddyKit — lesson 1 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.

What Are Struct Tags

A struct tag is a string of metadata attached to a struct field. It does not affect the field's value but carries instructions for libraries that read it.

Tag Syntax

Tags appear after the field type, wrapped in raw string quotes. Each tag is a key:"value" pair. Multiple pairs are space-separated.

A typical tag looks like json:"name".

A Tagged Struct

Here a field carries a json tag telling encoders to use the lowercase key name.

package main

import "fmt"

type User struct {
	Name string "json:\"name\""
}

func main() {
	u := User{Name: "Ann"}
	fmt.Println(u.Name)
}

Tags Are Just Metadata

Tags do not change how your code uses the field. u.Name still works normally. Tags only matter to libraries that inspect them via reflection.

Multiple Tags on One Field

A single field can hold several tag keys, each used by a different library.

package main

import "fmt"

type Product struct {
	Name string "json:\"name\" db:\"product_name\""
}

func main() {
	p := Product{Name: "Pen"}
	fmt.Println(p.Name)
}

Common Tag Keys

Frequently used tag keys include:

  • json - JSON encoding
  • xml - XML encoding
  • db - database column mapping
  • validate - validation rules

Reading Tags via Reflection

The reflect package lets you read tags at runtime. field.Tag.Get("json") returns the value for the json key.

package main

import (
	"fmt"
	"reflect"
)

type User struct {
	Name string "json:\"name\""
}

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

The Tag.Get Method

Tag.Get(key) returns the value for a key, or an empty string if the key is absent. Tag.Lookup(key) also reports whether the key existed.

package main

import (
	"fmt"
	"reflect"
)

type User struct {
	Name string "json:\"name\""
}

func main() {
	f := reflect.TypeOf(User{}).Field(0)
	val, ok := f.Tag.Lookup("json")
	fmt.Println(val, ok)
}

Tag Format Convention

The standard tag format is key:"value" with no spaces around the colon. Libraries expect this convention. Malformed tags may be silently ignored.

Why Use Tags

Tags let you keep mapping rules next to the fields they describe, instead of in separate config. One struct can serve JSON output, database rows, and validation, all driven by tags.

Tags on Unexported Fields

Tags on unexported (lowercase) fields are visible via reflection but most encoders skip unexported fields entirely. Keep fields you want serialized exported (capitalized).

Quick Check

What is the purpose of a struct tag?

Recap

Struct tags:

  • Metadata attached to fields in key:"value" form
  • Do not affect normal code; read via reflection
  • Used by json, xml, db, validate, and more

Frequently asked questions

Is the “What Are Struct Tags” lesson free?

Yes — the full text of “What Are Struct Tags” 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 “What Are Struct Tags”?

Attach metadata to fields. 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 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “What Are Struct Tags” 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