Golang or Go is an open source programming language that makes it easy to build reliable and efficient software. Go is a statically typed and compiled programming language. In this post, we will start with Golang Hello World program. Basically, it will be our first Go program that will help us understand the basics of this programming language.

1 – Go Installation

The first step to use Go programming language is to install it on your system of choice.

In order to do so, you can head over to this link and download the package appropriate for your operating system. A basic installation wizard will walk you through the process.

Once the installation is complete, you can check out whether it works by issuing the below command in your command prompt, terminal or bash.

$ go version

It should print the installed version of the Go.

2 – Initializing the Golang Program

Now that we have installed the programming language on our system, it’s time to write our very first Go program. As customary, it will be a basic Hello World program. In other words, all we are going to do is print Hello, World to our console.

The first step is to create a directory for the Go module.

We can do so by using the below command.

$ mkdir hello

Next, step is to initialize the Go module.

$ go mod init example/hello

Let us understand the above command. Basically, this command creates a go.mod file in the hello directory. This file keeps track of all the dependencies our module is using. In other words, you can think of it a dependency management file. The go mod init command enables dependency management for our project.

Here, example/hello is nothing but the module path. Typically, it will point to the repository location where the source code will be present. For example github.com/hello. We will look into naming modules in a later post.

At this point, the go.mod file will look as below:

module example/hello

go 1.17

Basically, we have the module path and name. We also have the version of Go used for this program.

3 – The Golang Hello World Program

Next step is to write the actual program. We create a file named hello.go in the hello directory. Below is the code:

package main

import "fmt"

func main() {
	fmt.Println("Hello, World")
}

Let us understand what is going on here.

  • We declare a main package. You can think of package as a way of grouping functions. It is made up of all the files residing in the same directory.
  • Next, we import the fmt package. This package contains functions for text formatting and printing stuff to the console. It is part of the standard package library that ships along with the Go installation.
  • Lastly, we implement the main() function. When we execute a package, the main() function gets automatically executed. In other words, it is the entry point to our package. In this main() function, we simply use the Println() function in the fmt package to print Hello, World to the console.

4 – Running the Golang Hello World Program

Running our code in Go is pretty straightforward. Just head over to the hello directory in the command prompt and execute the below command.

$ go run .
Hello, World

As you can see, this will print the message on the console.

Conclusion

With this, we are done with our Golang Hello World program. We looked at installation followed by how to initialize our Go module using the init command. Lastly, we wrote a small program to print a message using the standard fmt package and ran the program.

In the next post, we will looking at how to call a piece of code from an external module into our module.

If you have any comments or queries till this point, please feel free to write in the comments section below.


Saurabh Dashora

Saurabh is a Software Architect with over 12 years of experience. He has worked on large-scale distributed systems across various domains and organizations. He is also a passionate Technical Writer and loves sharing knowledge in the community.

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *