Home > Article > Backend Development > How to debug when developing in golang
How to debug during golang development
1. Get the Delve tool first
go get -u github.com/derekparker/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. Debug startup
dlv debug test.go
4. Break point
4.1): Break point for method:
b main.函数名
4.2): Run to break point:
c
4.3: Break point for a certain line:
Need to get the file Break point at the position:
b /Users/joker/go/src/…/test.go:14 对14行打断点
As for how to perform the same operation as on the ide: How to view the variables through n,s
n:相当于Java-Eclipse的F6 s:相当于Java-Eclipse的F5
: p
p testName 则会输出testName的值 args:则会输出所有方法参数信息 locals:则会输出所有的本地变量
Use go to play Debug, Info, and Error level logs
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 when developing in golang. For more information, please follow other related articles on the PHP Chinese website!