The Golang command line performs debug debugging

  • 2020-06-23 00:39:54
  • OfStack

GoLang debugging tool Delve

1. Get it 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. Start debug:


dlv debug test.go

4. Interrupt points:

4.1): Point of interruption of method:

b main. Function name

4.2): Run to the breakpoint:

c

4.3: Break point for a certain line:

Need to get the location of the file again:

[

b/Users/joker/go src /... test. go:14 to 14 line break points

]

How do you do operation 1 on ide: via n,s

n: Equivalent to F6 of ES48en-ES49en s: Equivalent to F5 of ES52en-ES53en

How to view variables :p

p testName prints the value of testName

args: prints all method parameter information

locals: prints all local variables

emmmmmmmm golang's command line compilation feels inconvenient, so find a way to get it on ide

Play Debug, Info, Error level log with go

Direct 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 all.

conclusion


Related articles: