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 Go 1.6 and build a simple Hello World application.
This tutorial assumes that you have access to the Ubuntu 16.04 system and configured a non-root user with sudo
permissions. 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
Visit Official Go Download Page to find the URL of the current binary version of the tarball and its SHA256 hash. Make sure you are in your home directory and use curl
to retrieve the tarball:
cd ~
curl -O https://storage.googleapis.com/golang/go1.6.linux-amd64.tar.gz
Next, you can use sha256sum
to verify the tarball:
sha256sum go1.6.linux-amd64.tar.gz
go1.6.linux-amd64.tar.gz
e40c36ae71756198478624ed1bb4ce17597b3c19d243f3f0899bb5740d56212a go1.6.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, extract the tarball with tar
. The x
flag tells tar
to decompress, v
tells it that we want detailed output (a list of extracted files), and f
tells it that we will specify the file name:
tar xvf go1.6.linux-amd64.tar.gz
You should now have a directory 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 it by calling the Go command install
:
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 compiled binary file hello
:
which hello
Output/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.
To learn more about installing Go 1.6 related tutorials, please go to [Tencent Cloud + Community] (https://cloud.tencent.com/developer?from=10680) to learn more.
Reference: "How to Install Go 1.6 on Ubuntu 16.04"
Recommended Posts