What is and what is not in Go. Part 1

Hello everyone, on the eve of the start of a new thread on the “Golang Developer” course , we want to share with you a translation of an interesting article, in the first part of which we understand the elements that are in Go. Go.







Go was created with a look back, and its basic configuration is really well-composed: it has garbage collection, packages, first-class functions, lexical scope, system call interface and immutable lines, the text of which is usually encoded in UTF-8. But it has relatively few features and is unlikely to increase their number. For example, it has no implicit numerical conversions, no constructors or destructors, no operator overloads, no default parameter values, no inheritance, no generics, no exceptions, no macros, no function annotations, and no local stream storage.



Before reading : Above is an excerpt from the book by Alan A. A. Donovan and Brian W. Kernigan “Go Programming Language”. page xiv. The items mentioned below are a brief and somewhat incomplete explanation of the terms used in designing a programming language. I tried to explain all the concepts in terms of programming on Go. All points mentioned below are not my authorship, they are taken from other extracts. I in no way protect Go or any other language.



Now we will try to understand each term in brief. For a beginner in general programming, knowing these terms is very important. The meaning of all these concepts is applicable in every programming language. These terms can help you distinguish between different languages ​​at a fundamental level.



Elements that are in Go



Garbage collection



This mechanism is present in any programming language that performs automatic memory management. To understand garbage collection or memory management, you first need to understand how memory itself works. When working with a programming language, the compiler assigns various memory spaces in the system to store data, for example, creating a variable, looping through an array, etc. Allocation and redistribution of memory must be implemented to make the program work with memory more efficient.



In a language like C, memory is managed manually. If you are familiar with C, you know that there is a malloc function that dynamically allocates memory in the system. In a high-level language such as JavaScript or Python, this allocation is automatically performed by a program known as a garbage collector. As the name implies, its job is to manage memory, to allocate space when it is needed, and to free allocated memory when it is no more. Go implements garbage collection, so the programmer does not need to worry about memory management.



Packages



Packaging is the combination of all source code and resources into a single object called a package. The software package is convenient in many ways, such as easy installation, sharing, adding, debugging, etc. Go has an integrated package system that integrates documentation, binaries, and source code files. The purpose of packaging is to use other software projects in your software without having to manually copy the source code.



First class functions



A first-class function is a function that can be processed like any other variable, that is, it can be assigned, returned, exported, passed as a parameter, etc. Look at the following snippet written in Go. The function that prints the string hello world first class function



assigned to the variable a



. The variable a behaves like an actual value in memory, however it can also be called as a function by adding ()



at the end. You can also see that the value of a is displayed in the same way as any other variable. This is the basic concept of first class functions.



 package main import ( "fmt" ) func main() { a := func() { fmt.Println("hello world first class function") } a() fmt.Printf("%T", a) }
      
      





Lexical scope



A scope in a program is similar to a block or scope in which the definition of any variable / function is valid. For example, a variable declared inside a function retains its value only inside this function block, that is, between curly braces {}



. If you try to access the value of such a variable outside this function block, the program will not be able to find it.



 package main import “fmt” func main() { { v := 1 { fmt.Println(v) } fmt.Println(v) } fmt.Println(v) // “undefined: v” compilation error }
      
      





In the given fragment there are four visibility areas. The first is the global region, the second is the main ()



function, the third is the first block inside the main



function, and the fourth is the region where fmt.Println



is called for the first time. Of the three Println



latter throws a compilation error. This is because the definition of v



is only available in areas three and four. When Println



is called with v



passed as a parameter, the program first looks for its definition in the current area, when it cannot find it, it moves outside in the scope of the parent and will continue to do this until it finds its definition. This is the lexical scope - the program begins to search for the definition of variables and functions from the area in which they are used / called, and moves out. In the last fmt.Println



program, it was not possible to find the definition of v



in the current or in any parent areas, so it throws a compilation error.



System call interface



Go comes with a system call interface that serves as a reference to system calls available to the operating system. For example, opening and reading a file, input and output, etc. It intercepts function calls in the API and calls the necessary system call in the operating system.



Immutable Rows



Although Go's syntax is similar to C, it has an advantage over it in the form of immutable lines that are encoded in UTF-8. That is, programs written in Go can form strings using many languages ​​and characters. In a simple sense, strings are a combination / array / list of characters in programming languages. Since strings are formed by combining characters, their composition can be changed. Characters can be added, deleted, moved, etc. We consider a method that guarantees the immutability of a string after its declaration. The concept of immutable strings is not new, in Python String instances objects cannot be mutated, JavaScript also has immutable strings, and Ruby added Frozen String Literals in 2.3. But, nevertheless, many popular languages, such as C ++, PHP, Perl, etc., do not have immutable strings.



So, on this the first part of the article came to an end. In it, the elements that are in Go were disassembled, and about what is not in Go, you can read in the second part of the article .



All Articles