Home >Backend Development >Golang >About the pros and cons of programming in Go language

About the pros and cons of programming in Go language

藏色散人
藏色散人forward
2020-08-24 13:09:443085browse

The following column Golang Tutorial will introduce to you the pros and cons of programming in Go language. I hope it will be helpful to friends in need!

About the pros and cons of programming in Go language

Recently, we wrote an API using Go, an open source programming language launched by Google in 2009. In the process of using Go for development, we gained a lot of experience and insights and wanted to share them with readers, so this article was written.

When choosing a programming language for a project, we always recommend understanding what the project is going to be built in before considering which programming language to use to build it. Let the product be the deciding factor in how it should be built.

Here are some of the pros and cons we found when developing in Go, which can help you understand whether Go is the right language for building your next project.

What we like about Go

Usage of the Go language has exploded in recent years. It seems like every startup uses it for backend systems. Developers believe there are many reasons behind why it is so popular.

Go language is very fast

Go language is a very fast programming language. Because the Go language is compiled into machine code, it will naturally perform better than those programming languages ​​​​that are interpreted or have virtual runtimes. Go programs also compile very quickly and the resulting binaries are very small. Our API compiles in just a few seconds, and the resulting executable is a mere 11.5MB.

Easy to Master

Compared with other languages, the syntax of Go language is very simple and easy to master. You can keep most of Go's syntax in your head, which means you don't need to spend a lot of time looking things up. The Go language is also very clean and easy to read. Non-Go programmers, especially those accustomed to C-style syntax, can read Go code and understand what is going on.

Static type definition language

Go language is a powerful static type definition language. There are basic types such as int, byte and string. There are also structural types. As with any strongly typed language, the type system allows the compiler to help catch errors across classes. The Go language also has built-in list and map types, and they are easy to use as well.

Interface Type

The Go language has interface types, and any structure can satisfy the interface simply by implementing the interface's methods. This allows you to decouple dependencies in your code. You can then mock your dependencies in your tests. By using interfaces, you can write more modular, testable code. The Go language also has first-class functions, which make it possible for developers to write code in a more practical way.

Standard Library

The Go language has a pretty good standard library. It provides convenient built-in functions for working with basic types. There are packages that allow you to easily build a web server, handle I/O, use encryption techniques, and manipulate raw bytes. JSON serialization and deserialization provided by the standard library is very simple. By using "tags" you can specify JSON field names next to struct fields.

Testing support

Testing support is built into the standard library and requires no additional dependencies. If you have a file called thing.go, write your tests in another file called thing_test.go and run "go test". Go will quickly execute these tests.

Static analysis tools

Go language has many and powerful static analysis tools. One tool in particular is gofmt, which formats code according to Go's suggested style. This normalizes many opinions on the project and allows team managers to focus on the work done by the code. We run gofmt, golint, and vet on each build, and if any warnings are found, the build will fail.

Garbage Collection

When the Go language was designed, memory management was intentionally designed to be easier than C and C. Dynamically allocated objects are garbage collected. The Go language makes the use of pointers safer because it does not allow pointer arithmetic. The option to use value types is also provided.

Easier Concurrency Model

Although concurrent programming has never been easy, concurrent programming is easier in Go than in other languages. It is almost as simple as creating a lightweight thread called a "goroutine" and communicating with it through a "channel", and more complex models are also possible.

What we don’t like about Go

As we discussed earlier, Go is indeed an excellent language. It has a clean syntax and is fast to execute. It also has many advantages. However, a programming language is not all about its syntax. Here are some of the issues we encountered.

No Generics

First of all, this problem is like the elephant in the room, an obvious but ignored fact. Go language does not have generics. For developers coming from a language like Java, moving to Go is a huge hurdle to overcome. This means that the level of code reuse is reduced. Although the Go language has first-class functions, if you write functions such as "map", "reduce" and "filter" and design these functions to operate on collections of one type, you cannot reuse these functions on collections of different types. . There are many ways to solve this problem, but they all ultimately involve writing more code, which reduces productivity and maintainability.

Interfaces are implicit

Although it is nice to have interfaces, structures implement interfaces implicitly rather than explicitly. This is said to be one of the strengths of the Go language, but we found it difficult to tell from the structure whether it implements the interface. You can only really know by trying to compile the program. This is certainly not a problem if the program is small. But if the program is medium to large-scale, the trouble will be big.

Poor library support

Go language library support is spotty. Our API integrates with Contentful, which does not have an officially supported Go SDK. This means we have to write (and maintain!) a lot of code to request and parse data from Contentful. We also have to rely on third-party Elasticsearch libraries. Go SDKs provided by vendors are not as popular as their Java, Ruby or JavaScript counterparts.

Community communication is difficult

Go community may not accept suggestions. Consider this issue in golint's GitHub repository: https://github.com/golang/lint/issues/65, a user requested that golint be able to fail the build when a warning is found (this is what we do in the project matter). Maintainers dismissed the idea out of hand. However, so many people commented on the issue that the maintainers ended up adding the requested feature a year later.

The Go community doesn't seem to like web frameworks either. While Go's HTTP library covers a lot of ground, it doesn't support path parameters, input checking and validation, or the cross-cutting concerns common in web applications. Ruby developers have Rails, Java developers have Spring MVC, and Python developers have Django. But many Go developers choose to avoid using frameworks. However, the reality is that there are not no frameworks, on the contrary, there are many. But once you start using a framework for a project, it's almost impossible to avoid abandonment.

Split Dependency Management

For a long time, the Go language did not have a stable, formal package manager. The Go project only recently released godep after years of community begging. There have been many tools that filled this gap before. We use govendor, which is very powerful, for our projects, but this means the community is fragmented and can be very confusing for developers new to the Go language. Additionally, almost all package managers are powered by Git repositories, and the history of Git repositories may change at any time. Compare this to Maven Central, which never removes or changes the libraries your project depends on.

Deciding whether to use the Go language

Sometimes, you need to consider the situation of the machine. When you send and receive bytes. When you manage thousands of concurrent processes. You might also be writing an operating system, container system, or blockchain node. In these cases, most likely you won't care about generics. Because you're busy squeezing every nanosecond of performance out of the chip.

But, many times, you need to consider humans. Business domain data you need to process: customers, employees, products, orders. You need to write business logic that operates on these domain entities, and you need to maintain this business logic over the years. And you need to deal with changing needs, and you have to do it as quickly as possible. For these cases, developer experience matters.

Go is a programming language that values ​​machine time over human time. Sometimes, machine or program performance is most critical in your field. In these cases, Go can be a good C or C alternative. But when you write a typical n-tier application, performance bottlenecks often arise in the database and, more importantly, how you model the data.

When deciding whether to use the Go language, consider the following rules of thumb:

  • If you are dealing with bytes, then Go language may be a good choice.

  • If you are dealing with data, then Go language may not be a good choice.

This situation may change one day in the future. The Go language and community are still very young. They might surprise us and add generics; or a popular web framework might take over. For now, though, we'll stick with mature programming languages ​​that have universal support, mature dependency management, and a focus on business domain modeling.

For more go technical articles, please visit the go language tutorial column!

The above is the detailed content of About the pros and cons of programming in Go language. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete