top | item 39567441

(no title)

lantastic | 2 years ago

I like the "go generate" approach to integrate such tools. I used a different (but identically named) go-enum tool [0], which accepts the go type and generates implementations for a bunch of interfaces. The neat thing is that the starting point is the "idiomatic" go enum definition, rather than description via a separate DSL:

  //go:generate go-enum -type=State
  type State int
  const (
    Unknown State = 0
    Disconnected = 1
    Connected = 2
  )
which then generates a separate file with implementations such as:

  func (i State) MarshalJSON() ([]byte, error) { ...

[0] https://pkg.go.dev/github.com/searKing/golang/tools/go-enum#...

discuss

order

No comments yet.