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

runtime: Panic, when passing anonymous field, which is a pointer to a named type #20907

Closed
vbauerster opened this issue Jul 5, 2017 · 2 comments

Comments

Projects
None yet
3 participants
@vbauerster
Copy link

commented Jul 5, 2017

Please answer these questions before submitting your issue. Thanks!

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

go version go1.8.3 darwin/amd64

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

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/vbauer/gocode"
GORACE=""
GOROOT="/usr/local/Cellar/go/1.8.3/libexec"
GOTOOLDIR="/usr/local/Cellar/go/1.8.3/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/5l/t0s3df6d2w56fq0bhc9tk0k40000gn/T/go-build151792197=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"
PKG_CONFIG="pkg-config"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"

What did you do?

This code works well, while this one panics. Both compiles without errors.

What did you expect to see?

I expected both code samples to work the same way, because the difference is only the way kPerson structure is declared.

What did you see instead?

Panic when running the second code sample:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0xffffffff addr=0x0 pc=0xc849a]

goroutine 1 [running]:
main.main()
	/tmp/sandbox393635792/main.go:17 +0x1a
@zegl

This comment has been minimized.

Copy link
Contributor

commented Jul 5, 2017

When you use var kp kPerson the *Person field is initialised to the default value of a pointer, which is nil.

You need to add kp.Person = &Person{} to get the panicing version to work as you expect it to. Full example.

@vbauerster

This comment has been minimized.

Copy link
Author

commented Jul 5, 2017

Ok, I see now. Thanks for pointing this out.

@vbauerster vbauerster closed this Jul 5, 2017

@mikioh mikioh changed the title Panic, when passing anonymous field, which is a pointer to a named type runtime: Panic, when passing anonymous field, which is a pointer to a named type Jul 10, 2017

@golang golang locked and limited conversation to collaborators Jul 10, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.