Installing and Running Go

Chapter 2

Installing Go#

Installing Go is pretty straightforward. You can either install via the package installers or directly from the source on the Go Installation page.

Running Go files#

Let's try running "Hello World" in Go.

package main
import "fmt"
func main() {
fmt.Println("hello world");

To run this, we simply use the run command as shown below. A good place to experiment with Go code is with the Go Playground.

// run a single file
go run hello.go
//creates an executable with a default name of the folder
go build
// creates an executable with specified filename
build -o filename

Why is the Go bundle so big (2MB) for a Hello World program?#

If you built the hello world code we wrote above, you will see a binary with the name of the folder the code is in. For example, if you had hello.go within a folder called hello, then a binary with a default name of hello or hello.exe (depends on macOS or Windows) is created within the folder.

You'll probably be surpised that a simple hello world program takes 2MB! Let's understand why this is so.

Let's take Java first. To run Java code you will need the Java Virtual Machine (JVM).


JVM runs over the OS and any place you run Java code, you need the JVM. This allows you to run the code anywhere the JVM is present, which handles threading, garbage collection etc. It becomes a mini-OS running over the actual OS.

For C code, it generates an executable. Even though the code itself is very small, at execution, it calls upon other system dependencies. When you write code which has dependencies not present on your production environment, the code simply won't run.

Now, coming back to Go. Go only requires the OS. It does not require a special runtime. When we build Go code, it compiles it directly from high level code into machine code. It is important for the code to be independent of dependencies such as libraries or runtimes etc. so that it can run in any environment possible.

To solve this problem, Go has its runtime within the bundle. And guess what? Concurrency, threading, garbage collection and everything required for the code to run is all bundled into just 2MB! Now, all we need to do is to take this bundle and run it where we need the code to execute.

Quick Tip

A> For someone using macOS, to build the same bundle for Windows, he/she simply specifies the OS - GOOS=windows go build.

Aside: VM vs Containers#

Virtual Machines

In a virtual machine, we have a hypervisor which runs on top of the hardware infrastructure. Here, each application needs a guest operating system which runs on top of the hypervisor. For example, this is how the Java Virtual Machine (JVM) runs.


Compare that with containers. Containers directly run on top of the operating system on the hardware. We have a container engine which manages the various containers. Each app does not require a separate OS to run. Instead the resources are shared efficiently. Containers are small and lightweight and we can easily deploy multiple 100s of containers as compared to heavy VMs. Containers can be spawned in just seconds.

Go shines beautifully here, being small and lightweight, we can do scaling well and quickly. Within seconds, multiple containers can be created to take on load.

Fun fact!

A> Google's popular container orchestration system, Kubernetes, is built with Go. Not surprising, since Go was built for efficiency and performance at Google itself!

A good discussion on this topic is available here.