Go daily develops standing third-party libraries and tools

Keywords: github grpc gorm

It has been almost a year since I wrote Go, and several large and small projects have been launched; Mentality has also experienced several rounds of changes.

Because I personally wrote Java in the first five years and Python in the middle of the course for more than a year, when I first came into contact with Go, I felt as follows:

There is neither the ecology of Java nor so much syntax sugar as Python.

The feeling of writing now is:

Here we will not discuss who is strong and who is weak in these languages; Focus on sharing with you some third-party libraries and tools used in our daily development.

Here, I mainly divide these libraries into two categories:

  • Business development
  • Basic tool development

Business development

The first is business development, which mainly includes web, database, Redis, etc.

Gin ⭐️⭐️⭐️⭐️⭐️

First Gin , an HTTP framework with simple use, excellent performance and numerous data; When you're still hesitant about which framework to choose, choose it. It's basically right.

Of course, it goes with it github.com/swaggo/gin-swagger Swagger tools are just needed; It can be used to generate swagger documents.

GORM ⭐️⭐️⭐️⭐️⭐️

GORM There's nothing to say. If you like the way orm operates the database, choose it; Similarly, it is simple to use and has more data.

If there is a need for read-write separation, you can also use the plug-in officially provided by GORM https://github.com/go-gorm/dbresolver It is also very simple to use with GORM.

errors ⭐️⭐️⭐️⭐️⭐️

The error handling provided by the Go language itself is relatively simple, https://github.com/pkg/errors It provides more powerful functions, such as:

  • Abnormal packaging
  • Packing stack, etc.

The following API s are commonly used:

// WithMessagef annotates err with the format specifier.
func WithMessagef(err error, format string, args ...interface{}) error

// WithStack annotates err with a stack trace at the point WithStack was called.
func WithStack(err error) error

zorolog ⭐️⭐️⭐️⭐️⭐️

