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

王林
Release: 2023-05-27 21:22:24
forward
1095 people have browsed it

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
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 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!

Related labels:
source:yisu.com
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