Skip to content

Instantly share code, notes, and snippets.

@GeertJohan
Created December 10, 2013 10:25
Show Gist options
  • Save GeertJohan/7888559 to your computer and use it in GitHub Desktop.
Save GeertJohan/7888559 to your computer and use it in GitHub Desktop.

panic stacktrace inlined vs not-inlined.

For this code:

package main

var x map[string]string

func add(a, b string) {
	x[a] = b
}

func main() {
	add("foo", "bar")
}

This is the output:

panic: runtime error: assignment to entry in nil map

goroutine 1 [running]:
runtime.panic(0x422a20, 0x45a2dd)
	/home/geertjohan/Applications/go/src/pkg/runtime/panic.c:266 +0xb6
main.main()
	/home/geertjohan/Workspaces/Go/devpath/src/github.com/GeertJohan/ango/bla/stuff.go:10 +0x65

Whereas for this code:

package main

import (
	"fmt"
)

var x map[string]string

func add(a, b string) {
	hello := "hello, world"
	fmt.Println(hello)
	x[a] = b
}

func main() {
	add("foo", "bar")
}

This is the output:

hello, world
panic: runtime error: assignment to entry in nil map

goroutine 1 [running]:
runtime.panic(0x499d00, 0x56679d)
	/home/geertjohan/Applications/go/src/pkg/runtime/panic.c:266 +0xb6
main.add(0x4aeb20, 0x3, 0x4adf80, 0x3)
	/home/geertjohan/Workspaces/Go/devpath/src/github.com/GeertJohan/ango/bla/stuff.go:12 +0xf5
main.main()
	/home/geertjohan/Workspaces/Go/devpath/src/github.com/GeertJohan/ango/bla/stuff.go:16 +0x4b
@GeertJohan
Copy link
Author

Note the extra stack frame showing the add function

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment