Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

type switch fails when multiple cases are put together #64421

Closed
bo-er opened this issue Nov 28, 2023 · 1 comment
Closed

type switch fails when multiple cases are put together #64421

bo-er opened this issue Nov 28, 2023 · 1 comment

Comments

@bo-er
Copy link

bo-er commented Nov 28, 2023

Go version

go version go1.21.3 linux/amd64

What operating system and processor architecture are you using (go env)?

GO111MODULE='on'
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/steve/.cache/go-build'
GOENV='/home/steve/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE='actiontech.cloud'
GOMODCACHE='/home/steve/go/pkg/mod'
GONOPROXY='actiontech.cloud'
GONOSUMDB='actiontech.cloud'
GOOS='linux'
GOPATH='/home/steve/go'
GOPRIVATE='actiontech.cloud'
GOPROXY='goproxy.cn,goproxy.io,direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.21.3'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/home/steve/actiontech/Diagno/dms-diagno/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build376176395=/tmp/go-build -gno-record-gcc-switches'

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

@mateusz834
Copy link
Member

mateusz834 commented Nov 28, 2023

This works as expected. Consider the spec https://go.dev/ref/spec#Type_switches and the example there.

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.

@mateusz834 mateusz834 closed this as not planned Won't fix, can't repro, duplicate, stale Nov 28, 2023
@golang golang locked and limited conversation to collaborators Nov 27, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants