“Hello World!” with Go.

Sasanka C
Analytics Vidhya
Published in
7 min readOct 6, 2020

This article is aimed at developers who are interested in learning one of the awesome programming languages from Google — “Go“. In the following “Hello World” example with Go, I will show you the detailed steps on how to install the Go environment, code and execute the “Hello World” binary, and finally, use golang playground to share your code with others.

“Hello World!” With Go

What is Go ?

Here is how the Go development team describes the language on their home page.

“Go is an open-source programming language that makes it easy to build simple, reliable, and efficient software.”

Well, that a little bit vague isn’t it? Let me explain a bit more, If you ever want a simple programming language to learn and a piece of code that compiles fast, executes fast, and makes it easy to distribute your work for others then Go is the programming language for you.

Go programming language is developed by 3 smart Google engineers — “Robert Griesemer, “Ken Thompson” and “Rob Pike. Some of the key goals of the Go programming language are :

  • Easy to learn and code.
  • Fast compilation.
  • Free up unused memory automatically (garbage collection)
  • Built-in concurrency (Software that performs multiple things at the same time).
  • Support for processors with multiple cores.

I will cover more details, features of this language in future posts. Let’s get the Go environment up and running.

Installing Go environment

Go language does not require a runtime for deploying applications, however, you need to download the development environment for writing the programs.

There are 7 simple steps in this post to run your first Go “Hello World”, bear with me.

  1. INSTALL GO FOR YOUR PLATFORM (WINDOWS, MAC OR LINUX)

The first step is to get a development environment. Just smash https://golang.org/dl/ in your browsers address bar, or click the link above, and you should be good to go. The home screen should give you what you want right away. As I am running Windows 10 on my machine, the respective version of Go for my operating system is listed. Go ahead, download and install it.

2. CHECKING FOR GIT

One of the requirements in this setup is to have Git installed on your machine. Open the command prompt and type in the command “git –version” and it should return something like below. If you don’t see this output then go ahead and install git.

Follow the instruction here.

C:\Users\sasan>git --version
git version 2.28.0.windows.1

3. INSTALLING A TEXT EDITOR OF YOUR CHOICE

Microsoft visual studio code is one of the excellent text editors with Go extension and is very active in the Go community. There are many text editors out there supporting Go programming, so feel free to you a text editor of your choice. Click on the image above to download the visual studio code or simply use the link to download.

4. INSTALLING GO EXTENSION FOR VISUAL STUDIO CODE.

We will get the Go extension to VS Code by pressing the command Ctrl + Shift + X or by simply click on the extension icon as shown in the image above and typing Go in the search box.

We have one more step in this setup, Go extension for VS code depends on various open-source tools that provide formatting, documentation, and other nice features for programming. From the VS Code press function key — F1, type in the command “Go:Install/Update Tools”, select all the plugins and click on install and wait till you see the below image.

5. CREATE A FILE NAMED HELLO_WORLD.GO

Create a new folder somewhere on your machine and name it as “Golang” something fancy. Now create a new file in the directory and name it as “Hello_World.go”

Write the following:

package mainimport "fmt"func main() {
fmt.Println(" ** Hello World! ** ")
}

Don’t worry too much about the indent, it will be taken care of by the editor as soon as you save the file. Now let’s understand the code.

A typical Go file layout is made up of

  1. Package Clause (package main)
  2. Any import statements (import “fmt”)
  3. Actual logic. (fmt.Println(” ** Hello World! ** “))

PACKAGE

package main

Every Go file starts with a package clause. Now, what is a package hmm?

The very first non-comment line in any Go program must be a package declaration. Every Go file belongs to a package and package is where Go uses to organize the code.

All our code is located in the package called main in this example, We don’t usually put all our code in a single package. Go programs are broken into multiple packages with each one focusing on one area of functionality.

Each package contains related functions and data types. There are 3 types of packages in Go — packages you create, the third-party packages that other people create, and the packages in the standard library. In this case, we use the special package main, which is required if this code is going to be run directly (usually from the terminal).

In short, the line “package main” says all the rest of the code in the file “Hello_World.go” belongs to the “main” package.

IMPORT

import "fmt"

The way to specify the packages that code depends on is by importing. If a package is not imported you cannot access the code within that package. This is different from Python and Java where importing a package simply means you can refer to the code within it by a shorter name.

Both Python and Java allow you to import a single item out of a package. For instance, to import counter from the collections package in Python is by running the below command.

from collections import counter

Go doesn’t allow you to just import one single item out of a package instead importing a package gives you access to everything in the package.

To import a package we use the keyword “import” followed by the path to the location of the package either within the standard library or within our Go path.

It’s a good practice to import only the packages required for that piece of code.

REFERRING FUNCTIONS

We refer to the functions, variables in the package by using the name of the package followed by a dot and the function name. The function “println” as the name suggests prints the output on the terminal or web browser.

6. RUNNING “HELLO WORLD!” ON TERMINAL

Type the command “go run Hello_World.go” and the see the output yourself.

if you want to create a binary then type the command “go build Hello_World.go” and follow the below screen shot.

Congratulations! you made a first Go binary.

7. RUNNING “HELLO WORLD!” ON GOLANG PLAYGROUND

I want to show you one more thing, a site where you can test and share your Go code.

Fire up your browser and Go to play or simply click on the image above. This is the Go Playground that you can use rather than setting up a project every time you have an idea you want to test, brilliant, isn’t it?

The Go Playground will also format the code for you when you hit the format button.

There’s one more cool trick the Go Playground can do. It can generate a unique URL that allows you to share your code snippet with other people. Click the share button and a text field will pop up next to it with a URL. You can copy this URL and share it with other people, or just come back to the URL again.

The Go apparently said they intend for these links to last forever.

That’s it for me today. If you liked this article and if it helped you in any way, feel free to like it.

If you believe this article will be of big help to someone, feel free to share.

--

--

Sasanka C
Analytics Vidhya

Data Engineering specialist at Major bank in Australia, Melbourne. Founder, author of thetechfirewall.com.