What are struct tags and how does reflection use them?

Updated Apr 28, 2026

Short answer

Struct tags are metadata attached to struct fields, interpreted at runtime using reflection.

Deep explanation

A struct tag is a raw string literal (using backticks) following the field type. Packages like encoding/json or gorm use the reflect package to read these tags at runtime to understand how to serialize/deserialize data, handle database columns, or enforce validation.

Real-world example

Defining how a Go struct maps to a JSON payload in an HTTP response, including ignoring empty fields with omitempty.

Common mistakes

  • Making typographical errors in struct tags (e.g., `json: "id"` with a space). Because they are just strings, the compiler won't catch the error, and reflection will silently ignore it.

Follow-up questions

  • Is reflection fast?

More Golang interview questions

View all →