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

Linux installation of Golang and related environment configuration

PHPz
Release: 2023-03-22 14:09:50
Original
2035 people have browsed it

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
Copy after login

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
Copy after login

2. Add environment variables

export PATH=$PATH:/usr/local/go/bin
Copy after login

Save and exit, the settings will take effect

source ~/.bashrc
Copy after login

3. Verify the installation

Enter the following command in the terminal:

go version
Copy after login

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!")
}
Copy after login

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
Copy after login

3. The output result is:

Hello, World!
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template