/structtag

Parse StructTag into a Map

Primary LanguageGoMIT LicenseMIT

structtag

Version GoDoc Go Report Card

The motivation behind this package is that the StructTag implementation shipped with Go's standard library is very limited in detecting a malformed StructTag and each time StructTag.Get(key) gets called, it results in the StructTag being parsed again. This package provides a way to parse the StructTag into a structtag.Map.

// Example of struct using tags to append metadata to fields.
type Server struct {
	Host string `json:"host" env:"SERVER_HOST" default:"localhost"`
	Port int    `json:"port" env:"SERVER_PORT" default:"3000"`
}

Install

go get github.com/quartercastle/structtag

Usage

tags, err := structtag.Parse(`json:"host" env:"SERVER_HOST"`)

if err != nil {
  panic(err)
}

fmt.Println(tags["json"])

See godoc for full documentation.

License

This project is licensed under the MIT License.