4 ms·
Your example wouldn't work for function param types, right? The following code compiles even though I'm not passing a valid flag ID: type FlagID string
by goodoldneon 4y ago
Your example wouldn't work for function param types, right? The following code compiles even though I'm not passing a valid flag ID:
type FlagID string
var Constants = struct {
FooBar FlagID
FizzBuzz FlagID
}{
"FooBar",
"FizzBuzz",
}
func p(flag FlagID) {
fmt.Println(flag)
}
func main() {
p("hi")
}
- xaduha 4y agoYou're right and OP mentioned it at the start too. I guess your editor would show you what is expected there and that should be good enough for most cases. If it is some sort of a library though, then there should be a better way to do this that to pass some enums as parameters to functions. But yes, it is a shortcoming that Go should probably fix.
- goodoldneon 4y agoThis type of thing has been surprising to me as I've learned Go. It seems like Go makes a great effort around safety in a lot of ways, but sometimes will avoid safety and say "just be careful" instead