Go language: ORM selection

    Go gained fame as one of the simplest programming languages, including its advantages - ease of 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 highly loaded projects with various architectures, both web-service and micro-service (SOA).

    Application


    This system language is good for most network tasks, any microservices and macro services, web services, as well as for system tools - it is not without reason 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 CRMs 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, development of the Go language began in 2007. Before that, 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:

    • One executable file (no dependencies)
    • Fast compilation
    • Fast runtime (at least on par with Java and C ++)
    • Garbage collector minimizing resource leakage
    • Optimization for multiprocessor systems
    • Static typing
    • Minimalism
    • Syntactic lightness
    • Multiparadigmness (imperative, functional, object-oriented)

    For those who do not work with Go, briefly recall the history of its creation. Google began to think up the language specification 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 that moment on, the language compiles itself, which allowed us to accelerate 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 one of the most popular languages. In June 2019, Go climbed 15th in terms of popularity in the world, according to the TIOBE index , adding three positions in a year. Among the reasons for its success are the following:

    • Strong standard library
    • Cool standard tuning
    • The most necessary functions are presented in the “box”, and libraries are given to open source
    • Very friendly community

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



    Illustration - github.com/ashleymcnamara/gophers

    If you are not comfortable with the speed of your application or other parameters, you can work with the profiler out of the box. To do this, you need:
    • Register profiler import: import _ "net / http / pprof"
    • Add an http server to receive results from the profiler: go http.ListenAndServe ("0.0.0.0:8080", nil)

    Go tools


    One of the benefits of Go is its extensive set of standard tools. We have selected some of the most interesting utilities, in our opinion (in fact, there are many more):

    • benchcmp - benchmark comparison, performance before and after changes
    • go tool cover - analysis of code coverage by tests
    • godoc - comment-based documentation generation
    • goimports - import analysis (removing unused, adding missing)
    • gorename - refactoring
    • present - presentation tuning
    • go vet - frequently encountered error linter
    • go test - run tests and benchmarks
    • go fix - fix changes when changing the API (before release 1.0)
    • go tool pprof - profiling
    • Go fmt - code formatting by code style
    • Race detector - finding a data race in runtime

    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.



    Go Training


    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 completed in just 1-3 days. In terms of learning, Go is one of the simplest languages.

    So what to read:


    ORM in Go


    It is believed that one of Go's weaknesses is ORM. Perhaps this is partly due to the fact that the language is young - in it, many necessary tools are just beginning to be developed. In particular, you can use the database / sql package to work with databases. What are the requirements for the package:

    • General package for all sql databases
    • Portable: does not contain any features in the field of working with databases. Ideally, everything should work correctly if you change the driver.
    • Converting Go types to standard database types (it should be possible to process specific types)
    • Contains connection pool
    • Thread-safety for goroutines

    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 instance:

    • close row object: defer rows.Close ()
    • check for errors err: = rows.Err (); err! = nil since it’s not clear why rows.Next () returned us false

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

    Types of ORM Implementations


    When developers are faced with the choice of ORM for working with Go, they are often guided by the rating on the github. 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 quite objective (if you select only ORM). Also among the leaders, Gorm, which has been developing since 2013, supports transactions and has many other advantages.



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

    • Commits immediately in master
    • Missing tags
    • Unpredictable sql builder

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

    To summarize


    The advantages of the Go language for developers and for businesses include increased development productivity, the ability to use fewer servers, the syntactic ease and faster training time 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's features make it the optimal language for a wide variety of projects, from network tasks to microservices and blockchain.

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

    Also popular now: