Home>Article>Backend Development> How to debug golang
Devle is a great golang debugging tool that supports multiple debugging methods. You can run the debugging directly, or attach to a running golang program for debugging.(Recommended learning:go)
When there is a problem with the online golang service, Devle is an essential online debugging tool. If you use docker, you can also enter Devle into docker In the image, debug the code.
Installing Devle
Installing Devle is very simple, just run go get:
go get -u github.com/derekparker/delve/cmd/dlv
If your go version For 1.5, please set the environment variable GO15VENDOREXPERIMENT=1 before running go get. My go version is 1.10, no need to set it.
Use Devle to debug golang services
First write a simple web service, and then use Devle to debug it.
Create main.go in the $GOPATH/src/github.com/mytest folder
package main import ( "fmt" "log" "net/http" "os" ) const port = "8000" func main() { http.HandleFunc("/hi", hi) fmt.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) }
Simple, a web service running on port 8000, access hi Returns the name of the machine. The line numbers of the above code are very useful and will be used later when we break points.
The above is the detailed content of How to debug golang. For more information, please follow other related articles on the PHP Chinese website!