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:
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:
If you want to install GoLang 1.9 on Ubuntu 16.04 LTS, enable the ‘xenial-backports’ ‘universe’ repository and run the following command:
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:
Or
To install GoLang 1.7 on Ubuntu 17.10, run the following command:
To install GoLang 1.9 on Ubuntu 17.10, run the following command:
I am using Ubuntu 17.10 for the demonstration in this article. I will install GoLang 1.8.
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:
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.
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:
In my case GO_SOURCE_FILE is ‘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:
In my case, GO_SOURCE_FILE is ‘helloworld.go’.
It should generate an executable file ‘helloworld’ as shown in the screenshot below.
Now you can run the executable as follows:
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.