Getting Started with GoLang

Getting Started with GoLang

Featured on Hashnode

Do you want to learn Golang, want to know how to get started, or find it difficult understanding the program structure, this is for you. In this article, I will be sharing:

  • What and Why Golang?

  • Golang basic code structure

  • Useful resources to get started

Let's Go there! gophercises_jumping.gif

What is Go?

Go is an open-source and compiled programming language for building efficient and scalable applications that would last over a long period of time. It was developed at Google by Robert Griesemer, Rob Pike, and Ken Thompson in 2007 and published in 2009. Asides from the teamwork, the combined effort from other contributors keep improving the language making it better.

Why Go?

I had felt the need to learn a new programming language so I picked Go because an open-source community Layer 5 I contribute to uses it for most of its projects and I wanted to expand my contributions to the community.

Coming from a Python developer space, Go is actually different from syntax and structure( On my first day, I said how will printing hello world have up to five lines of code).

Why did I choose Go? Well, I wanted to speed up the compilation of my programs, see whether the language would be easier to write, have more opportunities to contribute to open-source projects. So far with learning the basics, I have been able to catch up with the syntax pretty well and fast.

The goal of the Go project was to eliminate the slowness and clumsiness of software development at Google, making the process more scalable and productive. It was designed to solve concurrency problems that older low-level language had with improved computational systems.

Go Program Structure

Let's practically understand this by analyzing line by line a Hello World program in Go!

package main

import "fmt"

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

Line 1 - package main

Go programs are made of parts such as packages definitions and function definitions.

"package main" part defines the package. The main package is a special package that is used with the programs that are executable. When the main package is used, it tells the Go compiler to run the program as an executable.

Line 2 - import "fmt"

There are common sets of functionality that everyone needs. (e.g. I/O functions to print output) Go provides you with a rich set of packages with this functionality in the standard library package which is included automatically with Go. In order to use them within your program, you'll use the import keyword. So "import fmt" loads the fmt standard package.

Fmt is the Format package. It helps in formatting input and output(I/O) operations. This package allows you to format basic strings, values, or anything and print them or collect user input from the console, or write into a file using a writer, or even print customized fancy error messages.

Line 3 - func main

Func main declares the function "main". The main() function is the entry point of executable programs and it does not take in any arguments or parameters. This function executes by default when you run code in the file. The lines of function code are enclosed in curly brackets {} and the code written inside the braces {} of the main function in package main will be executed in sequence.

Line 4 - fmt.Println

While line 2 imported the library, we didn't use anything from the library. The functionality that we want to implement is to send the output of "Hello world" to the terminal. The fmt package is what implements the I/O functionality with a number of functions. This example uses Println to output what is given to it as an argument followed by a new line. To use a function in the standard package you write packageName.functionName in this case "fmt.Println".

This practical example just shows you how packages and function definitions work. Remember you can use different packages and functions depending on what your program does. You can refer to the Go documentation for more context

Useful resources to get started

A tweet I made that got some good responses with amazing resources to get started with Go. The highlights and some resources I use:

I hope this article was helpful to you. Feel free to drop the resources you use in the comment section. I recently started learning and would love to connect, you can send a DM @ikegahruth on Twitter.

Thanks for reading!