Go is a modern programming language developed by Google. It is becoming more and more popular in many applications and many companies, and provides a powerful set of libraries. This tutorial will guide you to download and install the latest version of Go (Go 1.10 at the time of publication of this article), and build a simple Hello World application.
This tutorial assumes that you can access the Ubuntu 18.04 system, which is configured with a non-root user with the permissions described in sudo
in the initial server setup of Ubuntu 18.04. Students who don’t have a server can buy it from here, but I personally recommend you to use the free Tencent Cloud Developer Lab for experimentation, and then buy server.
In this step, we will install Go on your server.
First, connect to your Ubuntu server via ssh
:
ssh sammy@your_server_ip
To install Go, you need to get the latest version from Official Go Download Page. On the website, you can find the URL and SHA256 hash of the current binary version of the tarball.
Visit the official Go download page and find the URL of the current binary version of the tarball and its SHA256 hash. Make sure you are currently in your home directory and use curl to retrieve the tarball:
cd ~
curl -O https://dl.google.com/go/go1.10.3.linux-amd64.tar.gz
Next, you can use sha256sum
to verify the tarball:
sha256sum go1.10.3.linux-amd64.tar.gz
go1.10.3.linux-amd64.tar.gz
fa1b0e45d3b647c252f51f5e1204aba049cde4af177ef9f2181f43004f901035 go1.10.3.linux-amd64.tar.gz
You will get a hash like the one highlighted in the output above. Make sure it matches the one on the download page.
Next, use tar
to extract the tarball. The x
flag tells tar
to decompress, v
tells it that we need a detailed output (a list of extracted files), and f
tells it that we will specify a file name:
tar xvf go1.10.3.linux-amd64.tar.gz
You should now have a directory called go
in your home directory. Recursively change the go
owner and group to root and move it to /usr/local
:
sudo chown -R root:root ./go
sudo mv go /usr/local
**Note: **Although /usr/local/go
is the officially recommended location, some users may prefer or need a different path.
In this step, we will set up some paths in your environment.
First, set the root value of Go and tell Go where to find its files.
sudo nano ~/.profile
At the end of the file, add the following line:
export GOPATH=$HOME/work
export PATH=$PATH:/usr/local/go/bin:$GOPATH/bin
If you choose an alternate installation location for Go, add these lines to the same file. This example shows the command to install Go in the home directory:
export GOROOT=$HOME/go
export GOPATH=$HOME/work
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
Paste the appropriate lines into your configuration file, save and close the file. Next, refresh your personal information by running
source ~/.profile
Now that Go is installed and the path is set for your server, you can test to make sure that Go works as expected.
Create a new directory for the Go workspace, and Go will build its files:
mkdir $HOME/work
Then, use this command to create a directory hierarchy in this folder to create test files. If you plan to use Git to submit and store Go code on GitHub, you can replace the value user with your GitHub username. If you do not plan to use GitHub to store and manage code, your folder structure may be different, such as ~/my_project
.
mkdir -p work/src/github.com/user/hello
Next, you can create a simple "Hello World" Go file.
nano ~/work/src/github.com/user/hello/hello.go
In the editor, paste the following code, use the main Go package, import the formatted IO content component, and set a new function to print "Hello, World" at runtime.
package main
import"fmt"
func main(){
fmt.Printf("hello, world\n")}
If it runs successfully, the program will print "hello, world", which will indicate that the Go program is being compiled correctly. Save and close the file, and then compile install
by calling the Go command:
go install github.com/user/hello
After compiling the file, just execute the following command to run:
hello
If the command returns "hello, world", Go has been successfully installed and is running normally. You can use the following which
command to view the location of the installed and compiled hello
binary:
which hello
/home/user/work/bin/hello
By downloading and installing the latest Go package and setting its path, you can now use a system for Go development.
For more Ubuntu tutorials, please go to [Tencent Cloud + Community] (https://cloud.tencent.com/developer?from=10680) to learn more.
Reference: "How To Install Go on Ubuntu 18.04"
Recommended Posts