今日快讯:Golang实现非常好用的第三方库(一)
2023-03-14 14:19:59   来源:腾讯云

1. fasthttp

Import导入:go get github.com/valyala/fasthttpGithub地址: https://github.com/valyala/fasthttp说明:高性能HTTP服务框架

示例

// net/http codem := &http.ServeMux{}m.HandleFunc("/foo", fooHandlerFunc)m.HandleFunc("/bar", barHandlerFunc)m.Handle("/baz", bazHandler)http.ListenAndServe(":80", m)
// the corresponding fasthttp codem := 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`在读多写少性能比较好,否则并发性能很差***
Import导入:go get github.com/orcaman/concurrent-mapGithub地址: https://github.com/orcaman/concurrent-map/tree/v1.0.0说明:分片带锁Map,比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

Import导入:go get github.com/bruceshao/lockfreeGithub地址: https://github.com/bruceshao/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

Iterator

Enumerable

Referenced by

Lists

ArrayList

yes

yes*

yes

index

SinglyLinkedList

yes

yes

yes

index

DoublyLinkedList

yes

yes*

yes

index

Sets

HashSet

no

no

no

index

TreeSet

yes

yes*

yes

index

LinkedHashSet

yes

yes*

yes

index

Stacks

LinkedListStack

yes

yes

no

index

ArrayStack

yes

yes*

no

index

Maps

HashMap

no

no

no

key

TreeMap

yes

yes*

yes

key

LinkedHashMap

yes

yes*

yes

key

HashBidiMap

no

no

no

key*

TreeBidiMap

yes

yes*

yes

key*

Trees

RedBlackTree

yes

yes*

no

key

AVLTree

yes

yes*

no

key

BTree

yes

yes*

no

key

BinaryHeap

yes

yes*

no

index

Queues

LinkedListQueue

yes

yes

no

index

ArrayQueue

yes

yes*

no

index

CircularBuffer

yes

yes*

no

index

PriorityQueue

yes

yes*

no

index

*reversible

*bidirectional

5. Gin Web Framework

Gin是一个用Go编写的web框架。由于httprouter,它具有类似马提尼的API,性能提高了40倍。如果你需要高性能和高生产力,你会喜欢Gin。
Import导入:go get github.com/gin-Gonic/ginGithub地址: https://github.com/gin-Gonic/gin说明:高性能的web 框架

示例

