Home > Article > Backend Development > How to install and configure Golang on Ubuntu system
In development, it is sometimes necessary to use different programming languages. In recent trends, Golang is widely used. It is a powerful programming language for building high-performance and scalable applications. In this article, we will discuss how to install Golang on Ubuntu operating system.
Golang is a powerful programming language also known as Go. It was developed by Google and specifically designed for building scalable web applications. Golang has many features, including fast compilation speed, ease of learning, static compilation, and more. If you want to write applications using Golang, here are the steps on how to install it on Ubuntu operating system.
Step 1: Update your Apt repository
Before installing Golang, you must first ensure that the APT repository of the Ubuntu operating system is up to date. To do this, run the following command in the terminal:
sudo apt update
This will update your APT repository and ensure that the latest version of the package can be installed.
Step 2: Install Golang
Before installing Golang, you need to download it from the official website (https://golang.org/dl/) Golang binaries. Find the version you need from the download page and copy its download link.
In the terminal, run the following command to download the Golang binary:
wget https://golang.org/dl/go1.17.1.linux-amd64.tar.gz
Please note that "go1.17.1.linux-amd64.tar.gz" is the name and version of the Golang binary . You can change the version to download the version you need.
After the download is complete, run the following command in the terminal to extract the binary file in "/usr/local":
sudo tar -C /usr/local -xzf go1.17.1.linux-amd64.tar.gz
This will extract the Golang binary file and place it in "/ usr/local" folder.
Step 3: Set Golang Path
Once you have successfully installed Golang, the next step is to set its environment variables so that you can use it in the terminal Golang commands.
Run the following command in the terminal to create a new script file in the "/etc/profile.d" directory:
sudo nano /etc/profile.d/golang.sh
Add the following content to the file:
export PATH=$PATH:/usr/local/go/bin export GOPATH=$HOME/go export GOBIN=$GOPATH/bin
Save and exit the file, then run the following commands for the changes to take effect:
source /etc/profile.d/golang.sh
Step 4: Test the Golang installation
Now you have successfully installed Golang, and set the path. You can test whether it works properly.
In the terminal, run the following command to create a new Golang project:
mkdir ~/goprojects cd ~/goprojects go mod init example.com/hello
This will initialize a Golang module named "hello".
Now you can create a new file in the "goprojects" folder, name it "hello.go" and add the following content to the file:
package main import "fmt" func main() { fmt.Println("Hello, World!") }
Save and exit the file . Finally, run the following command in the terminal to build and run the "hello" application:
go run hello.go
The output will be:
Hello, World!
Congratulations! You have successfully installed and tested Golang. You can now write interesting applications using Golang and push its limits.
The above is the detailed content of How to install and configure Golang on Ubuntu system. For more information, please follow other related articles on the PHP Chinese website!