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
Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (go env)?
go env Output
$ go env
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOROOT="/usr/lib/golang"
I ignored some irrelevant information.
What did you do?
// ConvertFloat64 convert any type to float64 if it can.
func ConvertFloat64(v interface{}) (ret float64, err error) {
switch val := v.(type) {
case float64:
return val, nil
case float32:
return float64(val), nil
case uint, uint8, uint16, uint32, uint64, int, int8, int16, int32, int64:
return float64(val), nil
default:
err = fmt.Errorf("unsupported value to convert to float64,%v", v)
}
return
}
When I use multiple-cases in type swtich, error will occur: cannot convert val (type interface {}) to type float64: need type assertion
Line 9. When I switch to single case, error won't occur.
I want to know why I can't merge these case branch.
Thank you.
What did you expect to see?
What did you see instead?
The text was updated successfully, but these errors were encountered:
// ConvertFloat64 convert any type to float64 if it can.
func ConvertFloat64(v interface{}) (ret float64, err error) {
switch val := v.(type) {
case float64:
return val, nil
case float32:
return float64(val), nil
case uint:
return float64(val), nil
case uint8:
return float64(val), nil
case uint16:
return float64(val), nil
case uint32:
return float64(val), nil
case uint64:
return float64(val), nil
case int:
return float64(val), nil
case int8:
return float64(val), nil
case int16:
return float64(val), nil
case int32:
return float64(val), nil
case int64:
return float64(val), nil
default:
err = fmt.Errorf("unsupported value to convert to float64,%v", v)
}
return
}
then if the case has multiple types, the type of val is interface{}. And Go does not permit writing float64(val) if val has type interface{}.
It's true that float64(val) can be compiled for every type in the case, but since Go is statically typed that could only be done if the compiler quietly duplicated the case for every type. The language requires that if that is what you want to do, you must do it explicitly.
The TypeSwitchGuard may include a short variable declaration. When that form is used, the variable is declared at the end of the TypeSwitchCase in the implicit block of each clause. 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.
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputI ignored some irrelevant information.
What did you do?
When I use multiple-cases in type swtich, error will occur:
cannot convert val (type interface {}) to type float64: need type assertion
Line 9. When I switch to single case, error won't occur.
I want to know why I can't merge these case branch.
Thank you.
What did you expect to see?
What did you see instead?
The text was updated successfully, but these errors were encountered: