Home  >  Article  >  Backend Development  >  linux settings golang

linux settings golang

WBOY
WBOYOriginal
2023-05-16 15:03:371158browse

Linux system has become the mainstream operating system in the cloud era. Its open source, security and stability are deeply loved by the majority of users. As a fast, efficient and highly concurrency programming language, Golang is increasingly favored by developers in Linux systems. This article will introduce you to how to set up Golang on a Linux system.

1. Install Golang

Installing Golang in a Linux system is very simple. First, we need to download the Golang installation package from the official website (https://golang.google.cn/dl/). After downloading, use the following command to decompress the installation package:

tar -C /usr/local -xzf go1.16.x.linux-amd64.tar.gz

Among them, x represents the version number of Golang, which may be different. After decompression is completed, the installation of Golang is completed.

2. Configure environment variables

After installing Golang, we need to configure Golang’s environment variables. Enter the following command on the command line:

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

This command will add the /usr/local/go/bin directory to the PATH environment variable. In this way, we can use the go command in any directory.

To avoid having to manually execute the above command every time you open the command line, we can add it to the .bashrc file. Enter the following command in the terminal:

echo 'export PATH=$PATH:/usr/local/go/bin' >> ~/.bashrc

This command will add the setting of the PATH environment variable to the .bashrc file, so that this command will be automatically executed every time you open the terminal.

3. Verify whether Golang has been successfully installed

In order to verify whether our Golang has been successfully installed, we can enter the following command in the terminal:

go version

If displayed in the terminal If you know the currently installed Golang version number, it means that our Golang has been configured.

4. Start using Golang

Golang is a very excellent programming language. Its syntax is simple, easy to read and write, and it supports concurrent programming. For the purpose of popular science, here we briefly introduce some common commands in Golang.

  1. Compile and run the Go program through the go run command
go run main.go

This command will automatically compile our Go program and run it.

  1. Generate an executable file through the go build command
go build main.go

This command will compile our Go program into the executable file main and save it in the current Under contents. We can run the executable file through the following command:

./main
  1. Compile and install the Go program through the go install command
go install example.com/hello

This command will automatically compile and install example.com/hello package and install it into the $GOPATH/bin directory.

5. Summary

This article mainly introduces how to set up the Golang environment on the Linux system, and how to use Golang to write and run our programs. Golang's efficiency, simplicity, and ease of use bring us unlimited programming fun. Hope this article can help you.

The above is the detailed content of linux settings golang. 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