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

conflict behavior when comparing nil and (*SturctName)(nil) #35413

Closed
t57root opened this issue Nov 7, 2019 · 1 comment
Closed

conflict behavior when comparing nil and (*SturctName)(nil) #35413

t57root opened this issue Nov 7, 2019 · 1 comment

Comments

@t57root
Copy link

t57root commented Nov 7, 2019

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

$ go version
go version go1.13.3 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=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/root/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/lib/go-1.13"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go-1.13/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
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-build223059799=/tmp/go-build -gno-record-gcc-switches"

What did you do?

package main
import (
    "fmt"
)
type A struct {
}
func (a *A) Error() string {
    return "A"
}
func t() *A {
    return nil
}
func fun1 () {
    var a error

    a = (*A)(nil)
    fmt.Printf("%#v\n", a)
    if a == nil {
        fmt.Printf("nil#1.1\n")
    }
   if a == (*A)(nil) {
       fmt.Printf("nil#1.2\n")
   }

    a = t()
    fmt.Printf("%#v\n", a)
    if a == nil {
        fmt.Printf("nil#2.1\n")
    }
   if a == (*A)(nil) {
       fmt.Printf("nil#2.2\n")
   }
}
func fun2() {
    var a *A

    a = (*A)(nil)
    fmt.Printf("%#v\n", a)
    if a == nil {
        fmt.Printf("nil#1.1\n")
    }
   if a == (*A)(nil) {
       fmt.Printf("nil#1.2\n")
   }

    a = t()
    fmt.Printf("%#v\n", a)
    if a == nil {
        fmt.Printf("nil#2.1\n")
    }
   if a == (*A)(nil) {
       fmt.Printf("nil#2.2\n")
   }
}
func main() {
    fmt.Printf("* fun1\n")
    fun1()
    fmt.Printf("* fun2\n")
    fun2()
    if nil == (*A)(nil) {
        fmt.Printf("equal\n")
    }
}

What did you expect to see?

* fun1
(*main.A)(nil)
nil#1.1
nil#1.2
(*main.A)(nil)
nil#2.1
nil#2.2

* fun2
(*main.A)(nil)
nil#1.1
nil#1.2
(*main.A)(nil)
nil#2.1
nil#2.2

equal

What did you see instead?

* fun1
(*main.A)(nil)
nil#1.2
(*main.A)(nil)
nil#2.2

* fun2
(*main.A)(nil)
nil#1.1
nil#1.2
(*main.A)(nil)
nil#2.1
nil#2.2

equal
@t57root t57root closed this as completed Nov 7, 2019
@t57root t57root changed the title implicit conversion without error while assigning nil cross different pointer types conflict behavior when comparing nil and (*SturctName)(nil) Nov 7, 2019
@t57root t57root reopened this Nov 7, 2019
@zigo101
Copy link

zigo101 commented Nov 7, 2019

This is not a bug. It is a confusion many new gophers may hit.
https://golang.org/doc/faq#nil_error
#35292
#28133
https://go101.org/article/nil.html
https://go101.org/article/interface.html

@bradfitz bradfitz closed this as completed Nov 7, 2019
@golang golang locked and limited conversation to collaborators Nov 6, 2020
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

4 participants