How to debug when developing in golang

angryTom
Release: 2020-03-12 16:53:01
Original
1868 people have browsed it

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
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. Debug startup

dlv debug test.go
Copy after login

4. Break point

4.1): Break point for method:

b main.函数名
Copy after login

4.2): Run to break point:

c
Copy after login

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

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

: p

p testName 则会输出testName的值
args:则会输出所有方法参数信息
locals:则会输出所有的本地变量
Copy after login

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

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!

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
Popular Tutorials
More>
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!