goenums
goenums
is a tool for auto generating Go Enum methods. Inspired by Stringer and gotests.
Overview
- Provide better code consistence for enum
- Must be signed or unsigned integer type
- Must contain 0 value as fall back
- Upsert the basic methods into the same file without overwriting other functions.
- Append if
functions/methods/variables
not exists
- Replace if
variables
exists
- Do nothing if
functions/methods
exists
Getting Started
Add the dependency with latest version
go get github.com/u-next/goenums
Create a tools.go
file and add goenums
as a tool dependency.
//go:build tools
// +build tools
package tools
import (
_ "github.com/u-next/goenums"
)
goenums
support various interfaces, run the command with either one below
go generate ./...
goenums [options]
go:generate
- Given the name of a (signed or unsigned) integer type T that has constants defined
package values
type Resolution int
const (
ResolutionUnspecified Resolution = iota
ResolutionSD
ResolutionHHD
ResolutionFHD
ResolutionUHD4K
ResolutionUHD8K
)
- Put the magic comment between
package
and import
to tells go generate
what command to run when we want to regenerate our code
package values
+ //go:generate go run github.com/u-next/goenums
type Resolution int
- Run go generate recursively over your entire project
go generate ./...
Command Line
# e.g.enums <examples/type.go>
goenums <filepath>
# Show the help menu
goenums -h
A commandline tool for auto generating Go Enum methods.
Usage: $ goenums [options] <filepath>
For more information, see:
https://github.com/u-next/goenums
Options:
-case string
The case style of enum in String. Available options are:
- UPPER_SNAKE_CASE
- lower_snake_case
- UpperCamelCase
- lowerCamelCase
(default "UPPER_SNAKE_CASE")
-dryrun
Should print the result to console rather than pipe to a file
-json
Should generate methods implementing json.Marshaler and json.Unmarshaler interface
-trim string
Trim the value of enum name