Skip to content

Introduction to the Go programming language

27 mayo, 2021

In order not to lose the habit, today I am going to do another introductory article. This time it will not be HTML5 or CSS3 or Python or any other dynamic language. Today I am going to do a introduction to the Go programming language which is a compiled language.

Go is a new programming language for systems released by the almighty Gororgland in November 2009. Although it began to be developed in September 2007 by Robert Griesemer, Rob Pike and Ken Thompson.

Go is a programming language compiled, concurrent, imperative, structured, not object-oriented – in a rather special way – and with garbage collector which is currently supported in different types of systems UNIX, including Linux, FreeBSD and Mac OS X. It is also available in Plan 9 since part of the compiler is based on previous work on the Inferno operating system. The supported architectures are i386, amd64 and ARM[1]. There is an unofficial port to Microsoft Windows but it is quite unstable. Work is currently underway to bring it to the Microsoft operating system.

Beginning lesson

Since Go was announced, an immense hype around it. There was no shortage of those who rushed to baptize him as the next C —There have been many next Cs that stayed on promises— the main difference when trying Go seriously is that it leaves you with a very pleasant feeling that indicates that really this time there could be some truth behind that claim.

After programming something more complex with him than a “Hello World!“Leave a nice feeling and the following elements are quickly detected in the language that reveal that behind all the scarecrow of fanboyism and hype there is substance:

  • Go uses a C-like syntax so programmers who have used that language feel very comfortable with the
  • Go usa static typing (statically typed) and is as efficient as C
  • Go has many of the features and ease from dynamic languages ​​like Python
  • Even being a language designed for system programming, it provides a garbage collector, reflection and other high-level capabilities that make it a very powerful language.
  • Go is not object-oriented because there is no type hierarchy but it implements interfaces

Characteristics

Go, like C and C ++, is a compiled language, obviously, it shares characteristics with other compiled languages, but the truth is that it has some unique characteristics, let’s see some

Concurrent

It is a concurrent language that supports communication channels based on the CSP language of Sir Charles Antony Richard Hoare —Which I already talked about in my first article in Genbeta Dev as he is the creator of the QuickSort sorting algorithm and winner of the Turing in 1980—. Concurrency in Go is different than lock-based scheduling criteria like pthreads.

Garbage collector

Like most modern languages, Go implements a garbage collector. Go’s garbage collector is being reimplemented to maximize efficiency and reduce latency everything possible.

Simplicity

The most widely used languages ​​like C ++, Java or C # are more heavy and bulky. Simplicity is the main feature of Go, its syntax is clear and concise. While C is infamous for the complexity of its declaration syntax, Go uses implicit inference of types being able in this way to avoid the explicit declaration of variables. The declaration of variables is simple and convenient, and differs from that of C.

Duck Typing

Go supports the dynamic typing data also known as duck typing present in many dynamic languages ​​such as JavaScript, Ruby or Python. A struct can implement an interface of form automatic, which is a powerful and novel feature.

Goroutines

They are neither threads, nor coroutines nor processes. Communication between goroutines is done through a language feature called channels -Based on CSP-, which is much more secure and easier to use than the prevailing systems based on locks pthreads or modern Java features

Exceptions

Go has no exceptions. The creators of the language give various reasons for this. One of them is that adding an exception layer adds unnecessary complexity to the language and runtime environment. By definition they should be exceptional but in the end they end up being used as controllers of the flow of the application and they stop having nothing of exceptional. According to the creators, the exceptions have to be really exceptional and the use that is given mostly does not justify his existence.

Innovation

For many years, developers have had to choose between compiled statically typed programming languages, generally quite complex but providing great features related to optimization and performance, and dynamically typed languages ​​interpreted with much higher-level features that made learning, using and syntax easier and therefore fun to program in them, yes, sacrificing performance and control.

Go mixes the best of both worlds and it gives us a simple, clear and fun syntax along with the power offered by a strongly typed and compiled language, also incorporating high-level features that facilitate the use of the language by developers.

Main differences with C

Although its syntax is similar, Go differs a lot from C, let’s see some examples

Upside down statements

In Go the declarations are made upside down from the perspective of C (or C ++ or Java). The main idea in C is that a variable is declared as an expression that denotes its type. According to the creators, although the idea behind the type declaration in C is good, the types and grammatical expressions do not mix very well and the result can be confusing. Go however, separates the expression and the type syntax which simplifies things (the prefix * for pointers is the exception that the rule confirms). A typical statement in C would be:

int* i, j;

Declares that i it’s a pointer, but no j. In Go:

var i, j *int

The two variables are declared as pointers. It is clearer and more natural.

Semicolon

In Go the use of semicolon “;“At the end of an instruction is optional.

Pointer arithmetic

Go does not have pointer arithmetic, according to the creators, the reason is security. Without pointer arithmetic it is possible to create a language in which you cannot get an illegal address that is used incorrectly. The lack of pointer arithmetic simplifies implementation from the garbage collector. Also, with compiler optimization and current hardware, a loop that uses the indexes of an array can be as efficient as a loop that uses pointer arithmetic.

++ and –

In Go, using ++ and – to increase and decrease the value of a variable is a judgment and not an expression. Furthermore, it can only be used in its postfixed version because according to the authors, the prefix version loses all its meaning in the absence of pointer arithmetic.

Is Go an object-oriented language?

Well, this is not an easy question to answer. Let’s say Yes and no. Go has types and methods and allows a style object-oriented programming but there is no object hierarchy, Thus, there is no inheritance. In Go, the concept of “Interface“Different than the creators believe it is easy to use and in many ways it is more general. There are also ways to embed types within other types to obtain something analogous to subclasses. Go’s methods are more general than those of C ++ or Java, they can be defined for any type of data not only for structs.

The absence of a type hierarchy in Go makes the “objects“They give a feeling of being lighter than in languages ​​like C ++ or Java.

The example

How could it be otherwise, I am going to put the simplest possible example to write with Go:

package main
import "fmt"
func main() {
    fmt.Printf("Hola, mundo!n")
}

All Go files must declare the package to which they belong through a sentence package. They can also import modules and external functions from other packages, in this case we use the package functions “fmt“Which gives us access to the function Printf. All files in Go are UTF-8. To compile the above code we would use the Gc compiler or the gcc backend.

Conclution

In this introduction to the Go programming language We have seen the basic characteristics of the new programming language of Gororgland and we have seen its most remarkable differences with other compiled languages. Still, I have left a lot in the pipeline on essential topics such as its different compilers and its installation. Do not worry, in future installments I will talk more in depth about this programming language of which there is still not much documentation in Spanish.

* Note 1: Less on Mac OS X

More information | Official language page