Home >Backend Development >Golang >How to install and deploy golang
This article will introduce how to install and deploy golang. Golang is a high-performance programming language that improves program performance by optimizing compilation and concurrent processing. Therefore, it is widely used by many enterprises and open source projects. Below, we will introduce how to install and deploy golang.
1. Install golang
golang can run on a variety of operating systems, such as Linux, Windows and MacOS. Here, we take Ubuntu 20.04 as an example to demonstrate how to install golang.
sudo apt update
sudo apt install golang
go version
2. Configure the golang environment
After successfully installing golang, you need to configure the environment. The specific operations are as follows.
GOPATH is the default working directory of golang, which contains the source code, packages, executable files, etc. of the go project. In order for golang to run normally, the GOPATH environment variable needs to be added.
export GOPATH=$HOME/go export PATH=$PATH:$GOPATH/bin
GOMODULES is golang's dependency management tool. Use it to easily manage project dependencies. Before configuring GOMODULES, you need to enable it.
export GO111MODULE=on
The next step is to configure GOMODULES.
First, open the terminal, enter the project directory, and enter the following command to initialize the module:
go mod init modulename
Then, enter the following command to download the dependencies:
go mod tidy
3. Write golang Program
The source code file of golang has .go as the file suffix. Enter the following command in the terminal to create a golang program:
mkdir demo && cd demo touch main.go
Edit the main.go file and enter the following code:
package main import "fmt" func main() { fmt.Println("Hello, world!") }
Save the file.
4. Run the golang program
Enter the following command in the terminal and run the program:
go run main.go
It will output the following results:
Hello, world!
5. Deploy golang Program
To deploy the golang program, you need to write a Dockerfile file to specify the environment of the program.
First, create a Dockerfile file in the project directory and enter the following content:
FROM golang:alpine RUN apk add --no-cache git WORKDIR /app COPY go.mod go.sum ./ RUN go mod download COPY . . RUN go build -o main . EXPOSE 8080 CMD ["./main"]
Explanation of the above Dockerfile file:
Next, build the container and enter the following command:
docker build -t my-golang-app .
This will build a container named my-golang-app.
Finally, start the container and enter the following command:
docker run -p 8080:8080 my-golang-app
This container will expose port 8080 to port 8080 of the local host.
To sum up, this article introduces the installation, configuration, programming and deployment of golang. As a high-performance programming language, golang is worth learning and using.
The above is the detailed content of How to install and deploy golang. For more information, please follow other related articles on the PHP Chinese website!