News for hackers. Stuff that matters.
Re:Compile-time safety for enumerations in Go by andreygrehov on Friday September 29, 2023 @09:19PM (#37712006)
Another approach could be:

  package color

  type Color struct {
    val string
  }

  func (c Color) String() string {
    return c.val
  }

  var (
    Red   = Color{val: "red"}
    Green = Color{val: "green"}
    Blue  = Color{val: "blue"}
  )
Since `val` is not exported, external packages cannot create arbitrary `Color`.
Re:Compile-time safety for enumerations in Go by curvilinear_m on Saturday September 30, 2023 @02:44AM (#37713267)
Ins't it still possible to construct the zero value for Color ? Like how bytes.Buffer does it https://pkg.go.dev/bytes#Buffer : the implementation is not exported but you the usage is to construct the zero value and use it. So unless Color{} is a valid enum value, this solution would not work.