• 20 Go Packages You Can Use in Your Next Project
  • Fernando Souza
  • The Nuggets translation Project
  • Permanent link to this article: github.com/xitu/gold-m…
  • Translator: tmpbook
  • Proofread: PassionPenguin, Kamly

20 Go tripartite libraries that you can use in your next project

Go is a great language. It was born in 2007, and you can use it to build almost any kind of application. It is as easy to write as an interpreted language and as fast and efficient as a compiled language. Concurrency and multi-core computing are also considered. You can even write programs that drive embedded devices.

Great projects like Kubernetes, Consul and NSQ are written in Go.

Using Go’s standard library alone, you can build decent applications.

Fortunately, Go has a vibrant community that can create and share many tripartite libraries that you can use to speed up and optimize development. We’ll cover some of them in this article.

Note: Don’t forget to check out this GitHub repository: Awesome Go, which contains plenty of Go projects, third-party libraries, and other resources.

Golang-Set

A library that attempts to emulate Python’s main feature in Go: the Set data structure. Example:

package main
 
import (
    "fmt"
    "github.com/deckarep/golang-set"
)
 
 
func main(a) {
    requiredClasses := mapset.NewSet()
    requiredClasses.Add("Cooking")
    requiredClasses.Add("English")
    requiredClasses.Add("Math")
    requiredClasses.Add("Biology")

    electiveClasses := mapset.NewSet()
    electiveClasses.Add("Welding")
    electiveClasses.Add("Music")
 
    allClasses := requiredClasses.Union(electiveClasses)
    fmt.Println(allClasses) // Cooking, English, Math, Biology, Welding, Music
    
    fmt.Println(electiveClasses.Contains("Cooking")) //false
    fmt.Println(electiveClasses.Cardinality()) / / 3
}
Copy the code

You can learn more here.

Go kit

It is a toolkit for implementing microservices. Helps you implement the basic functionality of distributed systems so you can focus on business logic.

If you want to use microservices architecture in your solution, “Go Kit will help you structure your services and help you avoid common pitfalls as your project grows”.

You can learn more here.

GRequests

It is a “clone” of the famous Python Requests tripartite library. You can easily make HTTP requests, upload, download files, or serialize the returns to JSON or XML.

If you’ve worked with Python-related libraries, you’ll be very comfortable with it.

package main

import (
  "log"
  "fmt"
  "github.com/levigross/grequests"
)

func main(a) {
  resp, err := grequests.Get("http://httpbin.org/get".nil)
  iferr ! =nil {
    log.Fatalln("Unable to make request:", err)
  }
  
  fmt.Println(resp.String())
}
Copy the code

Ws

The third-party library WS implements both the client and server side for the Websocket protocol. It has some nice features like zero-copy upgrades and low-level apis (which are useful if you want to write your own logic).

You can learn more here.

Email

Powerful and flexible email tripartite library. It provides a more user-friendly interface to send email using Go.

You can add attachments, send text, HTML messages, or add custom titles in a very clear way.

package main

import (
  "github.com/jordan-wright/email"
)

func main(a) {
  e := email.NewEmail()
  e.From = "Jordan Wright <[email protected]>"
  e.To = []string{"[email protected]"}
  e.Cc = []string{"[email protected]"}
  e.Subject = "Awesome Subject"
  e.Text = []byte("Text Body is, of course, supported!")
  e.HTML = []byte("

Fancy HTML is supported, too!

"
) e.Send("smtp.gmail.com:587", smtp.PlainAuth(""."[email protected]"."password123"."smtp.gmail.com"))}Copy the code

You can learn more here.

Gin

Github has more than 44K start, one of the most popular Go tripartite libraries. It is a Web framework that focuses on productivity and performance.

It has many features, such as custom middleware, providing static file services, handling multiple data formats, HTML rendering, etc.

If you want to develop an API or Web application, you should definitely consider using Gin.

Fuzzy

Go tripartite library that provides fuzzy string matching in the same style as editors such as Sublime, VsCode, etc.

It relies solely on the Go standard library and is fast enough to be a good choice if you’re adding search capabilities to your application.

The Github address is here.

Authboss

Authentication is a necessary part of modern Web applications. Writing a full-featured component can be cumbersome, and you’re likely to miss something.

This tripartite library is designed to help you implement your authentication system, saving you time and avoiding some mistakes you might make.

You can learn more here.

Uuid

This tripartite library provides a pure Go implementation of UUID, with support for both creation and parsing.

It supports versions 1 through 5 and is very easy to use.

package main

import (
 "fmt"
 "github.com/gofrs/uuid"
)

