📜 ⬆️ ⬇️

Go language: development, ORM choice

Go has gained fame as one of the simplest programming languages, including its simplicity in writing and reading code, in most cases, simpler support for the code base. We talk about several advantages of Go, thanks to which we at SimbirSoft used it in a number of high-loaded projects with various architectures, both web-based and microservice (SOA).

Application


This system language is good for most network tasks, any microservices and macroservices, web services, and also for system tools - it’s not for nothing that many DevOps experts love it. Go finds its use in Machine Learning, and some developers even write websites with it. In our practice, we turned to Go when we developed separate CRM and software products for internal use, for example, for insurance companies. The standard Go language repository also has a package for mobile development, this package is still supported, although there are more suitable tools for this.

Go: development and recognition


At Google, the development of the Go language began in 2007. Prior to this, the company used Java, Python and C ++, however, they had certain drawbacks (in particular, high memory consumption) or limited use. Then, Google began to develop a new language for their needs.
')
What are the requirements for the language:


For those who do not work with Go, we briefly recall the history of its creation. The language specification on Google began to be thought out in 2007. Release 1.0 took place in 2012, and in 2015 release 1.5 was released, already without a single line of C ++ code. From this point on, the language compiles itself, which made it possible to speed up the development of individual tasks on Go.

The average age of recognition of a new language in the community is about 10 years, but Go is already among the most requested languages. In June 2019, Go rose to 15th place in popularity in the world, according to the TIOBE index, adding three positions in a year. Among the reasons for its success are the following:


For example, the following packages are included in the standard library (the list is incomplete):



Illustration - github.com/ashleymcnamara/gophers

If you are not satisfied with the speed of your application or other parameters, you can work with the profiler from the “box”. For this you need:

Go tools


One of the advantages of Go is an extensive set of standard tools. We chose some of the most interesting utilities, in our opinion (in fact, there are many more):


Competitiveness


Another strength of Go is that it is easy to write competitive code in this language. To do this, you simply use keywords before calling the function.



Learning go


If you want to program on Go, the start will take relatively little time. The developers - Core Team - support the A Tour of Go course in several languages, including Russian. This course can be taken literally 1-3 days. In terms of learning, Go is one of the easiest languages.

So, what to read:


ORM in Go


There are opinions that one of the weak points of Go are ORM. Perhaps this is partly due to the fact that the language is young - it is just starting to develop many of the necessary tools. In particular, you can use the database / sql package for working with databases. What are the requirements for the package:


Sample SELECT query using database / sql


defer rows.Close() for rows.Next() { var message string err := rows.Scan(&message) if err != nil { log.Panic(err) } fmt.Println(message) } if err := rows.Err(); err != nil { log.Panic(err) } 

As you can see from the example, a lot of template actions will have to be performed every time. For example:


From all this, I want to use something more universal and simple, for example, ORM.

Types of implementations of ORM


When developers are faced with the question of choosing ORM to work with Go, they are often rated by githubs. When we chose ORM for ourselves, Beego had the best rating (18 thousand “stars”). However, in practice, ORM is part of the framework, so the assessment is not completely objective (if you choose only ORM). Also among the leaders Gorm, which has been developing since 2013, supports transactions and has many other advantages.



However, as our experience of acquaintance with Gorm has shown, certain problems are possible when using it, especially when testing: for example, a data race or deleting a foreign key. We can assume the following causes of similar errors in the Gorm:


At the moment, in our work, we prefer several ORMs that do not use the interface {} - this is Kallax, Reform and Sqlboiler. Usually we use the Golang package for migrations and the orm-bench repository with ORM benchmarks on Go. In this case, you should not chase only for benchmarks. More often, speed is not as important as the quality of the code and its functionality.

Summing up


The benefits of the Go language for the developer and for business include increased development productivity, the ability to use fewer servers, syntactic ease and quick training for new developers, a large set of tools and a friendly community. Despite the fact that it is a young language, it is constantly growing and developing. Go features make it the optimal language for performing various projects, from network tasks to microservices and blockchain.

The article was prepared on the basis of our report at the Hot Backend in Samara.

Source: https://habr.com/ru/post/458074/


All Articles