Home >Backend Development >Golang >Linux installation of Golang and related environment configuration

Linux installation of Golang and related environment configuration

PHPz
PHPzOriginal
2023-03-22 13:49:162037browse

Under Linux system, it is very simple to install Golang and configure environment variables. This article will introduce how to install Golang and related environment configuration in Linux system.

1. Download Golang

Open the official website golang.org and enter the "Downloads" page. Select the corresponding version. After selecting, you can download the corresponding version of the Golang installation package.

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 Linux installation of Golang and related environment configuration. 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