About the pros and cons of programming in Go language
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!
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to use Go language to write the user feedback module in the door-to-door cooking system? With the rise of takeout and door-to-door services, more and more users choose to enjoy delicious food at home. For door-to-door cooking services, user feedback is particularly important, which can help improve service quality and user satisfaction. This article will introduce how to use Go language to write the user feedback module in the door-to-door cooking system, and provide specific code examples. Database design and creation First, we need to design a database to store user feedback information. Suppose we have a feed called

Beego is one of the commonly used web frameworks in the Go language. It has the advantages of rapid development, binary deployment, and high concurrency. However, in a high concurrent request environment, the performance optimization needs of the Beego framework are highlighted. This article will introduce how to optimize Beego's performance through reasonable configuration, optimized code, cache, etc. 1. Use an efficient caching mechanism. Caching can greatly improve application performance and reduce the number of database queries. The Beego framework’s caching mechanism is also very simple and easy to use, and can be applied to different scales.

How to improve the efficiency of Go language programming? Why is Go language so important to programmers? With the rapid popularity of Go language in the field of software development, more and more developers are paying attention to this programming language. The Go language has been widely praised for its simplicity, efficiency, and ease of use, and has gradually become a mainstream programming language. So, how can we effectively use Go language for programming? 1. Make full use of the concurrency features of Go language. The concurrency model of Go language is one of its biggest features. Through goroutine and

Go language is an efficient programming language that is increasingly used in distributed systems. At the same time, the optimistic locking mechanism has also become an important tool for developers to deal with concurrency issues. This article will explore distributed systems and optimistic locking in the Go language. 1. What is a distributed system? Distributed System refers to a system composed of multiple computers that are connected to each other through a network to complete tasks together. Distributed systems can improve system reliability and throughput. in distributed

How to use Go language to write the delivery staff working time management module in the door-to-door cooking system? With the booming development of the takeout market, the door-to-door cooking system has also become a convenient choice in people's lives. In the door-to-door cooking system, the delivery person is a crucial part. Good working time management is very important for delivery staff's work efficiency and user experience. This article will introduce how to use Go language to write the delivery staff working time management module in the door-to-door cooking system, and provide specific code examples. Demand Analysis In the door-to-door cooking system, delivery personnel usually need to

How to deal with the file system directory traversal problem of concurrent files in Go language? In daily development, we often need to traverse files in the file system. In the Go language, by utilizing the features of goroutine and channel, we can easily perform concurrent file system directory traversal. First, we need to introduce the filepath package and os package to complete file system related operations. The specific code is as follows: import("fmt"

How to use Go language to write the user account management module in the door-to-door cooking system? In modern fast-paced life, more and more people choose to solve their dietary problems through door-to-door cooking services. For this service provider, a complete and sound user account management system is very important. This article will introduce how to use Go language to write a user account management module, aiming to help home cooking service providers better manage users' account information, and provide specific code examples. 1. Create a database table. First, you need to create a user.

As the takeout market matures, home cooking has become the first choice for many families for dinner. As a provider of door-to-door cooking services, it is essential to provide reliable user account recharge. This article will introduce how to use Go language to write the user account recharge module in the door-to-door cooking system. 1. Design When designing the recharge module, we need to consider the following aspects: The data structure to be used is in the recharge module, and we need to store the user's balance before and after recharge. Therefore, we can use the following data structure: typeAccount
