Home  >  Article  >  Operation and Maintenance  >  What is the method for installing Golang and related environment configuration on Linux?

What is the method for installing Golang and related environment configuration on Linux?

王林
王林forward
2023-05-27 21:22:241089browse

1. Download Golang

Go to the official website of golang.org and click on the "Downloads" page. You can select the Golang installation package for the version you need to download.

2. Install Golang

After the download is complete, unzip the installation package. Open the terminal and run the following command:

tar -C /usr/local -xzf go*.tar.gz

In this way, the file will be released to the /usr/local/go directory.

Configure environment variables:

In order to use Golang conveniently, you need to add environment variables so that the system can identify the location of the Golang installation directory.

1. Open the bashrc file

vim ~/.bashrc

2. Add environment variables

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

Save and exit, the settings will take effect

source ~/.bashrc

3. Verify the installation

Enter the following command in the terminal:

go version

If the go version can be displayed normally, it means that Golang has been installed successfully.

4. Sample code

1. Create a new folder and use vim to create a hello.go file and enter the following code:

package main

import "fmt"

func main() {
    fmt.Println("Hello, World!")
}

2. Save the file and exit, return to the terminal, use the following command to compile and run hello.go:

go build hello.go
./hello

3. The output result is:

Hello, World!

At this point, Golang is installed and configured successfully. You can Start writing Golang programs.

The above is the detailed content of What is the method for installing Golang and related environment configuration on Linux?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete