How to generate Go code using stringer

Writing code for repetitive tasks like creating methods to validate custom types or turning those types into readable texts can be time-consuming and require significant effort for debugging and testing. To address this issue, Go provides a tool called stringer.

The stringer tool

The stringer tool is included in the golang.org/x/tools/cmd/stringer package. It simplifies the process of generating string methods for custom types. String methods are functions in Go that implement the stringer interface, specifically the String() method. This method defines how a custom type should be converted to a readable format such as string representation. Instead of manually writing a String() method for each value of a custom enum type, the stringer can automate this process.

When applied to a custom type, such as an enum-like type with defined constants, the stringer automatically generates an implementation of the String() method. This tool reads the definition of the custom type and its constants and produces a Go source file with the String() method implemented.

// Define a custom type with constants
type Day int
const (
Sunday Day = iota
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
)
// The stringer tool will automatically generate the String() method for this type, so you don't have to write it manually.

By utilizing the stringer tool, developers can save time and effort that would otherwise be spent manually writing and maintaining string methods for their custom types. This leads to improved code quality, reduced chances of errors, and enhanced code maintainability.

Code example

Suppose we have to validate the type of file and want to accept two types of files; Flat or Zip. There are two ways to do this; firstly write custom validation code manually, and secondly use stringer tool. Here we use stringer.

Steps to be followed

  1. The first step is to configure the environment in your system to run stringer:

go get golang.org/x/tools/cmd/stringer@latest
  1. Set the PATH to access the stringer tool:

export PATH="$PATH:/Users/<Username>/go/bin"

<Username> will be according to your system.

  1. Create custom type:

package src
type FileType int
const (
FlatType FileType = iota
ZipType
)
func ParseFileType(input string) FileType {
switch input {
case FlatType.String():
return FlatType
case ZipType.String():
return ZipType
default:
return -1 // Invalid file type
}
}
  • Line 3: We create a custom type FileType.

  • Lines 5–8: We define enums for FlatType and ZipType.

  • Lines 10–19: We define a ParseFileType which validates our input string based on the String() method that will be generated by the stringer tool.

  1. To use the stringer tool, we have two methods; using the command line or using comments to generate Go. Here, we will discuss both:

    1. Using comments

    2. Using CLI

Using comments

To generate code using comments, we need to add the line 3 in our code. In this line go:generate is Go tool chain that tells the Go compiler to execute the command followed by generate keyword (stringer -type-FileType in our case):

package src
//go:generate stringer -type=FileType
type FileType int
const (
FlatType FileType = iota
ZipType
)
func ParseFileType(input string) FileType {
switch input {
case FlatType.String():
return FlatType
case ZipType.String():
return ZipType
default:
return -1 // Invalid file type
}
}

To generate code using comments that we inserted in our code, run the following command in the terminal:

go generate <path to the file>

In the above command, <path to file> is the file in which we add comments for stringer command.

Using CLI

If we do not want to add comments like in line 3 in the above code. We can directly run stringer by ourselves using a terminal like below:

stringer -type=<custom type> <path to file.go>
  • stringer is the command that we set up in steps 1 and 2.

  • -type=<custom type> in this flag type is a keyword that specifies the type that we want to create and <custom type> is the name of our type, FileType in our case.

  • <path to file.go> is the path where we define the structure for custom type. It can be an absolute or relative path but it must include the file name at the last.

Output

Both methods will generate the same following output code:

// Code generated by "stringer -type=FileType"; DO NOT EDIT.
package src
import "strconv"
func _() {
// An "invalid array index" compiler error signifies that the constant values have changed.
// Re-run the stringer command to generate them again.
var x [1]struct{}
_ = x[FlatType-0]
_ = x[ZipType-1]
}
const _FileType_name = "FlatTypeZipType"
var _FileType_index = [...]uint8{0, 8, 15}
func (i FileType) String() string {
if i < 0 || i >= FileType(len(_FileType_index)-1) {
return "FileType(" + strconv.FormatInt(int64(i), 10) + ")"
}
return _FileType_name[_FileType_index[i]:_FileType_index[i+1]]
}

Test yourself

To test the code, click the “Run” button and execute the below commands in the terminal:

Note: To use CLI method, you can remove the line 3 in the file_type.go.

# Command to run stringer
stringer -type=FileType src/file_type.go
# Command to generate code
go generate ./...
# Command to run code
go run main.go
# Command to list the file inside the src folder
ls src
# Command to move into src directory
cd src
# Command to open the file
cat filetype_string.go
module stringer

go 1.23
Example: Validate the file type using stringer tool to generate String() method for custom type

Conclusion

By leveraging stringer, developers can save time and effort that would otherwise be spent on manual implementation and maintenance of string methods. Both methods achieve the same result, generating the necessary code for string methods. This not only improves code quality but also enhances maintainability and reduces the risk of bugs.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved