How to install Go on Ubuntu 18.04

Introduction

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.

Course Preparation

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.

Step 1-Install Go

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.

Step 2-Set Go 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

Step 3-Test your installation

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

in conclusion

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

How to install Go on Ubuntu 20.04
How to install Go on Ubuntu 20.04
How to install Go 1.6 on Ubuntu 16.04
How to install Go on Ubuntu 18.04
How to install Ruby on Ubuntu 20.04
How to install Memcached on Ubuntu 20.04
How to install Java on Ubuntu 20.04
How to install MySQL on Ubuntu 20.04
How to install VirtualBox on Ubuntu 20.04
How to install Elasticsearch on Ubuntu 20.04
How to install Protobuf 3 on Ubuntu
How to install Apache on Ubuntu 20.04
How to install Git on Ubuntu 20.04
How to install Node.js on Ubuntu 16.04
How to install MySQL on Ubuntu 20.04
How to install Vagrant on Ubuntu 20.04
How to install Go on CentOS 8
How to install Bacula-Web on Ubuntu 14.04
How to install Anaconda3 on Ubuntu 18.04
How to install Memcached on Ubuntu 18.04
How to install Jenkins on Ubuntu 16.04
How to install MemSQL on Ubuntu 14.04
How to install MongoDB on Ubuntu 16.04
How to install Mailpile on Ubuntu 14.04
How to install PrestaShop on Ubuntu 16.04
How to install Skype on Ubuntu 20.04
How to install Jenkins on Ubuntu 20.04
How to install Python 3.8 on Ubuntu 18.04
How to install KVM on Ubuntu 18.04
How to install KVM on Ubuntu 20.04
How to install opencv3.0.0 on ubuntu14.04
How to install Anaconda on Ubuntu 20.04
How to install Prometheus on Ubuntu 16.04
How to install Jenkins on Ubuntu 18.04
How to install Apache on Ubuntu 20.04
How to install R on Ubuntu 20.04
How to install Moodle on Ubuntu 16.04
How to install Solr 5.2.1 on Ubuntu 14.04
How to install Teamviewer on Ubuntu 16.04
How to install MariaDB on Ubuntu 20.04
How to install Mono on Ubuntu 20.04
How to install Zoom on Ubuntu 20.04
How to install Nginx on Ubuntu 16.04
How to install OpenCV on Ubuntu 20.04
How to install Spotify on Ubuntu 20.04
How to install Postman on Ubuntu 18.04
How to install MySQL on Ubuntu 14.04
How to install PostgreSQL on Ubuntu 20.04
How to install VLC on Ubuntu 18.04
How to install TeamViewer on Ubuntu 20.04
How to install Webmin on Ubuntu 20.04
How to install Docker Compose on Ubuntu 18.04
How to install Ubuntu on Raspberry Pi
How to install Bacula Server on Ubuntu 14.04
How to install MySQL on Ubuntu 18.04 (linux)
How to install Ubuntu 19.10 on Raspberry Pi 4
How to install Apache Kafka on Ubuntu 18.04
How to install Apache Maven on Ubuntu 20.04
How to install Apache Tomcat 8 on Ubuntu 16.04
How to install Python2 on Ubuntu20.04 ubuntu/focal64
How to install GCC compiler on Ubuntu 18.04