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

nil pointer but func can be called #41639

Closed
PolarPanda611 opened this issue Sep 26, 2020 · 1 comment
Closed

nil pointer but func can be called #41639

PolarPanda611 opened this issue Sep 26, 2020 · 1 comment

Comments

@PolarPanda611
Copy link

@PolarPanda611 PolarPanda611 commented Sep 26, 2020

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

$ go version

go version go1.15 darwin/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="/Users/danieltan/Library/Caches/go-build"
GOENV="/Users/danieltan/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/danieltan/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/danieltan/go"
GOPRIVATE=""
GOPROXY="https://goproxy.cn,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
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 -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/57/w33ygw3s19d8mxctbm1gzjv00000gn/T/go-build484192535=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

package main

import (
	"fmt"
)

type Door struct {
	ID string
}

func (d *Door) Open() {
	fmt.Println("Open")
}

type Car struct {
	Door *Door
}

func (c *Car) OpenDoor() {
	c.Door.Open()
}

func main() {
	c := new(Car)
	c.Door.Open()
	// 	c.Door.ID = "test"
}

With this code , the c.Door is not initial , the c.Door.Open() can be called .
when do the c.Door.ID = "test" , it is nil pointer error

What did you expect to see?

when call c.Door.Open() , should be nil pointer error .

What did you see instead?

the c.Door.Open() can be called

@davecheney
Copy link
Contributor

@davecheney davecheney commented Sep 26, 2020

Thank you for raising this issue. This is working as intended. In Go it is perfectly valid to call a method on a nil receiver. This is by design and not a bug.

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 Sep 26, 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.