Go: print variables with gdb

In this program, how can I break execution with the debugger and print the value of i?

package main import "fmt" func main() { x := "abc" i := 3 fmt.Println(i) fmt.Println(x) } 

I can not print i. However, I can print x:

 go build test.go gdb test [...]Reading symbols from /home/ned/test...done. (gdb) br 9 (gdb) run (gdb) pi No symbol "i" in current context. (gdb) px $1 = "abc" 
+4
source share
1 answer

It looks like the variable i was probably optimized due to the existence of the compiler. Have you tried building debugging?

You can use go build -gcflags '-N' .

+3
source

Source: https://habr.com/ru/post/1413633/


All Articles