Stepping Towards Golang — The first Go program

Rajat Gupta
5 min readJul 31, 2020
Let's get started being a gopher!

Golang developed rapidly over the last decade tracing history back to 2009 of early development. Golang includes all good things inspired by other programming languages and tackled many modern-day programming language problems. Its main focus was on building language which can be used to write large enterprise applications with increasing efficiency and computations speed majorly through concurrency. This tutorial is for beginners in Go who want to get started with Golang.

My personal choice has been Python for the quick development of APIs and services for years and professionally I’m working in Python and Node. Key reasons which still drives me to learn a new language are code structure, readability, and performance. That’s why I started learning Go last year.

I’m writing this for absolute beginners. So in this post, I will cover project setup and first programs. My belief for learning a new language is to implement what you previously did code in other languages like to start by writing a few algorithms and data structure programs. I have one repository in Github for algorithms and data structure in Golang. I would encourage you to fork and create pull requests for adding new programs (DS/Algos or other problems) written in Golang.

Outline:

  • Project setup
  • First Go program
  • Variables, Functions, and I/O formatting
  • Building Go program

Introduction to Golang

Golang is a modern general-purpose statically typed, compiled programming language that majorly focusses on performance. Developed by the internal team at Google designed by Robe Pike & Ken Thompson. It’s syntactically similar to C but with some changes like module includes is replaced with imports, garbage collection, and most importantly which made is successful is concurrency. We will study later on how Golang achieves concurrency through goroutines. Let’s get into the first Go project.

Project Setup in Golang

Setting up a project in Golang is a bit tricky. Assuming you have downloaded and installed the Golang compiler from the official page. Once installed fire up a terminal in your Linux or macOS then see where environment variables GOPATH and GOROOT are pointing. Just copy and paste the following commands.

GOROOT is variable which points to go SDK and GOPATH points to the workspace where all projects should be kept. Although we can change GOPATH to whichever path we want to make our workspace as of now we won’t be doing that. Inside GOPATH there are 3 directories, “bin”, “pkg”, “src”. “bin” contains all binary packages after the build, “pkg”, contain’s all the packages compiled from the “src” directory. In short, you can keep different projects under the source directory for the beginning. Or you can have different GOPATHs for different projects.

Let's start one project named with anything you want. I named it “algos”. You can either point GOPATH to the project folder or put the project folder inside $GOPATH/src/ so that you don’t have to modify and keep track of $GOPATH. Clone the following repo for reference inside $GOPATH/src/.

IDEs and Code editors like Goland and VSCode respectively very good for beginners and professionals both. Although, I would suggest using Goland by Jetbrains.

Let’s Code

Let's write our first hello world program in Goland and put the first step to be a gopher. Create a file name main.go.

go run main.go

For running this program in terminal run above program inside your project. This program simply outputs Hello World in your terminal. Let’s dive into what is exactly happening over here.

  • Line 1, we define this as the main package. As go applications are comprised of packages
  • Then we import formatted I/o package “main”. This package contains all functions necessary to perform I/O operations in Go.
  • All go programs start from executing the main function. Just like C/C++ we can see and compare it has a lot to share syntactically.
  • All functions definitions start with keyword fun before the name of the function.

There are variations of I/O functions in Golang just like C.

Let’s write a few more functions in Go to get more grasp before we start writing programs for algorithms and DS.

Run following in a terminal:

go run main.go
==== Output:
Hello World!
Addition of 2 and 4 is = 6
Value of c with floating 1 digit precision 4.2
Value of d with floating 2 digit precision 2.42
Is 4.20 is grater thant 2.42 ?
Answer is true

Notice above program has variable declarations of different types and out formatted I/O package has replaced the contents respectively how we wanted. We wrote two functions add which adds two integers and return addition of those integers and another function “greaterA” which compares two float64 variables and returns a bool value. We can also see different ways of declaring variables in Go.

Now, let’s build our very own package main and run the native binary executable program. For this run following:

go build main.go

By running the above command we generate one binary name main in the same directory. Run that binary using:

./main==== Output will be ===
Hello World!
Addition of 2 and 4 is = 6
Value of c with floating 1 digit precision 4.2
Value of d with floating 2 digit precision 2.42
Is 4.20 is grater thant 2.42 ?
Answer is true

Conclusion

In conclusion, I would like to say go is a fairly easy language to learn if you are coming from C/C++ or any other language background. It is also easy for first-time learners who are just starting to learn how to code. We saw how to set up a Go project and create our first package main and learned writing functions and basics of I/O formatting along with building a Go program to generate a binary file.

What’s next?

An upcoming article will be on interfaces and structs in Go with some algorithm problems and test suits in Go. After that, we can start building APIs and learn how to utilize goroutines too.

Once again, I would encourage to fork and contribute to the repository I shared, that contains all programs with the above explained and various algorithms. Contribute to it and create pull requests for adding DS/Algo programs. Feel free to reach me out.

Thanks for reading you can find all code here

--

--