You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It is a bit confusing, but the compiler is doing the right thing here. In the first code snippet, value == 0 is false because value has concrete type uint, but 0 has type int. For interfaces to compare equal, both their type and their concrete value must be the same.
Two other ways to write this program to avoid this problem:
https://play.golang.org/p/a_d97KPZUn -- make value be a concrete type, not an interface, so the compiler treats 0 as type uint to match the type of value
I think in general you probably want to use the second form.
The more common way to write such a switch is to introduce a variable with the type switch: https://play.golang.org/p/93shxyhA_Z and then the code is less surprising.
Please answer these questions before submitting your issue. Thanks!
What version of Go are you using (
go version
)?Latest from play.golang.org
What operating system and processor architecture are you using (
go env
)?Mac OS
What did you do?
I do comparison of uint value inside switch by type.
And I got different result for same code:
https://play.golang.org/p/MGQjG9ij4y and https://play.golang.org/p/kUgwdQrOe5
What did you expect to see?
I expect same result from both commands.
What did you see instead?
I see different results.
I think that compiler should notify me about this issue or restrict it completely because it's very unexpected behaviour from code.
The text was updated successfully, but these errors were encountered: