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

== test reports false for the mismatched types? but why can it be compiled successfully? #40493

Closed
xgfone opened this issue Jul 30, 2020 · 1 comment

Comments

@xgfone
Copy link

@xgfone xgfone commented Jul 30, 2020

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

$ go version
go version go1.14.6 linux/amd64

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
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GONOPROXY="gitlab.sndaurl.cn"
GONOSUMDB="gitlab.sndaurl.cn"
GOOS="linux"
GOPATH="/root/gopath"
GOPRIVATE="gitlab.sndaurl.cn"
GOPROXY="https://goproxy.cn,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.google.cn"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build349278088=/tmp/go-build -gno-record-gcc-switches"

What did you do?

// main.go
package main

import (
	"fmt"
	"reflect"
)

func main() {
	var i interface{}

	i = uint(0)
	switch v := i.(type) {
	case uint, uint8, uint16, uint32, uint64:
		fmt.Println("111:", v == 0) // Why is v not equal to 0?? Because of mismatched types??
	case int, int8, int16, int32, int64:
		fmt.Println("222:", v == 0)
	default:
		panic(v)
	}

	// ------------------------------------------------------------------------ //

	switch vf := reflect.ValueOf(i); vf.Kind() {
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		fmt.Println("333:", vf.Int() == 0)
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
		fmt.Println("444:", vf.Uint() == 0)
	default:
		panic(i)
	}

	// v1 := int(0)
	// v2 := uint(1)
	// fmt.Println(v1 == v2) // Cannot compile because their types are mismatched.
}

What did you expect to see?

$ go run main.go
111: true
444: true

What did you see instead?

$ go run main.go
111: false
444: true
@davecheney
Copy link
Contributor

@davecheney davecheney commented Jul 30, 2020

Smaller reprodution

package main

import (
	"fmt"
)

func main() {
	var v interface{} 
	v = uint(0)
	switch v.(type) {
	case uint, int:
		fmt.Println(v == 0)
	}
}

the answer is because v is of type uint while 0 is of type int.

Thank you for raising this issue. Unlike many projects, the Go project does not use GitHub Issues for general discussion or asking questions. GitHub Issues are used for tracking bugs and proposals only.

For asking questions, see:

@davecheney davecheney closed this Jul 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.