This article is a mirror article of machine translation, please click here to jump to the original article.

View: 10198|Reply: 1

Pros and cons of programming in Go

[Copy link]
Posted on 5/27/2019 9:57:10 AM | | |
How popular is the Go language? Foreign countries such as Google, AWS, Cloudflare, CoreOS, etc., and domestic companies such as Qiniu and Alibaba have begun to use Go language to develop their cloud computing-related products on a large scale. What Yes and Buts should I pay attention to when using Go language?

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

When choosing a programming language for a project, we always recommend understanding what the project will be building in before considering which programming language to build on. Let the product be the decisive factor in how it should be built.

Here are some of the pros and cons we've found when developing with Go to help you understand if it's a good fit for your next project.

What we love about Go

The use of the Go language has exploded in recent years. It seems like every startup uses it for backend systems. There are many reasons behind the developers considering it so popular.

The Go language is very fast

Go is a very fast programming language. Because Go is compiled into machine code, it naturally outperforms programming languages that are interpretive or have a virtual runtime. Go programs also compile very quickly and produce very small binaries. Our API compiles in just a few seconds, resulting in an executable file that is as small as 11.5MB.

Easy to master

The syntax of Go language is simple and easy to grasp compared to other languages. You can memorize most of the syntax of Go in your head, which means you don't have to spend a lot of time looking for things. The Go language is also very clean and easy to read. Non-Go programmers, especially those who are used to C-style syntax, can read Go program code and understand what is happening.

Static type definition language

Go is a powerful static type-defining language. There are basic types like int, byte, and string. There are also types of structures. As with any strongly typed language, the type system allows the compiler to help catch errors for the entire class. The Go language also has built-in list and mapping types, and they're also easy to use.

Interface type

The Go language has interface types, and any structure can simply satisfy an interface by implementing the interface. This allows you to decouple dependencies in your code. You can then simulate your dependencies in your tests. By using interfaces, you can write more modular, testable code. The Go language also has first-class functions, which makes 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 handling basic types. Some packages make it easy to build a web server, handle I/O, use encryption, and manipulate raw bytes. The JSON serialization and deserialization provided by the standard library is very simple. By using "tags", you can specify the JSON field name next to the struct field.

Testing support

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

Static analysis tools

The static analysis tools for the Go language are numerous and powerful. One particular tool is gofmt, which formats code according to Go's suggested style. This can normalize many of the project's opinions, allowing the team award manager to focus on the work the code has done. We run gofmt, golint, and vet on each build, and if we find any warnings, the build will fail.

Garbage collection

When designing the Go language, memory management was intentionally designed to be easier than C and C++. The object of dynamic allocation is garbage collection. The Go language makes pointer use more secure because it does not allow pointer operations. The option to use value types is also provided.

Easier concurrency model

While concurrent programming is never easy, Go is easier to program concurrently than other languages. Creating a lightweight thread called "goroutine" and communicating with it via "channel" is almost a simple task, but it is possible for more complex models.

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. But the whole of a programming language doesn't just refer to its syntax. Here are some of the problems we encountered.

There are no generics

First, this problem, like an elephant in a room, is an obvious and overlooked fact. There are no generics in the Go language. For developers from languages like Java, switching to Go is a huge hurdle to overcome. This means that the level of reuse of the code is reduced. While the Go language has first-class functions, if you write functions such as "map", "reduce", and "filter", which are designed to operate on one type of collection, you cannot reuse these functions for other different types of collections. There are many ways to solve this problem, but they all end up writing more code, which reduces productivity and maintainability.

The interface is implicit

While it's nice to have interfaces, the structure implements 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 implemented interfaces or not. You can only really understand by trying to compile the program. If the program is small, there is certainly nothing wrong with that. But if this program is medium to large-scale, the trouble is big.

Poor library support

Go library support is uneven. Our API integrates with Contentful, but the latter doesn't have an officially supported Go SDK. This means that we have to write (and maintain!) A lot of code to request and parse data in 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

The Go community may not accept suggestions. Consider this in golint's GitHub repository:The hyperlink login is visible., a user requested that golint be able to fail the build when it found a warning (which is what we do in the project). The defenders immediately dismissed the idea. However, since too many people commented on the issue, after a year, the maintainers finally had to add the requested features.

The Go community doesn't seem to like web frameworks either. While the Go language's HTTP library covers many aspects, it doesn't support path parameters, input checking, and validation, nor does it support 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 nearly impossible to avoid the fate of abandonment.

Fragmented dependency management

For a long time, the Go language did not have a stable, formal package manager. After years of community begging, the Go project only recently released godep. Before that, there were already many tools to fill this gap. We use a very powerful govendor in our project, but this means that the community is divided, which can be very confusing for developers who are new to the Go language. In addition, almost all package managers are powered by Git repositories, and the history of Git repositories can change at any time. Compare it to Maven Central, which never deletes or changes the libraries your project depends on.

Decide whether to use Go or not

Sometimes, you need to think about the machine. When you send and receive bytes. When you manage thousands of concurrent processes. You may 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 performance per nanosecond from the chip.

However, many times, you need to think about humans. Business area data you need to work with: customers, employees, products, orders. You need to write the business logic that operates on these domain entities, and you need to maintain this business logic for many years. And it needs to deal with changing needs, and the faster the better. For these cases, the experience of the developer is important.

Go is a programming language that values machine time more than human time. Sometimes, in your field, machine or program performance is the most critical. 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're going to model the data.

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

  • If you're dealing with bytes, then the Go language might be a good choice.
  • If you're dealing with data, then the Go language may not be a good choice.


This may change one day in the future. The Go language and community are still young. They may surprise us with the addition of generics; Or a popular web framework will win big. However, for now, we will stick with mature programming languages with universal support, mature dependency management, and a focus on business domain modeling.





Previous:Common.LoggingLog configuration
Next:c# discuz POST
Posted on 5/27/2019 10:09:35 AM |
Although I didn't have much contact with the Go language, it was well written
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com