There are many log printing libraries in Go. The best way to use logs in daily development is to have a low sense of existence; In other words, it has strong performance (it can't affect business code) and easy to use API.

log.Debug().Msgf("OrderID :%s", "12121")


https://github.com/qax-os/excelize It is a library for reading and writing Excel. Basically, it can realize all Excel operations you can encounter.

now ⭐️⭐️⭐️⭐️

https://github.com/jinzhu/now Is a time tool library:

  • Get the current month, day, hour, minute and second.
  • Different time zones are supported.
  • Last week, last month, etc.
import "github.com/jinzhu/now"

time.Now() // 2013-11-18 17:51:49.123456789 Mon

now.BeginningOfMinute()        // 2013-11-18 17:51:00 Mon
now.BeginningOfHour()          // 2013-11-18 17:00:00 Mon
now.BeginningOfDay()           // 2013-11-18 00:00:00 Mon
now.BeginningOfWeek()          // 2013-11-17 00:00:00 Sun
now.BeginningOfMonth()         // 2013-11-01 00:00:00 Fri
now.BeginningOfQuarter()       // 2013-10-01 00:00:00 Tue
now.BeginningOfYear()          // 2013-01-01 00:00:00 Tue

now.EndOfMinute()              // 2013-11-18 17:51:59.999999999 Mon
now.EndOfHour()                // 2013-11-18 17:59:59.999999999 Mon
now.EndOfDay()                 // 2013-11-18 23:59:59.999999999 Mon
now.EndOfWeek()                // 2013-11-23 23:59:59.999999999 Sat
now.EndOfMonth()               // 2013-11-30 23:59:59.999999999 Sat
now.EndOfQuarter()             // 2013-12-31 23:59:59.999999999 Tue
now.EndOfYear()                // 2013-12-31 23:59:59.999999999 Tue

now.WeekStartDay = time.Monday // Set Monday as first day, default is Sunday
now.EndOfWeek()                // 2013-11-24 23:59:59.999999999 Sun

Decimal ⭐️⭐️⭐️⭐️

When business requires precision calculation https://github.com/shopspring/decimal Can help.

import (

func main() {
	price, err := decimal.NewFromString("136.02")

	quantity := decimal.NewFromInt(3)
	fee, _ := decimal.NewFromString(".035")
	taxRate, _ := decimal.NewFromString(".08875")

	subtotal := price.Mul(quantity)

	preTax := subtotal.Mul(fee.Add(decimal.NewFromFloat(1)))

	total := preTax.Mul(taxRate.Add(decimal.NewFromFloat(1)))

	fmt.Println("Subtotal:", subtotal)                      // Subtotal: 408.06
	fmt.Println("Pre-tax:", preTax)                         // Pre-tax: 422.3421
	fmt.Println("Taxes:", total.Sub(preTax))                // Taxes: 37.482861375
	fmt.Println("Total:", total)                            // Total: 459.824961375
	fmt.Println("Tax rate:", total.Sub(preTax).Div(preTax)) // Tax rate: 0.08875

Basically, it can do all the precision conversion you can think of; In addition, GORM can also declare the model field as the type of decimal, and the database corresponds to decimal, which is more convenient to use.

Amount decimal.Decimal `gorm:"column:amout;default:0.0000;NOT NULL" json:"amout"` 

configor ⭐️⭐️⭐️⭐️

https://github.com/jinzhu/configor It is a configuration file reading library and supports YAML/JSON/TOML and other formats.

go-cache ⭐️⭐️⭐️

https://github.com/patrickmn/go-cache It is similar to the Guava cache in Java. It is thread safe and easy to use; Simple scenarios that do not require distributed caching can be considered.

	c := cache.New(5*time.Minute, 10*time.Minute)
	// Set the value of the key "foo" to "bar", with the default expiration time
	c.Set("foo", "bar", cache.DefaultExpiration)

copier ⭐️⭐️⭐️

https://github.com/jinzhu/copier You can see from the name that this is a data replication library, which is similar to BeanUtils.copy() in Java; Two struct s with the same field but different objects can be copied, and deep copy is also supported.

func Copy(toValue interface{}, fromValue interface{}) (err error) 

It is very useful when we need a temporary struct to store data, especially when there are many fields in a struct, it takes a little time to assign values back and forth.

However, be careful not to use it in all cases, which will bring some disadvantages:

  • You cannot take advantage of compiler prompts when deleting fields.
  • When some fields require additional manual processing, the code is not easy to read.
  • Reflection assignment has a certain performance loss.

In short, it is recommended to write manually during business development. After all, the code is for people to see.

env ⭐️⭐️⭐️

https://github.com/caarlos0/env This library can convert our environment variables into a struct

type config struct {
	Home string `env:"HOME"`

func main() {
	cfg := config{}
	if err := env.Parse(&cfg); err != nil {
		fmt.Printf("%+v\n", err)

	fmt.Printf("%+v\n", cfg)

This is very useful when we package code to different running environments. We can easily obtain different environment variables by using it.

user_agent ⭐️⭐️⭐️

https://github.com/mssola/user_agent Is a gadget for formatting user agent.

When we need to collect user agen on the server, we can read the data faster.

func main() {
    ua := user_agent.New("Mozilla/5.0 (Linux; U; Android 2.3.7; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1")

    fmt.Printf("%v\n", ua.Mobile())   // => true
    fmt.Printf("%v\n", ua.Bot())      // => false
    fmt.Printf("%v\n", ua.Mozilla())  // => "5.0"
    fmt.Printf("%v\n", ua.Model())    // => "Nexus One"
    fmt.Printf("%v\n", ua.Platform()) // => "Linux"
    fmt.Printf("%v\n", ua.OS()) 

phonenumbers ⭐️⭐️⭐️

https://github.com/nyaruka/phonenumbers Mobile phone number verification library, you don't have to write your own regular expression.

// parse our phone number
num, err := phonenumbers.Parse("6502530000", "US")

Basic tools

Next, there are some basic tool libraries, including some mainstream storage clients, middleware, etc.

gomonkey ⭐️⭐️⭐️⭐️⭐️

github.com/agiledragon/gomonkey It is a mock piling tool. When we write unit tests, it is difficult to mock some non interface functions, so we need to use it.

Since it modifies the machine jump instruction when calling the corresponding function, and the instructions corresponding to different CPU architectures are also different, it is not compatible with Apple's M1 chip when we use it, but it should be compatible at present. You can try it.

goconvey ⭐️⭐️⭐️⭐️⭐️

https://github.com/smartystreets/goconvey It is also a library for unit testing and can be compatible with the go test command.

  • Provides a visual web UI.
  • Display unit coverage integrated with IDE.

dig ⭐️⭐️⭐️⭐️⭐️

https://github.com/uber-go/dig This is a dependency injection repository. We will not discuss whether dependency injection should be used for the time being. At least we have several advantages in using it at present:

  • All objects are singletons.
  • There is a unified local management object.
  • When in use, you can directly pass the object as a parameter (the container will be injected automatically).

Of course, there are some inconvenient places:

  • When unfamiliar, it is unclear how an object is created.
  • The code is not easy to understand.

We have developed a business framework internally, in which all objects are managed by dig, which is more convenient to use.

cobra ⭐️⭐️⭐️⭐️

https://github.com/spf13/cobra It is a powerful command line tool library. We use it to implement internal command line tools. It is also recommended to use https://github.com/urfave/cli/ Personally, I will be more used to the latter and be concise.

BloomRPC ⭐️⭐️⭐️⭐️

https://github.com/uw-labs/bloomrpc A gRPC visualization tool is much simpler than writing gRPC client code.

But there are also some small problems, such as accuracy. If int64 exceeds the value obtained by the 2 ^ 56 server, an error will occur, which has not been solved yet.

redis ⭐️⭐️⭐️⭐️

https://github.com/go-redis/redis/ Redis client, not much to say; It has been developed for many years, and all the functions it should have have have been provided.

elastic ⭐️⭐️⭐️⭐️

https://github.com/olivere/elastic This is also a very mature elasticsearch library.

resty ⭐️⭐️⭐️⭐️

https://github.com/go-resty/resty/ An http client is very simple to use:

// Create a Resty Client
client := resty.New()
resp, err := client.R().

It smells like Python requests package.

pulsar-client-go ⭐️⭐️⭐️

The go language client officially produced by Pulsar is almost stepmother compared with Java; There will be fewer functions, and the update is not so active; But I have no choice.

go-grpc-middleware ⭐️⭐️⭐️

https://github.com/grpc-ecosystem/go-grpc-middleware The officially provided gRPC middleware can realize some internal authentication, metadata, log and other functions.

go-pilosa ⭐️⭐️⭐️

https://github.com/pilosa/go-pilosa It is a client of bitmap database. The scene application of bitmap database is relatively limited, and it is usually used when there is label demand; For example, find the intersection and complement of N labels; After the data has a certain scale, the operation will raise relevant needs; Can be prepared for a rainy day.

pb ⭐️⭐️⭐️

https://github.com/cheggaaa/pb A command line tool progress bar, which can be used for more elegant interaction when writing command line tools.


Finally, I summarized a table for easy viewing:

name type function Star
Gin Business development HTTP framework ⭐️⭐️⭐️⭐️⭐️
GORM Business development ORM framework ⭐️⭐️⭐️⭐️⭐️
errors Business development Exception handling Library ⭐️⭐️⭐️⭐️⭐️
zorolog Business development Log Library ⭐️⭐️⭐️⭐️⭐️
excelize Business development Excel related requirements ⭐️⭐️⭐️⭐️⭐️
now Business development Time processing ⭐️⭐️⭐️⭐️️
Decimal Business development Precision processing ⭐️⭐️⭐️⭐️️
configor Business development configuration file ⭐️⭐️⭐️⭐️️
go-cache Business development Local cache ⭐️⭐️⭐️
copier Business development Data replication ⭐️⭐️⭐️️️
env Business development environment variable ⭐️⭐️⭐️️️
user_agent Business development Read user agent ⭐️⭐️⭐️️️
phonenumbers Business development Mobile number verification ⭐️⭐️⭐️️️
gomonkey Basic tools mock tool ⭐️⭐️⭐️⭐️⭐
goconvey Basic tools Single test coverage ⭐️⭐️⭐️⭐️⭐
dig Basic tools Dependency injection ⭐️⭐️⭐️⭐️⭐
cobra Basic tools Command line tools ⭐️⭐️⭐️⭐
cli Basic tools Command line tools ⭐️⭐️⭐️⭐
BloomRPC Basic tools gRPC debug client ⭐️⭐️⭐️⭐
redis Basic tools Redis client ⭐️⭐️⭐️⭐
elastic Basic tools elasticsearch client ⭐️⭐️⭐️⭐
resty Basic tools http client ⭐️⭐️⭐️⭐
pulsar-client-go Basic tools Pulsar client ⭐️⭐️⭐️
go-grpc-middleware Basic tools gRPC Middleware ⭐️⭐️⭐
go-pilosa Basic tools pilosa client ⭐️⭐️⭐️
pb Basic tools Command line tool progress bar ⭐️⭐️⭐️

The rules of star rating mainly depend on the frequency of actual use.

Finally, there are some private goods (in fact, there is no way to talk about it). The article mentioned that we have integrated a business development framework based on the above library; More than 10 large and small projects have been launched based on the framework, and there is still a lot of room for improvement. At present, it is still in rapid iteration.

For general usage, enter main.go: Finally, I intercepted my internal sharing to summarize the overall idea -- quoted from colleagues surnamed I of the company.

Perhaps after many iterations, we will try open source when we feel that we have the ability to open up and bring some help to the community; It's not ugly at this stage.

These libraries are the most commonly used in our daily development. You are also welcome to leave your commonly used libraries and tools in the comment area.

Posted by dreamline on Tue, 02 Nov 2021 05:17:29 -0700