Home >Backend Development >Golang >golang installation and use
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
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
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:
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
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
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
Add the following content in the ~/.profile or ~/.bashrc file:
export PATH=$PATH:/usr/local/go/bin
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
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
Just run the installation package to install.
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
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!