How do I debug a program written in the Go language?

前端 未结 9 1112
予麋鹿
予麋鹿 2021-02-02 06:17

How do I debug a Go program? I have been using the Gedit Go IDE, but it doesn\'t have debugging. Is there a way to step though my code and inspect memory? Or am I stuck with

9条回答
  •  无人及你
    2021-02-02 06:48

    Perhaps some step by step instructions for getting started with GDB would help.

    I created silly.go containing:

    package main
    
    import "fmt"
    
    func x() {
        foo := 5
        fmt.Printf("foo: %v\n", foo)
    }
    
    func main() {
        go x()
        fmt.Printf("Done.\n")
    }
    

    After running 8g silly.go and 8l -o silly silly.8, I can run gdb silly. (I have "GNU gdb (Ubuntu/Linaro 7.2-1ubuntu11) 7.2" that as far as I know came with Ubuntu 11.04 32 bit.)

    I can then type list, b 7 (short for break 7), and run. It stops at line 7, and I can run:

    (gdb) p foo
    $1 = 5
    

    It would be interesting to see if the Eclipse/CDT debugger and/or DDD would work with Go.

提交回复
热议问题