I like creating new types like that
type Category string
const (
CategoryA Category = "A"
CategoryB Category = "B"
...
)
type Object struct {
Cat Category `json:"cat"`
}
I think this is a great thing to do when we have a limited choice of values for a variable. But I have some trouble to validate this kind of type because someone can do something like this (especially when we talk about REST API where the client doesn't have the server code):
s := Objecy{
Cat : Category("not a valid category")
}
Is there a way to check the value of Object.Cat
without using :
if s.Cat != CategoryA || s.Cat != CategoryB || ... {
return false
}
Thanks for your answers