Golang | The Basics Part 1

Waleed Ahmed
6 min readSep 20, 2022

Today we have so many great languages like Python, Node.js, Java, PHP, used to build backend services and distributed systems. Each of them has their own advantages and disadvantages. One is reliable and easily understandable, the other is fast and scalable but there is no language other than GO to fill the sweet spot between all four categories, Fast, Reliable, Productive and Efficient.

Agenda:

  • Introduction of Golang
  • Unique features of Golang
  • Breakdown of Hello World program
  • Running/executing the Program
  • Conclusion

Introduction of Golang

GO is a fast, reliable and compiled language designed by Robert Griesemer, Rob Pike, Ken Thompson at Google in 2007. GO became an open source project in 2012. It is mostly used to create high performance large scale distributed infrastructures. GO was initially developed for distributed network services but now it is expanding towards Cloud Native development, DevOps etc.

This article is for everyone either learning golang or an expert. I’ll be using a top down approach to explain deep concepts in golang. I’m pretty sure that we all know about the features of golang, but didn’t know why those features make so much impact.

Unique Features of Golang

Go is one of the fastest growing language in terms of popularity. Go was made in the era where we knew the concepts of concurrency and parallelism, So it was designed to handle concurrency and other important features. Some of the features are…

  • Statically Typed (feels like dynamically typed)

GO is a statically typed language but it also gives functionality of dynamic typed languages. I know it might be complicated but first let’s know the difference between static and dynamic typed languages. In a simple way, statically-typed languages require you to declare the data types of your variables before you use them, while dynamically-typed languages do not.

In the above example both codes are trying to create a variable and store a value but in C++ we have to specify the data type int as well, but in Python data type is inferred based on the value 2.

In Golang we have both syntaxes. That is why golang is a sweet spot between languages.

  • Object Oriented (Not Type Oriented)

Go is an object-oriented language but unlike other object-oriented languages like Java or C++ Go is not type-oriented. There is no sub classing because there are no classes at all. Instead we have structs and methods, where we can add functions to the struct that adds behavior to it. Hence, objects in Go are simply types with methods defined on them.

  • Built-In Concurrency

Concurrency is the execution of multiple tasks at once without affecting the final outcome. Go has built in concurrency, that is one of the benefits of Go over other languages. In most popular languages like, java and python concurrency is achieved using threads. In Golang concurrency is achieved using goroutines. Goroutines are lightweight threads where communication is managed by Channels.

Writing your first application in golang

Download and install GO through the official website Download and install — The Go Programming Language. Install the latest and stable version for golang. After Installation Run go version to check the successful installation of GO on your machine.

Golang Hello world program

After installation, we’ll create a main.go file inside a directory. Write the hello world code inside the go file shown above. Hello-World is the first program you’ll always write to understand the basic structure of a programming language. Let’s break the program into pieces and discuss them.

  1. Package Main

In golang, we have a package system. If you have worked with java or JavaScript then you might have a good idea about package systems. A package is just a directory with a bunch of reusable code in it. Let’s say you want to build a calculator app, you have to perform simple arithmetic operations addition, subtraction, multiplication and division repeatedly. What will you do? A Naïve solution would be to just implement these functions in every code file but that will be a tedious task. Another solution is to take these functions and bind them in a directory called package and use these functions in your main directory.

The main package is an entry point of an executable program. GO main package tells the compiler that the package should compile as an executable program instead of a shared library.

2. Import Packages

Import keyword is used for importing a package into another package. In Line 2, we have imported the fmt package inside our code. Fmt package is mostly used to perform IO operations inside the app. Yes, you are right, GO doesn’t even have a built-in IO functionality, it’s because the creators want to keep GO light weight. There are two ways of importing a package or packages.

3. Main Function

Line 3, represents the main function of the program. Func is a keyword to write a function inside golang. Looking at the func maincompiler knows to start the execution of the program from here. Then there are curly brackets {} which specify the block of the main function in the program.

Inside the main block first line we see fmt.Println("Hello World")statement. Println function inside fmt package is used to print output on screen. You’ll be wondering why we didn’t specify a line separator like semicolon ; at the end of line, like other compiled languages. Go compiler is already efficient, it places a ; at runtime internally.

Running your first GO application

We got a pretty good idea of the basic structure of the GO program. Our next step is to run the program and get the expected outcome. We have two options: either run the program using the go cli command go run main.go or build the executable file and run that.

To build the executable file we run go build main.go in cli. This will build a binary file which you can run on any architecture without installing GO language. To run the build file we run ./main command.

Conclusion

After you have read this article, you’ll have a great motivation towards learning golang. I think I have explained why golang is a sweet spot between languages as it has the best features taken from famous languages.

If you want to read about the golang role in DevOps and Cloud Native Computing, Check out this blog here.

A little bit of my introduction, I’m a 7th semester student, doing bachelors in data science. I’m an intern at Cloud Native Islamabad, also a CNCF Glossary contributor. I have my Interests in the field of DevOps and Cloud Native development.

Follow Cloud Native Islamabad handle for amazing content and hands-on tutorials.

--

--