func main(a) {
 // Creating UUID Version 4
 // panic on error
 u1 := uuid.Must(uuid.NewV4())
 fmt.Printf("UUIDv4: %s\n", u1)

 // Parsing UUID from string input
 u2, err := uuid.FromString("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
 iferr ! =nil {
  fmt.Printf("Something went wrong: %s", err)
  return
 }
 fmt.Printf("Successfully parsed: %s", u2)
}
Copy the code

You can learn more here.

Gorm

If you want to implement some API functionality, you will most likely need to connect to a database. Although you can do this manually, using ORM can save a lot of time.

Gorm is an excellent Go ORM tripartite library. You can use its model creation, associations, hooks, transactions, and other cool features.

It is essential if you are going to work with databases.

GraphQL

If you want to support GraphQL, you can use it, which provides queries, updates, and subscriptions.

You can learn more here.

Ginkgo

One of the biggest complaints in the community is that the testing library is too weak.

Ginkgo extends the Testing library to allow for a more robust BDD (Behavior Driven Development) type of testing.

package books_test

import(_"github.com/onsi/ginkgo/ginkgo"
)

Describe("the strings package".func(a) {
  Context("strings.Contains()".func(a) {
    When("the string contains the substring in the middle".func(a) {
      It("returns `true`".func(a) {
        Expect(strings.Contains("Ginkgo is awesome"."is")).To(BeTrue())
      })
    })
  })
})
Copy the code

You can learn more here.

Errors

Excellent error handling tripartite library. The main function is to handle errors the same as the official way, except that comments are added without losing the context (file and line number) of the original error.

If the document belongs, error handling using this tripartite library looks like this:

iferr := SomeFunc(); err ! =nil {
  return errors.Annotate(err, "more context")}Copy the code

This can save a lot of time, especially when we’re trying to troubleshoot annoying bugs.

Cobra

It not only helps you build CLI programs, but also helps you create well-structured applications.

It has command nesting, flag, intelligent suggestions, help generation and other functions.

Cobra is one of the tripartite libraries you must use if you need to create a CLI program.

Logrus

Another process’s Go tripartite library, Logrus, is a structured logger that provides a comprehensive extension to the local logging standard library.

You can also add hooks that can be executed when a particular level of error occurs.

You can learn more here.

Dateparse

With this library, you can parse date strings without knowing the format. It can read the bytes and use the state machine to find the correct format.

t, err := dateparse.ParseAny("3/1/2014")
Copy the code

You can learn more here.

Gonum

A set of Go’s numerical processing tripartite libraries. It contains matrix, statistics, calculus and other tripartite libraries.

If you need to include mathematical operations in your code, by all means use it. It will save you time and provide you with scientifically consistent code.

You can learn more here.

Gopsutil

Another library inspired by Python packages. You can use it to retrieve information about running processes and system utilization from different platforms.

It can be useful if you need to monitor system resources and processes.

package main

import (
    "fmt"
    "github.com/shirou/gopsutil/v3/mem"
)

func main(a) {
    v, _ := mem.VirtualMemory()

    // almost every return value is a struct
    fmt.Printf("Total: %v, Free:%v, UsedPercent:%f%%\n", v.Total, v.Free, v.UsedPercent)

    // convert to JSON. String() is also implemented
    fmt.Println(v)
}
Copy the code

You can learn more here.

Fyne

You can use the Fyne Tripartite library to create beautiful DESKTOP and mobile GUI applications.

It is based on Material Design, with high availability, support for components, layout and other friendly features, it is designed to be very easy to develop.

You can learn more here.

Ants

One nice feature of the Go language is concurrency support and Goroutines. But managing all the coroutines in an application can be very challenging.

Ants implements an automatic batch management and recycling pool for Goroutines. It has a non-blocking mechanism that can handle panic without crashing the application.

If you need to create an application that involves concurrency, you should definitely try this library.

The last

Go is a great language with a great standard library. But even with the support of the standard library, sometimes we still need a little extra help.

The community already provides excellent tripartite libraries that can save you time so you don’t have to reinvent the wheel or implement easy-to-use interfaces.

If you know of other great tripartite libraries not listed here, please leave a comment.

If you find any mistakes in your translation or other areas that need to be improved, you are welcome to the Nuggets Translation Program to revise and PR your translation, and you can also get the corresponding reward points. The permanent link to this article at the beginning of this article is the MarkDown link to this article on GitHub.


The Nuggets Translation Project is a community that translates quality Internet technical articles from English sharing articles on nuggets. The content covers Android, iOS, front-end, back-end, blockchain, products, design, artificial intelligence and other fields. If you want to see more high-quality translation, please continue to pay attention to the Translation plan of Digging Gold, the official Weibo, Zhihu column.