Home  >  Article  >  Backend Development  >  golang installation and use

golang installation and use

王林
王林Original
2023-05-16 15:07:081173browse

Golang is an open source programming language whose design goal is to provide a simple, efficient, safe, and concurrent programming experience. It has important applications in network programming, distributed systems, cloud computing, big data processing and other fields. This article explains how to install and use Golang on various operating systems.

1. Installation on Windows system

  1. Download the installation package

The official website of Golang is https://golang.org, which can be found on the website Download the corresponding installation package. On Windows systems, we can download the latest version of Golang from the following link.

https://golang.org/dl/go1.16.5.windows-amd64.msi

  1. Installation

After the download is complete, double-click to run Install the package to start the installation. During the installation process, you can select the installation path and add environment variables for quick calls in the command line:

  1. Verify installation

After the installation is completed, open the command line, Enter the following command:

$ go version

If the output is similar to the following information, the installation is successful:

go version go1.16.5 windows/amd64

2. Installation on Linux system

  1. Download the installation package

Similarly, we can download the installation of Golang on Linux at https://golang.org/dl/ Bag. Taking the Debian/Ubuntu system as an example, download the latest version of Golang:

https://golang.org/dl/go1.16.5.linux-amd64.tar.gz

  1. Decompression

After the download is complete, decompress the installation package. You can choose to decompress it to the /usr/local directory:

$ tar -C /usr/local -xzf go1.16.5.linux-amd64.tar.gz

  1. Add environment variables

Add the following content in the ~/.profile or ~/.bashrc file:

export PATH=$PATH:/usr/local/go/bin

  1. Verify installation

Open a new terminal and enter the following command:

$ go version

If the output is similar to the following information, the installation is successful:

go version go1.16.5 linux/amd64

3. Installation on Mac OS system

  1. Download the installation package

Similarly, you can download the latest version of Golang For Mac OS X at https://golang.org/dl/:

https://golang.org/dl/go1.16.5.darwin-amd64.pkg

  1. Installation

Just run the installation package to install.

  1. Configure environment variables

Open the terminal, run the following command, open the ~/.bash_profile file:

$ open ~/.bash_profile

Add the following content to the opened file:

export PATH=$PATH:/usr/local/go/bin

After saving, run the following command to take effect:

$ source ~/.bash_profile

  1. Verify installation

Open the terminal and enter the following command:

$ go version

If the output is similar to the following information, the installation is successful:

go version go1.16.5 darwin/amd64

4. Use of Golang

After the installation is completed, we can use Golang Write and run the program. In Golang, a program consists of multiple packages, one of which must be the main package, and the file name of this package is main.go. The main() function in the main package is the entry point of the program.

The following is a simple hello world program:

package main

import "fmt"

func main() {

fmt.Println("Hello, world!")

}

Compile the program using the following command:

$ go build main.go

Then run:

$ ./main

Output information similar to the following:

Hello, world!

Of course, Golang has many powerful functions and features. Here is a brief introduction to the installation and most basic use. If you want to learn more about Golang, it is recommended to learn from the official documentation.

The above is the detailed content of golang installation and use. 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
Previous article:linux settings golangNext article:linux settings golang