Go programming language that is impossible not to know

What is Go programming language and what is its main advantage. Because it’s worth learning the rudiments of it.

There are dozens of programming languages: some are quite old and rarely used, others seem not to feel the weight of the years, still others have been developed recovering at least in part the work already done in the past.

In another article we talked about machine language and the differences between compiled and interpreted languages.

We have often talked about the Go programming language: born in 2007 and officially presented in 2009, it is a language developed by Google that is easy to use, performing and efficient in the compilation phase. Go compilers they are available for the main platforms including Windows, macOS, Linux and in general Unix-like systems.

The syntax of Go is close to C, except for the declaration of types and the lack of parentheses in the constructs for e if.

In 2019 we published a post on the occasion of the first ten years of the Go language, also recalling how Go was one of the most promising programming languages.

The main advantage of the Go programming language: parallelization and goroutines

In this second decade of the 2000s it becomes an increasingly pressing need learn the Go language certainly not because it is proposed by Google, because it is now used to run web applications and servers, because it is open source but rather because it has an essential advantage over the others.

Go was created to meet the needs of concurrent programming and was designed to optimize compile times even on modest hardware.

In computer science, concurrency is a characteristic for which a set of processes or subprocesses (thread) are running at the same time. The way Go was conceived, it is possible to take the concept of to the extreme parallelization

The goroutine represent the backbone of Go: they are easy to implement and at the same time very efficient constructs that help to work for thread. In other words, the activities to be carried out are divided into several parallel “lanes” which can operate separately without necessarily having to wait for the conclusion of a previous “slow activity”.

Parallelization in Go can be used to perform multiple operations at the same time which improves performance of the program and increasing itsefficiency.

For this reason Go is used to create different types of programs, including:

  • Web applications. The language can be used to build web servers and RESTful services. Its standard library includes all the tools needed to work with the HTTP protocol.
  • System tools. It is used to create system tools such as compilers, command interpreters, or file system management utilities.
  • Network applications. Given its specifics, Go is very suitable for creating programs that work on the network: proxies, chat servers or programs for transferring files.
  • Microservizi. Go is increasingly used to build microservices, which are small independent programs that work together to provide complex functionality. A great feature in the age of the Internet of Things or IoT.

Per run a goroutine just add the keyword go before the call to the function you want to parallelize:

go myFunction()

This makes the function called myFunction() runs in parallel with the rest of the program thanks to Go.

Goroutines can of course communicate with each other and coordinate their work using i channels.

Each Go program can have only one main goroutine, called main goroutine: it has the task of starting all the other goroutines and of terminating the program when all the other goroutines have concluded their work.

Take this Go code as an example

The two goroutines task1() e task2() run in parallel (note the presence of go in the corresponding calls…).

To simulate a time consuming task, both artificially introduce a 300 millisecond delay.

Screen messages are printed in random order, depending on which goroutine completes its activity first. In this way, the code takes advantage of the parallelization of Go to perform multiple operations simultaneously and improve the overall performance of the program.

Go programming language that is impossible not to know

As can be seen in the example, it is even the second task to show the message first “Starting second activity“.

Removing the two go in front of calls a task1() e task2()the tasks will be executed sequentially (this can be seen from the returned output) but the execution of the program takes longer as it cannot benefit from parallelization.

This other Go code example creates a channel that accepts integers then uses two goroutines that send data over the channel. This information is then printed on screen with the cycle for the final. As mentioned, Go channels allow multiple goroutines to communicate with each other and exchange data.

Go programming language that is impossible not to know

As can be seen, the values ​​entered on the channel all appear when printing them on the screen is requested.

Speaking of CPU stress tests, we have already explained how much the search for prime numbers is a very demanding activity from a computational point of view.

This code example leverages the goroutines to find prime numbers in the range between 1 and 100,000: however, you can freely change the range of your own interest.

The function findPrimeNumbers in the example it is executed as a goroutine and accepts as input word and two integers as parameters (search interval).

One cycle for is used to iterate over the integers in the specified range and check if each number is prime. This last check is performed by making sure, with another cycle for, if the number is divisible by numbers other than 1 and itself. If the number is prime it is sent on the Go channel.

Between the last lines is the call to the function findPrimeNumbers which passes the channel and range of numbers to examine.

The last cycle forat the end of the procedure, does nothing but examine the content of the channel and print it on screen.

Go programming language that is impossible not to know

In the example in the figure, you can see how we proceeded to search for prime numbers between 1 and 1,000,000.

How to try the Go code examples in Windows

To test the examples, simply download the Go language in the Windows version and then install it on your PC. By default Go loads into the folder C:\Program files\Go.

After downloading the examples, you need to rename the files by replacing the extension .txt con .go.

Typing cmd in the Windows search box you can write the following:
cd %programfiles%\Go\bin

At this point, to run the Go code, just type the following command:
go run path\filename.go

In conclusion of this simple excursusyou can start learn Go starting from the many learning resources available on the official website of the project.

Leave a Reply

Your email address will not be published. Required fields are marked *