finds.dev← search

// the find

fatih/structtag

★ 651 · Go · NOASSERTION · updated Sep 2023

Parse and modify Go struct field tags

structtag parses Go struct field tags, such as the json and xml tags on a struct field, into a list you can read, change, and write back as a string. It is the parsing layer underneath gomodifytags, so it is aimed at people writing code generation and editor tooling rather than application code that just needs a tag value at runtime.

The whole API fits in the README: Parse, Get, Set, Tags, and a sort.Interface implementation for deterministic output order. Set replaces a tag by key or appends a new one, so you can change one tag without rebuilding the string by hand. The code is two files, tags.go and tags_test.go, which makes it easy to read in one sitting. Since gomodifytags depends on it, the round-trip behavior has been exercised by a real code-editing tool and not only by its own tests.

The last push was September 2023 and the tree shows no changes since, so treat it as finished rather than maintained. Check go.mod and confirm it builds on your toolchain before depending on it. The README only covers the happy path. It says nothing about malformed input, duplicate keys, or escaped characters beyond Parse returning an error, so you will need to read tags_test.go to learn the edge cases. Options are plain strings with no typed model, so any meaning attached to an option, like omitempty or a database key flag, is your code's job to interpret.

View on GitHub →

// want more like this?

We dig through GitHub every week and send a few repos picked for what you actually care about — each with an honest take like this one.

Get finds in your inbox → Search again →