Golang实现非常好用的第三方库(一)

1. fasthttp

示例

// net/http code

m := &http.ServeMux{}
m.HandleFunc("/foo", fooHandlerFunc)
m.HandleFunc("/bar", barHandlerFunc)
m.Handle("/baz", bazHandler)

http.ListenAndServe(":80", m)

// the corresponding fasthttp code
m := func(ctx *fasthttp.RequestCtx) {
    switch string(ctx.Path()) {
    case "/foo":
        fooHandlerFunc(ctx)
    case "/bar":
        barHandlerFunc(ctx)
    case "/baz":
        bazHandler.HandlerFunc(ctx)
    default:
        ctx.Error("not found", fasthttp.StatusNotFound)
    }
}

fasthttp.ListenAndServe(":80", m)

2. concurrent map

正如 这里这里所描述的, Go语言原生的map类型并不支持并发读写。concurrent-map提供了一种高性能的解决方案:通过对内部map进行分片,降低锁粒度,从而达到最少的锁等待时间(锁冲突)

在Go 1.9之前,go语言标准库中并没有实现并发map。在Go 1.9中,引入了sync.Map。新的sync.Map与此concurrent-map有几个关键区别。标准库中的sync.Map是专为append-only场景设计的。因此,如果您想将Map用于一个类似内存数据库,那么使用我们的版本可能会受益。你可以在golang repo上读到更多,这里 and 这里

***译注:`sync.Map`在读多写少性能比较好,否则并发性能很差***

示例

    // 创建一个新的 map.
    m := cmap.New()

    // 设置变量m一个键为“foo”值为“bar”键值对
    m.Set("foo", "bar")

    // 从m中获取指定键值.
    if tmp, ok := m.Get("foo"); ok {
        bar := tmp.(string)
    }

    // 删除键为“foo”的项
    m.Remove("foo")

3. lockfree

性能对比

整体上来看,Disruptor(lockfree)在写入和读取上的性能大概都在channel的7倍以上,数据写入的越多,性能提升越明显。 下面是buffer=1024*1024时,写入数据的耗时对比:

4. GoDS (Go Data Structures)

  • Import导入:go get github.com/emirpasic/gods/…
  • Github地址: https://github.com/emirpasic/gods
  • 说明:对象方式的链表、队列、各种树等多种数据结构

支持数据结构列表

Data

Structure

Ordered

Referenced by

yes

yes*

yes

index

yes

yes

yes

index

yes

yes*

yes

index

no

no

no

index

yes

yes*

yes

index

yes

yes*

yes

index

yes

yes

no

index

yes

yes*

no

index

no

no

no

key

yes

yes*

yes

key

yes

yes*

yes

key

no

no

no

key*

yes

yes*

yes

key*

yes

yes*

no

key

yes

yes*

no

key

yes

yes*

no

key

yes

yes*

no

index

yes

yes

no

index

yes

yes*

no

index

yes

yes*

no

index

yes

yes*

no

index

*reversible

*bidirectional

5. Gin Web Framework

Gin是一个用Go编写的web框架。由于httprouter,它具有类似马提尼的API,性能提高了40倍。如果你需要高性能和高生产力,你会喜欢Gin。

示例

package main

import (
  "net/http"

  "github.com/gin-gonic/gin"
)

func main() {
  r := gin.Default()
  r.GET("/ping", func(c *gin.Context) {
    c.JSON(http.StatusOK, gin.H{
      "message": "pong",
    })
  })
  r.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}

6. cron 定时器

  • Import导入:go get github.com/robfig/cron
  • Github地址: https://github.com/robfig/cron
  • 说明:支持多种多样的灵活的定时器,有着丰富完善的定时器表达式

示例

c := cron.New()
c.AddFunc("0 30 * * * *", func() { fmt.Println("Every hour on the half hour") })
c.AddFunc("@hourly",      func() { fmt.Println("Every hour") })
c.AddFunc("@every 1h30m", func() { fmt.Println("Every hour thirty") })
c.Start()
..
// Funcs are invoked in their own goroutine, asynchronously.
...
// Funcs may also be added to a running Cron
c.AddFunc("@daily", func() { fmt.Println("Every day") })
..
// Inspect the cron job entries' next and previous run times.
inspect(c.Entries())
..
c.Stop()  // Stop the scheduler (does not stop any jobs already running).

7. golang-set

示例

// Syntax example, doesn't compile.
mySet := mapset.NewSet[T]() // where T is some concrete comparable type.

// Therefore this code creates an int set
mySet := mapset.NewSet[int]()

// Or perhaps you want a string set
mySet := mapset.NewSet[string]()

type myStruct {
  name string
  age uint8
}

// Alternatively a set of structs
mySet := mapset.NewSet[myStruct]()

