Skip to content

Type switch doesn't support multiple-cases? #36616

Closed
@kiwi-yan

Description

@kiwi-yan

What version of Go are you using (go version)?

$ go version
1.13.3

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?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions