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

前端 未结 9 1116
予麋鹿
予麋鹿 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:44

    New initiative (started May 2014): derekparker/delve

    Delve is a Go debugger, written in Go.
    (mainly for Linux though, OsX support is coming, Windows support unknown supported in 2016)

    Features

    • Attach to an already running process
    • Launch a process and begin debug session
    • Set breakpoints, single step, step over functions, print variable contents

    Usage

    The debugger can be launched in three ways:

    Compile, run, and attach in one step:

    $ dlv -run
    

    Provide the name of the program you want to debug, and the debugger will launch it for you.

    $ dlv -proc path/to/program
    

    Provide the pid of a currently running process, and the debugger will attach and begin the session.

    $ sudo dlv -pid 44839
    

    Breakpoints

    Delve can insert breakpoints via the breakpoint command once inside a debug session, however for ease of debugging, you can also call runtime.Breakpoint() and Delve will handle the breakpoint and stop the program at the next source line.

提交回复
热议问题