Home  >  Article  >  Backend Development  >  How to install and deploy golang

How to install and deploy golang

PHPz
PHPzOriginal
2023-04-24 09:11:40685browse

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.

  1. Open the terminal and enter the following command:
sudo apt update
  1. Install golang
sudo apt install golang
  1. Check the golang version
go version

2. Configure the golang environment

After successfully installing golang, you need to configure the environment. The specific operations are as follows.

  1. Add GOPATH

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
  1. Add GOMODULES

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:

  1. Use the alpine image as the basis Mirror;
  2. Install git;
  3. Create the folder /app, and copy the go.mod and go.sum files to the /app directory;
  4. Download dependencies;
  5. Copy the application source code to the /app directory;
  6. Build the application;
  7. Expose port 8080;
  8. Run the application.

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!

Statement:
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