How to debug golang

angryTom
Release: 2020-02-15 10:24:12
Original
2967 people have browsed it

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

How to debug golang

You can use the GoLang debugging toolDelve

1. First get

go get -u github.com/go-delve/delve/cmd/dlv
Copy after login

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)) }
Copy after login

3. Start debug

dlv debug test.go
Copy after login

4. Breakpoint

1) For the method breakpoint:

b main.函数名
Copy after login

2) Run to the breakpoint:

c
Copy after login

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行打断点
Copy after login

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"); }
Copy after login

That’s it.

For more golang knowledge, please pay attention to thegolang tutorialcolumn 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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!