package mainimport (  "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/cronGithub地址: 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 Cronc.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

Import导入:go get github.com/deckarep/golang-setGithub地址: https://github.com/deckarep/Golang-set说明:包含线程安全和非线程安全的高性能集合

示例

// Syntax example, doesn"t compile.mySet := mapset.NewSet[T]() // where T is some concrete comparable type.// Therefore this code creates an int setmySet := mapset.NewSet[int]()// Or perhaps you want a string setmySet := mapset.NewSet[string]()type myStruct {  name string  age uint8}// Alternatively a set of structsmySet := 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 mainimport (  "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%。

Import导入:go get github.com/bits-and-blooms/bloomGithub地址: https://github.com/bits-and-blooms/bloom说明:Go版本的布隆过滤器

示例

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。

Import导入:go get github.com/bluele/gcacheGithub地址: https://github.com/bluele/gcache说明:Go语言的缓存库,支持LRU、LFU和ARC算法

特性

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

示例

package mainimport (  "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导入:go get github.com/ledisdb/ledisdb/ledisGithub地址: https://github.com/ledisdb/ledisdb说明:Go语言版本的redis

示例

import (    lediscfg "github.com/ledisdb/ledisdb/config"    "github.com/ledisdb/ledisdb/ledis")// Use Ledis"s default configcfg := 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相比)的能力。

Import导入:go get github.com/google/uuidGithub地址: https://github.com/google/uuid说明:生成uuid

示例

package uuidimport (    "fmt"    "testing"    "github.com/google/uuid")func TestUuid(t *testing.T) {    for i := 0; i <= 10; i++ {        uuid, _ := uuid.NewUUID()        fmt.Println(uuid)    }}

结果

=== RUN   TestUuid709096ca-bcb5-11ed-b598-acde4800112270909d5a-bcb5-11ed-b598-acde4800112270909d6e-bcb5-11ed-b598-acde4800112270909d78-bcb5-11ed-b598-acde4800112270909d8c-bcb5-11ed-b598-acde4800112270909d96-bcb5-11ed-b598-acde4800112270909da0-bcb5-11ed-b598-acde4800112270909db4-bcb5-11ed-b598-acde4800112270909dbe-bcb5-11ed-b598-acde4800112270909dc8-bcb5-11ed-b598-acde4800112270909dd2-bcb5-11ed-b598-acde48001122--- PASS: TestUuid (0.00s)PASS

12. Redigo

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

Import导入:go get github.com/gomodule/redigoGithub地址: https://github.com/gomodule/redigo说明:Redis数据库的客户端

示例

package mainimport (    "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

Import导入:go get -u google.golang.org/grpcGithub地址: https://github.com/grpc/grpc-Go说明:go语言实现的grpc

示例

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_clientGreeting: Hello world

14. Viper配置解析

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

Import导入:go get github.com/spf13/viperGithub地址: https://github.com/spf13/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 nameviper.AddConfigPath("/etc/appname/")   // path to look for the config file inviper.AddConfigPath("$HOME/.appname")  // call multiple times to add many search pathsviper.AddConfigPath(".")               // optionally look for config in the working directoryerr := viper.ReadInConfig() // Find and read the config fileif 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并发服务器框架).

Import导入:go get github.com/aceld/zinxGithub地址: https://github.com/aceld/zinx说明: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/nowGithub地址: https://github.com/jinzhu/now说明:Go语言的时间工具集,各种时间的获取、转换等等

示例

import "github.com/jinzhu/now"time.Now() // 2013-11-18 17:51:49.123456789 Monnow.BeginningOfMinute()        // 2013-11-18 17:51:00 Monnow.BeginningOfHour()          // 2013-11-18 17:00:00 Monnow.BeginningOfDay()           // 2013-11-18 00:00:00 Monnow.BeginningOfWeek()          // 2013-11-17 00:00:00 Sunnow.BeginningOfMonth()         // 2013-11-01 00:00:00 Frinow.BeginningOfQuarter()       // 2013-10-01 00:00:00 Tuenow.BeginningOfYear()          // 2013-01-01 00:00:00 Tuenow.EndOfMinute()              // 2013-11-18 17:51:59.999999999 Monnow.EndOfHour()                // 2013-11-18 17:59:59.999999999 Monnow.EndOfDay()                 // 2013-11-18 23:59:59.999999999 Monnow.EndOfWeek()                // 2013-11-23 23:59:59.999999999 Satnow.EndOfMonth()               // 2013-11-30 23:59:59.999999999 Satnow.EndOfQuarter()             // 2013-12-31 23:59:59.999999999 Tuenow.EndOfYear()                // 2013-12-31 23:59:59.999999999 Tuenow.WeekStartDay = time.Monday // Set Monday as first day, default is Sundaynow.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 MonmyConfig.With(t).BeginningOfWeek()         // 2013-11-18 00:00:00 MonmyConfig.Parse("2002-10-12 22:14:01")     // 2002-10-12 22:14:01myConfig.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”

Import导入:go get github.com/json-iterator/goGithub地址: https://github.com/json-iterator/go说明:100%兼容encoding/json的更高效的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.ConfigCompatibleWithStandardLibraryjson.Marshal(&data)

Replace

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

with

import jsoniter "github.com/json-iterator/go"var json = jsoniter.ConfigCompatibleWithStandardLibraryjson.Unmarshal(input, &data)

18. zap 日志

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

Import导入:go get -u go.uber.org/zapGithub地址: https://github.com/uber-go/zap说明:功能强大的日志输出库

示例

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 打印输出

Import导入:go get github.com/fatih/colorGithub地址: https://github.com/fatih/color说明:打印输出带有自定义颜色的日志

示例

// Print with default helper functionscolor.Cyan("Prints text in cyan.")// A newline will be appended automaticallycolor.Blue("Prints %s in blue.", "text")// These are using the default foreground colorscolor.Red("We have red")color.Magenta("And many others ..")

关键词:

上一篇:日播时尚:因家庭资产规划需要,实控人曲江亭将所持6.14%公司股份转让给其子王晟羽
下一篇:最后一页