Reputation: 34
I have a struct like this:
.foo/bar/constants.go
.foo/constants.go
.main.go
In main.go
I declare the type:
package agepack
type EventType uint
//go:generate stringer -type EventType
const (
FirstType EventType iota
SecondType
....
)
In each constants.go
I have soimething like this:
package foo
const (
OneMoreType agepack.EventType = 100 + iota
)
How can I generate stringer with values from all packages?
Upvotes: 2
Views: 1834
Reputation:
It is in fact possible to generate the Stringer interface on multiple packages: more specifically on all packages contained in a directory (recursively). Just use
$ go generate ./...
This is just like go test ./...
which allows you to execute all tests contained in a directory recursively.
Upvotes: 0
Reputation: 418167
golang.org/x/tools/cmd/stringer
doesn't support this. Quoting from its doc:
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.
Easiest solution would be to put all enum values in the same package. You may use separate files, but they must be in the same package.
If you want / must use multiple packages, you can only do this with stringer
if you use different types, each defined in the package in which you list the constants.
Upvotes: 2