Install GoLang on Ubuntu and Write Your First Program

12/02/2020

Install GoLang on Ubuntu and Write Your First Program in Go

GoLang is a very powerful programming language developed by Google. It is a compiled programming language. It means, Go source codes are converted to machine code or commonly known as executable file. Then you can run these executable files on other computers. Unlike Java that converts source code to byte code, then runs these byte codes using JVM (Java Virtual Machine), Go does not use any VM (Virtual Machines). It is not an interpreted language either like Python or PHP. It is very fast and built with concurrency in mind. GoLang is widely used for Web Development because it has many libraries available for such stuff.

In this article, I will show you how to install the GoLang on different versions of Ubuntu operating system and how to write, run and build your first program with Go. Let’s get started.

Installing GoLang:

GoLang is available in the official package repository of Ubuntu.  First update the package repository cache of your Ubuntu operating system with the following command:

$ sudo apt-get update

Your package repository cache should be updated.

Now you can install GoLang from the official repository of Ubuntu.

Ubuntu 16.04 LTS:

On Ubuntu 16.04LTS, you can install GoLang 1.6 from the official repository of Ubuntu. This is the recommended version of GoLang on Ubuntu 16.04 LTS.

To install GoLang 1.6 from the official repository of Ubuntu 16.04 LTS, run the following command:

$ sudo apt-get install golang

If you want to install GoLang 1.9 on Ubuntu 16.04 LTS, enable the ‘xenial-backports’ ‘universe’ repository and run the following command:

$ sudo apt-get install golang-1.9

Ubuntu 17.10:

On Ubuntu 17.10, you can install GoLang 1.7, GoLang 1.8 and GoLang 1.9.
To install GoLang 1.8 on Ubuntu 17.10, you can run the following command:

$ sudo apt-get install golang

Or

$ sudo apt-get install golang-1.8

To install GoLang 1.7 on Ubuntu 17.10, run the following command:

$ sudo apt-get install golang-1.7

To install GoLang 1.9 on Ubuntu 17.10, run the following command:

$ sudo apt-get install golang-1.9

I am using Ubuntu 17.10 for the demonstration in this article. I will install GoLang 1.8.

$ sudo apt-get install golang

Once you run the command to install the version of GoLang you want, you should see the following prompt. Just press ‘y’ and then press <Enter> to continue.

GoLang should be installed.

Testing GoLang:

Now run the following command to verify that Go commands are accessible:

$ go version

You should see similar output as shown in the screenshot below. It means Go is working correctly.

Writing your First “Hello World” Program on GoLang:

The very first program that most people write while learning a language is the “Hello World” program. I would say, “It’s the gateway to the heart of the programming language”. It is very simple. All a “Hello World” program does is; it prints “Hello World” to the console or terminal.

Now I am going to write a simple “Hello World” program in Go.

This is the code that I am going to run.

package main
import "fmt"
func main() {
fmt.Println("Hello World");
}

It is saved in ‘~/work/helloworld.go’ file. Remember to save GoLang source files with .go extension.

GoLang can be used like an interpreted language like Python. It means that you can run a GoLang source file directly without manually compiling it first.

To run a go program, run the following command:

$ go run GO_SOURCE_FILE

In my case GO_SOURCE_FILE is ‘helloworld.go’.

$ go run helloworld.go

You should be able to see “Hello World!” output on the console as shown in the screenshot below.

The good thing about GoLang is that, you can also build an executable file out of GoLang source code. So it can be executed just as C or C++ programs.

Run the following command to compile Go source code:

$ go build GO_SOURCE_FILE

In my case, GO_SOURCE_FILE is ‘helloworld.go’.

$ go build helloworld.go

It should generate an executable file ‘helloworld’ as shown in the screenshot below.

Now you can run the executable as follows:

$ ./helloworld

You should see “Hello World!” on the terminal just like before.

So this is how you install GoLang on Ubuntu and write your first program in GoLang. Thanks for reading this article.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Nuclear Music Player – An Electron-based Music Streaming Player

Nuclear Music Player is a nice looking free music streaming app that pulls its content from various free sources all over...
12/02/2020

Upgrade Ubuntu to 1804

How to Upgrade from Ubuntu 17.10 to 18.04 Ubuntu 18.04 Bionic Beaver is not out yet. But many people may want to upgrade...
28/12/2020

How to Listen to WAV Files on Ubuntu

There are a number of available media file types all over the internet, each of them having their own features and disadvantages....
29/12/2020