Today I will share with you an article about how to debug golang. It has a very good reference value. Friends who need it can take a look.
How to debug golang
You can use the GoLang debugging toolDelve
1. First get
go get -u github.com/go-delve/delve/cmd/dlv
2. Write test code
func main(){ http.HandleFunc("/test",func(writer http.ResponseWriter,req *http.Request){ //TODO }) log.Fatal(http.ListenAndServe("127.0.0.1:8080",nil)) }
3. Start debug
dlv debug test.go
4. Breakpoint
1) For the method breakpoint:
b main.函数名
2) Run to the breakpoint:
c
3) For Break point of a certain line:
Need to get the location of the file and then break point:
b /Users/joker/go/src/…/test.go:14 对14行打断点
As for how to perform the same operation as on the ide: through n,s
n: Equivalent to F6 of Java-Eclipse
s: Equivalent to F5 of Java-Eclipse
How to view variables:p
p testName will output the value of testName
args: will output all method parameter information
locals: will output all local variables
Use Go play Debug, Info, Error level log
Directly upload the code:
package mylog import ( "log" "os" ) var ( Debug *log.Logger Info *log.Logger Error *log.Logger ) func init() { log.Println("init ...") Debug = log.New(os.Stdout, "[DEBUG] ", log.Ldate|log.Ltime|log.Lshortfile) Info = log.New(os.Stdout, "[INFO] ", log.Ldate|log.Ltime|log.Lshortfile) Error = log.New(os.Stderr, "[ERROR] ", log.Ldate|log.Ltime|log.Lshortfile) } package main import "mylog" func main(){ mylog.Debug.Println("good"); mylog.Info.Println("good"); mylog.Error.Println("good"); }
That’s it.
For more golang knowledge, please pay attention to the golang tutorial column on the PHP Chinese website.
The above is the detailed content of How to debug golang. For more information, please follow other related articles on the PHP Chinese website!