Home >Backend Development >Golang >Detailed introduction to Golang deployment and debugging methods
In recent years, Golang has become an increasingly popular language and its applications have become more and more widespread, attracting the attention of many developers. However, for developers who are new to Golang, deployment and debugging are difficult problems to solve. This article will introduce the deployment and debugging methods of Golang in detail.
1. Deployment
Golang is a compiled language, and the written code needs to be compiled into an executable file before it can be run. There are two ways of compilation: static compilation and dynamic compilation. Static compilation will package the libraries required by the program into an executable file. The executable file is larger in size, but does not need to rely on external libraries at runtime; while dynamic compilation requires dynamic linking of library files at runtime, and the size of the executable file is Smaller, but requires dependence on external libraries.
Static compilation:
$ go build -o main main.go
Dynamic compilation:
$ go build -o main main.go -ldflags "-linkmode=external"
Among them, the -linkmode=external parameter indicates the library files required for dynamic linking.
In Golang, the default package management tool is go mod, so you need to set environment variables to specify the code storage path and download source. In Linux systems, environment variables can be set by modifying the ~/.bash_profile file.
export GO111MODULE=on
export GOPROXY=https://goproxy.cn,direct
export GOSUMDB=sum.golang.google.cn
Among them, GO111MODULE means using go mod;GOPROXY represents the download source used when go get; GOSUMDB represents verifying whether the downloaded code package is the officially released version.
Docker can help us package Golang programs into images for easy deployment on any platform. First you need to write a Dockerfile:
FROM golang:1.15.0-alpine
WORKDIR /app
COPY . .
RUN go build -o main .
CMD ["/app /main"]
Among them, FROM specifies the mirror source; WORKDIR specifies the working directory; COPY copies the files in the current folder to the mirror; RUN executes the compilation command; CMD specifies the run command.
Then execute the following command:
$ docker build -t myapp .
$ docker run -p 8080:8080 myapp
Among them, -t specifies the label name; -p specifies port mapping.
2. Debugging
Golang has a built-in set of debugging tools-go tool pprof, which can help us analyze CPU and memory usage Condition. First, you need to import the pprof package in the code and add the performance analysis code:
import (
"net/http" _ "net/http/pprof"
)
func main() {
go func() { http.ListenAndServe("0.0.0.0:6060", nil) }() // your code here
}
Among them, pprof does not need to be called when importing, it will automatically register the route. Then execute the following command:
$ go tool pprof http://localhost:6060/debug/pprof/profile
This command will print out the CPU usage analysis results in the console.
If an exception occurs in the program, you can use the debugger to debug it. There are many debuggers in Golang. Here are two types:
GDB debugger:
You need to specify the -g parameter to add debugging information when compiling, and then execute the following command:
$ gdb myapp
(gdb) break main.main
(gdb) run
Taking the breakpoint as an example, when the execution reaches the breakpoint, it will stop running and enter debugging mode.
Delve Debugger:
Delve is a powerful debugger that supports remote debugging and network debugging. First you need to install Delve:
$ go get -u github.com/go-delve/delve/cmd/dlv
Then execute the following command to start the debugger:
$ dlv debug myapp
This command will start the debugger and open a REPL (Read-Eval-Print Loop) interface, which can be used for debugging.
Summary
This article introduces the deployment and debugging methods of Golang: compilation, environment variables, Docker, pprof, GDB and Delve. I hope it can help developers better master Golang skills.
The above is the detailed content of Detailed introduction to Golang deployment and debugging methods. For more information, please follow other related articles on the PHP Chinese website!