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
In clauses with a case listing exactly one type, the variable has that type; otherwise, the variable has the type of the expression in the TypeSwitchGuard.
Go version
go version go1.21.3 linux/amd64
What operating system and processor architecture are you using (
go env
)?What did you do?
`type Value struct {
floatValue float64
intValue int64
stringValue string
}
func NewValue(v any) *Value {
switch nv := v.(type) {
case string:
return &Value{
stringValue: nv,
}
case int64, int, uint:
return &Value{
intValue: int64(nv),
}
case float64:
return &Value{
floatValue: nv,
}
}
return &Value{}
}`
The above code fails to compile due to error:
cannot convert nv (variable of type any) to type int64: need type assertion
What did you expect to see?
I expect the case:
case int64, int, uint: return &Value{ intValue: int64(nv), }
to be treated as:
case int64: return &Value{ intValue: int64(nv), } case int: return &Value{ intValue: int64(nv), } case uint: return &Value{ intValue: int64(nv), }
What did you see instead?
The compiler complains about syntax error:
cannot convert nv (variable of type any) to type int64: need type assertion
The text was updated successfully, but these errors were encountered: