Hello, everyone, I’m asong. It’s been a long time. It’s updated this week.
There are always some beginners recentlyGoThe language partner asked me what web framework and open source middleware I usually use in business development; So I summarized the libraries I used in my daily development. These libraries are not necessarily perfect, but they can basically solve the needs of daily work. Next, let’s take a look.
Gin is a web framework written in go. It is an API framework similar to Martini but with better performance. Basically, the first web framework for every go beginner is gin. See a comparison of the popularity of various go web frameworks on the Internet:
From the network
We can see that gin ranks first in the community. Gin framework is often the preferred framework for web application development. Many companies will choose to use gin framework for secondary development, adding logging, service discovery and other functions. For example, a set of go micro service framework open source by BiliBili, Kratos has used gin framework for secondary development.
Learning Gin can start quickly through his official documents, but not worry about the English language. I translated a Chinese version and can get it back to my official account and get it back to gin.
GitHub address: https://github.com/gin-gonic/gin
- Zap is an open source log Library of Uber. Choosing zap has two advantages:
- It’s very fast
It provides both structured logging and printf style logging
Most log libraries are basically based on reflection serialization and string formatting, which will occupy a lot of CPU resources on the log and is not suitable for business development scenarios. The business is still very sensitive to performance. Zap adopts different methods. It designs a JSON encoder without reflection and zero allocation, and the basic logger tries to avoid serialization overhead and allocation as much as possible. By building an advanced sugarlogger on this basis, zap allows users to choose when each allocation needs to be calculated and when they prefer a more familiar loosely typed API.
Zap benchmarks are as follows:
From official documents
It can be seen that the efficiency of zap is completely higher than that of other log libraries. I don’t need to tell you who to choose!!!
GitHub address: https://github.com/uber-go/zap
Business development is inseparable from JSON serialization and deserialization. Although the standard library provides encoding / JSON, it is mainly realized through reflection, so the performance consumption is relatively large. Jsoniter can solve this pain point. It is a fast and flexible JSON parser with good performance and 100% compatibility with the standard library. We can use jsoniter instead of encoding / JSON. The official document says it can be more than 6 times faster than the standard library. Later, the go official used sync.pool to cache the decoder for the json.unmarshal function in go1.12, The performance is improved compared with the previous version, so it is less than 6 times faster now.
From official documents
GitHub address: https://github.com/json-iterator/go
For those interested in the jsoniter optimization principle, you can move here: http://jsoniter.com/benchmark.html#optimization -used
Gorm is an ORM framework written in go language. It has complete documents, is friendly to developers, and supports mainstream databases: MySQL, PostgreSQL, SQLite and SQL server.
Personally, I think the biggest advantage of using Gorm is that it is developed by Chinese people, with complete Chinese documents and quick start. At present, most enterprises are also using Gorm. Let’s talk about Gorm’s features:
- Full function ORM
- Association (has one, has many, belongs to, many to many, polymorphic, single table inheritance)
- Hook methods in create, save, update, delete, find
- Supports preloading of preload and joins
- Transaction, nested transaction, save point, rollback to saved point
- Context, precompiled mode, dryrun mode
- Batch insert, findinbatches, find / create with map, crud using SQL expression and context valuer
- SQL builder, upsert, database lock, optimizer / index / comment hint, named parameter, subquery
- Composite primary key, index, constraint
- Auto Migration
- Custom logger
- Flexible extensible plug-in API: database resolver, Prometheus
- Each feature has been tested
- Developer friendly
GitHub address: https://github.com/go-gorm/gorm
Official documents: https://gorm.io/zh_ CN/docs/index.html
GitHub address: https://github.com/robfig/cron
Business development is inseparable from the use of timers. Cron is a library for managing scheduled tasks. The effect of crontab command in Linux is implemented with go, which is similar to the crontab command in Linux. Cron library supports fields separated by five spaces to represent time. Cron is also very easy to get started. Take an official example:
For the use of cron, please refer to this article: https://segmentfault.com/a/1190000023029219
I also wrote an article on the basic use of cron before. You can refer to the following: https://mp.weixin.qq.com/s/Z4B7Tn8ikFIkXVGhXNbsVA
It’s been 1202 years. No one should not know the role of dependency injection. We can also implement dependency injection ourselves, but this is in the case of less code and uncomplicated structure. When the relationship between structures becomes very complex, manually creating dependencies and then assembling them will become extremely cumbersome and error prone. The go language community has many dependency injection frameworks, which can be divided into two categories:
Runtime dependency injection for dependency reflection implementation: inject, Uber, dig
Dependency injection using code generation: wire
Personally, using wire for project management is the best. Dependency injection problems can be found in the code compilation stage. They can be reported when the code is generated and will not be reported until the runtime, which is more convenient for debugging.
The use of wire is also very simple. I also wrote an article about the use of wire before. You can refer to: https://mp.weixin.qq.com/s/Z4B7Tn8ikFIkXVGhXNbsVA
GitHub address: https://github.com/google/wire
Goroutine pool is also used in some business scenarios. Ants is a widely used goroutine pool, which can effectively control the number of processes and prevent too many processes from affecting program performance. Ants is also developed by Chinese people, and the design blog is also very detailed. At present, many large factories are also using ants and have experienced online business inspection, so they can be used at ease.
GitHub address: https://github.com/panjf2000/ants
The ants source code is less than 1K lines. I suggest you appreciate the source code ~.
Several libraries listed in this article are often usedOpen source library, you have mastered these libraries, and there is no problem with basic business development. Some beginners can reach the entry level through these libraries. There are also some libraries, such as go redis, go SQL driver, Didi / gendry, golang / groupcache, olive / elastic / V7, etc. these libraries are also frequently used. The introduction is relatively simple, so I won’t introduce them in detail here.