// Lastly a set that can hold anything using the any or empty interface keyword: interface{}. This is effectively removes type safety.
mySet := mapset.NewSet[any]()

package main

import (
  "fmt"
  mapset "github.com/deckarep/golang-set/v2"
)

func main() {
  // Create a string-based set of required classes.
  required := mapset.NewSet[string]()
  required.Add("cooking")
  required.Add("english")
  required.Add("math")
  required.Add("biology")

  // Create a string-based set of science classes.
  sciences := mapset.NewSet[string]()
  sciences.Add("biology")
  sciences.Add("chemistry")
  
  // Create a string-based set of electives.
  electives := mapset.NewSet[string]()
  electives.Add("welding")
  electives.Add("music")
  electives.Add("automotive")

  // Create a string-based set of bonus programming classes.
  bonus := mapset.NewSet[string]()
  bonus.Add("beginner go")
  bonus.Add("python for dummies")
}

8. Bloom filters 布隆过滤器

Bloom过滤器是集合的简洁/压缩表示,其中主要要求是进行成员查询;即项目是否是集合的成员。当元素确实存在时,Bloom过滤器将始终正确地报告集合中元素的存在。Bloom过滤器可以使用比原始集合少得多的存储空间,但它允许一些“误报”:它有时可能会报告某个元素在集合中,而不是在集合中。

当你构建时,你需要知道你有多少元素(期望的容量),以及你愿意容忍的期望假阳性率是多少。常见的假阳性率为1%。假阳性率越低,需要的内存就越多。同样,容量越高,使用的内存就越多。您可以按照以下方式构造Bloom过滤器,该过滤器能够接收100万个元素,误报率为1%。

示例

    filter := bloom.NewWithEstimates(1000000, 0.01) 
    
    // to add a string item, "Love"
    filter.Add([]byte("Love"))
    
    // Similarly, to test if "Love" is in bloom:
    if filter.Test([]byte("Love"))
    
    // to add a uint32 to the filter
    i := uint32(100)
    n1 := make([]byte, 4)
    binary.BigEndian.PutUint32(n1, i)
    filter.Add(n1)

9. GCache

golang的缓存库。它支持可擦除缓存、LFU、LRU和ARC。

特性

  • 支持可擦除缓存、LFU、LRU和ARC
  • 协程安全
  • 支持驱逐、清除和添加条目的事件处理程序 (可选)
  • 缓存不存在的时候自动加载缓存 (可选)

示例

package main

import (
  "github.com/bluele/gcache"
  "fmt"
)

func main() {
  gc := gcache.New(20).
    LRU().
    Build()
  gc.Set("key", "ok")
  value, err := gc.Get("key")
  if err != nil {
    panic(err)
  }
  fmt.Println("Get:", value)
}

10. ledis(go版本的redis)

Ledisdb是一个用Go编写的高性能NoSQL数据库库和服务器。它类似于Redis,但将数据存储在磁盘中。它支持许多数据结构,包括kv、list、hash、zset、set。

LedisDB现在支持多个不同的数据库作为后端。

示例

import (
    lediscfg "github.com/ledisdb/ledisdb/config"
    "github.com/ledisdb/ledisdb/ledis"
)

// Use Ledis's default config
cfg := lediscfg.NewConfigDefault()
l, _ := ledis.Open(cfg)
db, _ := l.Select(0)

db.Set(key, value)

db.Get(key)

11. uuid生成器

uuid包基于RFC 4122和DCE 1.1:身份验证和安全服务生成和检查uuid。

此包基于 github.com/pboman/uuid包(以前名为code.google.com/p/go-uuid)。它与这些早期包的不同之处在于uuid是一个16字节数组,而不是一个字节片。此更改导致的一个损失是表示无效UUID(与NIL UUID相比)的能力。

示例

package uuid

import (
    "fmt"
    "testing"

    "github.com/google/uuid"
)

func TestUuid(t *testing.T) {
    for i := 0; i <= 10; i++ {
        uuid, _ := uuid.NewUUID()
        fmt.Println(uuid)
    }
}

结果

=== RUN   TestUuid
709096ca-bcb5-11ed-b598-acde48001122
70909d5a-bcb5-11ed-b598-acde48001122
70909d6e-bcb5-11ed-b598-acde48001122
70909d78-bcb5-11ed-b598-acde48001122
70909d8c-bcb5-11ed-b598-acde48001122
70909d96-bcb5-11ed-b598-acde48001122
70909da0-bcb5-11ed-b598-acde48001122
70909db4-bcb5-11ed-b598-acde48001122
70909dbe-bcb5-11ed-b598-acde48001122
70909dc8-bcb5-11ed-b598-acde48001122
70909dd2-bcb5-11ed-b598-acde48001122
--- PASS: TestUuid (0.00s)
PASS

12. Redigo

