Tag|Docs

What is Tag?

Understand Tag as a declarative struct tag parser for Go

What is Tag?

Tag is a declarative struct tag parser for Go. It turns raw struct tag strings into typed Go values by reading a small schema you define with regular structs.

Use it when a tag value needs more structure than a single string: primary values, named options, boolean flags, primitive conversion, slices, maps, nested structs, custom types, and predictable parsing errors.

Why use it?

Go's reflect.StructTag gives you the raw value for a tag name. The next step is usually custom parsing: split by commas, preserve quoted values, detect flags, convert primitives, and map option names back into fields.

Tag keeps that parsing logic declarative. You describe the tag shape once:

schema.go
type DataSourceTag struct {
	Host     string `option:"value"`
	Username string `option:"username"`
	Password string `option:"password"`
	Driver   string `option:"driver"`
}

func (DataSourceTag) Tag() string {
	return "datasource"
}

Then the parser binds the raw tag into that schema. The schema stays close to the code that consumes the parsed values, so tag formats are easier to read, test, and change over time.

Core ideas

  • Implement Tag() string to select which struct tag name should be parsed.
  • Use option:"value" for the primary positional value.
  • Use option:"name" to map a named option to a struct field.
  • Let field types control how raw values are converted.
  • Handle parsing errors explicitly instead of relying on silent zero values.

Where it fits

Tag is useful for libraries and frameworks that expose configuration through struct tags. It keeps tag parsing small and predictable without forcing a larger configuration system into your package.