This is a creation in Article, where the information may have evolved or changed.
Devle is a great Golang debugging tool that supports a variety of debugging methods, runs debugging directly, or attach to a running Golang program for debugging.
Online Golang service problems, Devle is a lot of on-line debugging tools, if you use Docker, you can also put Devle into the Docker image, debug code.
Installing Devle
Installing Devle is simple, just run go get:
get -u github.com/derekparker/delve/cmd/dlv
If your go version is 1.5, set the environment variable go15vendorexperiment=1 before running go get. My go version is 1.10 without setting.
Debugging Golang Services with Devle
Write a simple Web service first, and then use Devle to Debug.
Create Main.go under the $gopath/src/github.com/mytest folder
1 Package Main2 3 Import (4 "FMT"5 "Log"6 "net/http"7 "OS"8 )9 Ten ConstPort ="8000" One A Func Main () { -http. Handlefunc ("/hi", HI) - theFmt. Println ("runing on port:"+Port) -Log. Fatal (http. Listenandserve (":"+port, nil)) - } - +Func Hi (w http. Responsewriter, R *http. Request) { -HostName, _: =OS. Hostname () +Fmt. fprintf (W,"HostName:%s", HostName) A}
Simply put, a Web service running on port 8000, Access Hi will return the name of the machine. The line number of the above code is very useful, and will be used when we break the point.
Use delve to run our Main.go
DLV Debug./main.go
You can enter help to see the documentation
A few simple commands.
Let's hit a breakpoint on the main method first:
b main.main
Then run C to run to the breakpoint,
To make a breakpoint in Func Li, we can use the
b Main.hi
or use "File: line number" To break the point
B/home/goworkspace/src/github.com/mytest/main.go:
Now execute the continue let the service run up. Visit our service to see if the Hi method will stop.
Curl localhost:8000/hi
See, stop at number 19th.
Enter n carriage, step into,
Input print (alias p) Output variable information
Enter args to print out all method parameter information
Enter locals to print all local variables
Other orders I will not be here to show you, let yourself try.
Debugging with delve attached to a running Golang service
Just compile our main.go and go to the main.
go build main.go. /main
Then use delve to attach to our project, first look at the PID of our project
PS Aux|grep Main
29260
Break the point in the Hi method and execute C to wait for the breakpoint to execute.
B/home/goworkspace/src/github.com/mytest/main.go:
Visit our server to see if the breakpoint will be executed
Curl localhost:8000/hi
The breakpoint was executed. Then debug your code!