Redigo是Redis数据库的Go客户端。

示例

package main

import (
    "os"

    "github.com/gomodule/redigo/redis"
)

func main() {
    c, err := redis.DialURL(os.Getenv("REDIS_URL"))
    if err != nil {
        // handle connection error
    }
    defer c.Close()
} 

13. gRPC-Go

示例

Follow these setup to run the quick start example:

  • Get the code:
$ go get google.golang.org/grpc/examples/helloworld/greeter_client
$ go get google.golang.org/grpc/examples/helloworld/greeter_server

  • Run the server:
$ $(go env GOPATH)/bin/greeter_server &

  • Run the client:
$ $(go env GOPATH)/bin/greeter_client
Greeting: Hello world

14. Viper配置解析

Viper需要最少的配置,因此它知道在哪里查找配置文件。Viper支持JSON、TOML、YAML、HCL、INI、envfile和Java财产文件。Viper可以搜索多个路径,但目前单个Viper实例仅支持单个配置文件。Viper不默认任何配置搜索路径,将默认决策留给应用程序。

示例

viper.SetConfigName("config") // name of config file (without extension)
viper.SetConfigType("yaml") // REQUIRED if the config file does not have the extension in the name
viper.AddConfigPath("/etc/appname/")   // path to look for the config file in
viper.AddConfigPath("$HOME/.appname")  // call multiple times to add many search paths
viper.AddConfigPath(".")               // optionally look for config in the working directory
err := viper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
    panic(fmt.Errorf("fatal error config file: %w", err))
}

15. TPC并发服务器框架

image

Based on Golang Lightweight TCP Concurrent server framework(基于Golang轻量级TCP并发服务器框架).

快速启动

# clone from git
$ git clone https://github.com/aceld/zinx.git

# cd the dir of Demo
$ cd ./zinx/examples/zinx_server

# Build
$ make build

# Build for docker image
$ make image

# start and run
$ make run 

# cd the dir of Demo Client
$ cd ../zinx_client

# run 
$ go run main.go 

服务端

func main() {
    //1 Create the server object
    s := znet.NewServer()

    //2 Configure user-defined routes and services
    s.AddRouter(0, &PingRouter{})

    //3 Start the service
    s.Serve()
}

16. 时间工具

  • Import导入:go get github.com/jinzhu/now
  • Github地址: https://github.com/jinzhu/now
  • 说明:Go语言的时间工具集,各种时间的获取、转换等等

示例

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

location, err := time.LoadLocation("Asia/Shanghai")

myConfig := &now.Config{
	WeekStartDay: time.Monday,
	TimeLocation: location,
	TimeFormats: []string{"2006-01-02 15:04:05"},
}

t := time.Date(2013, 11, 18, 17, 51, 49, 123456789, time.Now().Location()) // // 2013-11-18 17:51:49.123456789 Mon
myConfig.With(t).BeginningOfWeek()         // 2013-11-18 00:00:00 Mon

myConfig.Parse("2002-10-12 22:14:01")     // 2002-10-12 22:14:01
myConfig.Parse("2002-10-12 22:14")        // returns error 'can't parse string as time: 2002-10-12 22:14'

17. json-iterator

高性能100%兼容的替换“encoding/json”

ns/op

allocation bytes

allocation times

std decode

35510 ns/op

1960 B/op

99 allocs/op

easyjson decode

8499 ns/op

160 B/op

4 allocs/op

jsoniter decode

5623 ns/op

160 B/op

3 allocs/op

std encode

2213 ns/op

712 B/op

5 allocs/op

easyjson encode

883 ns/op

576 B/op

3 allocs/op

jsoniter encode

837 ns/op

384 B/op

4 allocs/op

示例

100% 与标准lib的兼容性

Replace

import "encoding/json"
json.Marshal(&data)

with

import jsoniter "github.com/json-iterator/go"

var json = jsoniter.ConfigCompatibleWithStandardLibrary
json.Marshal(&data)

Replace

import "encoding/json"
json.Unmarshal(input, &data)

with

import jsoniter "github.com/json-iterator/go"

var json = jsoniter.ConfigCompatibleWithStandardLibrary
json.Unmarshal(input, &data)

18. zap 日志

基于Golang实现的 高性能,结构化,分等级的日志库

示例

logger, _ := zap.NewProduction()
defer logger.Sync()
logger.Info("failed to fetch URL",
  // Structured context as strongly typed Field values.
  zap.String("url", url),
  zap.Int("attempt", 3),
  zap.Duration("backoff", time.Second),
)

19. color 打印输出

示例

// Print with default helper functions
color.Cyan("Prints text in cyan.")

// A newline will be appended automatically
color.Blue("Prints %s in blue.", "text")

// These are using the default foreground colors
color.Red("We have red")
color.Magenta("And many others ..")

正文完