There is no enum type at the moment in go, and there currently isn't a direct way to enforce the same rules as what typescript does.
A common practice in go is to use the suggestion posted by @ttrasn :
define a custom type, and typed constants with your "enum" values :
type EventName string
const (
NEW_USER EventName = "NEW_USER"
DIRECT_MESSAGE EventName = "DIRECT_MESSAGE"
DISCONNECT EventName = "DISCONNECT"
)
This allows you to flag, in your go code, the places where you expect such a value :
// example function signature :
func OnEvent(e EventName, id int) error { ... }
// example struct :
type ConnectionPayload struct {
EventName EventName `json:"eventName"`
EventPayload interface{} `json:"eventPayload"`
}
and it will prevent assigning a plain string
to an EventName
var str string = "foo"
var ev EventName
ev = str // won't compile
OnEvent(str, 42) // won't compile
The known limitations are :
- in go, there is always a zero value :
var ev EventName // ev is ""
- string litterals are not the same as typed variables, and can be assigned :
var ev EventName = "SOMETHING_ELSE"
- casting is allowed :
var str string = "foo"
var ev EventName = EventName(str)
- there is no check on unmarshalling :
jsn := []byte(`{"eventName":"SOMETHING_ELSE","eventPayload":"some message"}`)
err := json.Unmarshal(jsn, &payload) // no error
https://go.dev/play/p/vMUTpvH8DBb
If you want some stricter checking, you would have to write a validator or a custom unmarshaler yourself.