Writing Your First Golang Application

Nilesh Kesar
Revel Systems Engineering Blog
4 min readJun 6, 2022

Writing your first Golang Application and becoming a certified gopher isn’t as hard as you might imagine. In this tutorial, we will build a Hello World project, run it, and build an executable. If you already know how to do all that you can go to this tutorial where it goes into Go lang concepts https://medium.com/@nilesh.kesar/understand-concepts-in-golang-9f0195adf165

What Is Go

Go or GoLang is an open-sourced general purpose programming language created at Google in 2007, it is a compiled programming language, and takes things from C++, Java, and Python. It is meant to be easier to use than C++ while also being optimized for speed and efficiency. Also, their mascot/logo is a blue gopher, which personally always brings a smile to my face.

A Smiling Blue Go Gopher

Install Go

Go to the official Go website https://go.dev/dl/ and download the version that matches your architecture. To verify that you installed it correctly in your terminal run the following command go version and you should see some output like this.

Download IDE

You can install a number of different Go IDEs such as VSCode or GoLand, more links are here https://www.tabnine.com/blog/top-7-golang-ides-for-go-developers/. For this tutorial, we will be using VSCode.

Writing Hello World

Create an empty folder and open up it up in your IDE, once opened create a file called main.go. Starting on line one type the following. This declares that your file belongs to the main package which is like modules if you're familiar with python. If you are not familiar with python, think of packages as a folder each folder is a package of Go code. Each package has a unique name and you will always start with the main package. As well you will declare your main function which is where all code will start from.

package mainfunc main() {}

After that let’s write hello world to the screen. Inside your Main file type

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

Now open up your terminal and type go run main.go this will run your go application and print “Hello World!!” to the terminal

Build an executable file

With your terminal open type go mod init firstGoProject this will create a go.mod file, if you are familiar with npm it's just like your package.json, this file names your project, this will also be where you specify any libraries you are using for this project. If you have any unused libs you can run go mod tidy this will clean your go.mod file it will get rid of any unused dependencies from the file and if you have any dependencies it will create a go.sum file, the go.sum file is an auto-generated file and is like a package-lock.json which go uses to make sure it installs the right package and version.

Now run go build this will create a binary executable file which will automatically be compiled to run on your host OS, but you can change it to different architectures with build flags. Now run ./firstGoProject and it will run the executable. The nice thing about Go is that like docker everything is packaged up when you build so you can run it anywhere exactly the same way. You don’t even need Go installed on the OS that is running the executable! Congratulations on becoming a certified gopher!!

Next Steps

This is a more advanced tutorial explaining Go concepts https://medium.com/@nilesh.kesar/understand-concepts-in-golang-9f0195adf165

Credits:

Gopher Pictures: https://github.com/MariaLetta/free-gophers-pack

--

--