How to debug golang

Release: 2019-12-28 10:29:40
Original
3683 people have browsed it

How to debug golang

We can use Devle to debug go programs.

Installing Devle is very simple, just run go get:

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

Use Devle to debug golang service

First write a simple web service, Then use Devle to debug.

Create main.go

package main

import (
    "fmt"
    "log"
    "net/http"
    "os"
)

const port  = "8000"

func main() {
    http.HandleFunc("/hi", hi)

    fmt.Println("runing on port: " + port)
    log.Fatal(http.ListenAndServe(":" + port, nil))
}

func hi(w http.ResponseWriter, r *http.Request) {
    hostName, _ := os.Hostname()
    fmt.Fprintf(w, "HostName: %s", hostName)
}
Copy after login

in the $GOPATH/src/github.com/mytest folder. A web service running on port 8000. Accessing hi will return the name of the machine. The line numbers of the above code are very useful and will be used later when we break points.

Use Delve to run our main.go

dlv debug ./main.go

How to debug golang

For more golang knowledge please Follow the golang tutorial column.

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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template