Golang, the programming language developed by Google, was officially launched in 2011 but has gained popularity only recently. It’s a modern language that adapts to each Internet capacity and speed, and is optimized to enable thousands of users to execute programs at the same time.
Due to its open source nature and, undoubtedly, the relevance of its creators, Go has earned the support of the software community, particularly of the new generation of programmers who value the fact that Go is syntactically similar to one of the most stable and respected languages (C) but, at the same time, is simpler.
Among its similarities with C, we can mention that it’s a statically typed, compiled programming language that can produce executable files in other operating systems like Windows, Linux and Mac from the same source code. But unlike C, in Go it isn’t mandatory to use parenthesis for conditions and loops, and using semicolons at the end of lines is optional.
Like Java, this language has an automatic garbage collector that helps to manage issues related to memory and space available.
Golang’s Key Points
- It belongs to the imperative, non-object-oriented programming paradigm but it also enables functional programming, although experts advise against using it for this purpose.
- To state if functions or methods are public or private, uppercase or lowercase are used. So, if the first letter of the word is capitalized, it means that the field is public, otherwise, it means it’s private.
- Usually, functions return more than one value, the result of the operation and errors, if any. Underscores (_), also called blank identifiers, are used to ignore values.
- Like C, Go uses pointers, through which we can access the location where a variable is stored. Pointers are created using asterisks (*) and ampersands (&): the formers are used to get the variable’s value while the latters are used to obtain the memory location. The reason behind this is to enable access to the variable either by value or reference.
- Regarding risk management, in Go the error state is taken as one of the possible outcomes of an operation, so it’s necessary to explicitly state all the possible errors that can appear in every function. As a result, the code can become wordy.
A Powerful Back-end
One of Go’s strengths is that it supports concurrency through two basic elements: goroutines and channels.
Several goroutines can be executed simultaneously. We could think of them as lightweight threads, because they require less memory and less code. They are created by adding the “go” keyword to a function call.
On the other hand, channels are communication mechanisms that allow sharing data between goroutines. Their main operations are send and receive, which are represented by an arrow (<-).
There are two types of channels:
- Unbuffered: These can store one single data and prevent goroutines from sending more until other goroutine receives the one that was previously stored.
- Buffered: They include an item queue that works as a FIFO queue.
To avoid common concurrency issues, Go offers different solutions:
- Blocking shared resources in packages such as atomic and sync.
- Mutual exclusion using mutex, to create a critical section.
- The channels mentioned above, to keep goroutines synchronized.
Why Use Golang?
Go has all the benefits of mature languages like C combined with the characteristics, optimization and support provided by a modern language, such as code simplicity. This means that for developers who want to get to know more about this language —and especially for those with solid knowledge of C who want to migrate to a newer language, the learning curve is soft.
Please feel free to comment and share your experiences with Golang. Have you tried it yet? For what kind of project? What do you think are some of its advantages and disadvantages?
If you haven’t tried it yet, you can write your first line of code in Go here:
- The Go Programming Language, by Alan A. A. Donovan & Brian W. Kernighan
- Go in Action, by William Kennedy with Brian Ketelsen & Erik St.