Home > Article > Backend Development > Guide to installing golang in Ubuntu
Golang installation tutorial in Ubuntu environment, specific code examples are required
Overview:
Golang (also known as Go) is an open source programming language, developed by Developed by Google. It is widely popular for its concise syntax, efficient performance and powerful tool chain. This article will introduce the detailed steps to install golang on the Ubuntu operating system and provide specific code examples.
Step 1: Install Golang
Open the terminal (Ctrl Alt T) and enter the following command to download the golang installation package:
wget https://golang.org/dl/go1.16.7.linux-amd64.tar.gz
Extract the installation package:
tar -xvf go1.16.7.linux-amd64.tar.gz
Move the decompressed folder to the /usr/local
directory:
sudo mv go /usr/local
Configure environment variables. Open a terminal and enter the following command to edit the ~/.profile
file:
nano ~/.profile
Add the following two lines at the end of the file:
export GOPATH=$HOME/go export PATH=$PATH:/usr/local/go/bin:$GOPATH/bin
Save and close the file, then enter the following command to update the environment variables:
source ~/.profile
Verify that Golang is installed successfully. Enter the following command in the terminal:
go version
If something similar to the following is displayed, the installation is successful:
go version go1.16.7 linux/amd64
Step 2: Write and run the sample code
Now, We will write a simple example code to verify that Golang is installed correctly.
Open a text editor and create a file named hello.go
;
nano hello.go
in the file Enter the following code:
package main import "fmt" func main() { fmt.Println("Hello, World!") }
Save and close the file. In the terminal, go to the directory containing the hello.go
file and enter the following command to compile the code:
go build hello.go
Run the resulting executable:
./hello
If everything goes well, you will see the following output in the terminal:
Hello, World!
Summary:
Install and configure Golang in Ubuntu operating system by following the above steps , we were able to successfully write and run the sample code. Golang has become the language of choice for many developers due to its simplicity, efficiency and scalability. Hopefully this article has provided you with a clear guide to easily start developing your projects using golang.
The above is the detailed content of Guide to installing golang in Ubuntu. For more information, please follow other related articles on the PHP Chinese website!