stringer

command module
v0.0.0-...-9d03f5d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 13, 2025 License: MIT Imports: 15 Imported by: 0

README

stringer

Standard stringer tool with enhancements

Documentation

Overview

Stringer is a tool to automate the creation of methods that satisfy the fmt.Stringer interface. Given the name of a (signed or unsigned) integer type T that has constants defined, stringer will create a new self-contained Go source file implementing

func (t T) String() string

The file is created in the same package and directory as the package that defines T. It has helpful defaults designed for use with go generate.

Stringer works best with constants that are consecutive values such as created using iota, but creates good code regardless. In the future it might also provide custom support for constant sets that are bit patterns.

For example, given this snippet,

package painkiller

type Pill int

const (
	Placebo Pill = iota
	Aspirin
	Ibuprofen
	Paracetamol
	Acetaminophen = Paracetamol
)

running this command

stringer -type=Pill

in the same directory will create the file pill_string.go, in package painkiller, containing a definition of

func (Pill) String() string

That method will translate the value of a Pill constant to the string representation of the respective constant name, so that the call fmt.Print(painkiller.Aspirin) will print the string "Aspirin".

Typically this process would be run using go generate, like this:

//go:generate stringer -type=Pill

If multiple constants have the same value, the lexically first matching name will be used (in the example, Acetaminophen will print as "Paracetamol").

With no arguments, it processes the package in the current directory. Otherwise, the arguments must name a single directory holding a Go package or a set of Go source files that represent a single Go package.

The -type flag accepts a comma-separated list of types so a single run can generate methods for multiple types. The default output file is t_string.go, where t is the lower-cased name of the first type listed. It can be overridden with the -output flag.

Types can also be declared in tests, in which case type declarations in the non-test package or its test variant are preferred over types defined in the package with suffix "_test". The default output file for type declarations in tests is t_string_test.go with t picked as above.

The -linecomment flag tells stringer to generate the text of any line comment, trimmed of leading spaces, instead of the constant name. For instance, if the constants above had a Pill prefix, one could write

PillAspirin // Aspirin

to suppress it in the output.

The -trimprefix flag specifies a prefix to remove from the constant names when generating the string representations. For instance, -trimprefix=Pill would be an alternative way to ensure that PillAspirin.String() == "Aspirin".

Additional Methods

The -valid flag generates a Valid() bool method that returns true if the value is one of the defined constants.

The -invalid flag accepts a comma-separated list of values or ranges that should be considered invalid. For example, -invalid="0,<4,>=100" marks 0, values less than 4, and values greater than or equal to 100 as invalid. This affects the Valid() method and the reverse lookup function.

The -reverse flag generates a Reverse{{Type}}(s string, caseSensitive bool) ({{Type}}, bool) function that performs a reverse lookup from string to enum value.

The -replace flag (can be used multiple times) specifies string replacements to apply in the reverse lookup function. For example, -replace=-,_ will replace dashes with underscores before lookup. Requires -reverse.

The flag supports backslash escaping for special characters:

\,  → literal comma (allows replacing commas)
\\  → literal backslash

For example, -replace=\,,_ replaces commas with underscores.

When multiple -replace flags are used, they are applied sequentially in the order specified. For example, -replace=a,b -replace=b,c will transform "a" to "c" (a→b→c).

The -marshal flag generates MarshalJSON and UnmarshalJSON methods for JSON encoding/decoding. The methods use the String() representation for marshaling and the Reverse{{Type}} function for unmarshaling. MarshalJSON validates the value using Valid() before marshaling, returning an error for invalid values. Requires -reverse and automatically enables -valid.

The -marshalinsensitive flag makes UnmarshalJSON case-insensitive when looking up string values. For example, "red", "Red", and "RED" would all unmarshal to the same value. Automatically enables -marshal (and transitively -reverse and -valid).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL