Skip to content

Instantly share code, notes, and snippets.

@bozdoz
Created November 6, 2022 14:57
Show Gist options
  • Save bozdoz/df442b61cf1224be59ae4f03ecaff662 to your computer and use it in GitHub Desktop.
Save bozdoz/df442b61cf1224be59ae4f03ecaff662 to your computer and use it in GitHub Desktop.
exclusive_enums.go
package main
type dessert int
const (
ICE_CREAM dessert = iota
CUP_CAKES
GUMMY_WORMS
)
type exclusive_dessert interface {
ignore_me()
}
func (d dessert) ignore_me() {}
// now use exclusive_dessert
func use_dessert(d exclusive_dessert) {}
// use_dessert(0) doesn't work!
// must use use_dessert(ICE_CREAM)
@flan6
Copy link

flan6 commented Apr 27, 2024

Perhaps implementing fmt.Sringer interface helps with formatting for better logs. Explored it a little bit on playground. It still has some limitations, specially inside structs.

func (d dessert) String() string {
	switch d {
	case ICE_CREAM:
		return "ice cream"
	case CUP_CAKES:
		return "cup cakes"
	case GUMMY_WORMS:
		return "gummy worms"
	default:
		return strconv.Itoa(int(d))
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment