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
類型并不支持并發(fā)讀寫。concurrent-map
提供了一種高性能的解決方案:通過對內(nèi)部map
進行分片,降低鎖粒度,從而達到最少的鎖等待時間(鎖沖突)
(相關資料圖)
在Go 1.9之前,go語言標準庫中并沒有實現(xiàn)并發(fā)map
。在Go 1.9中,引入了sync.Map
。新的sync.Map
與此concurrent-map
有幾個關鍵區(qū)別。標準庫中的sync.Map
是專為append-only
場景設計的。因此,如果您想將Map
用于一個類似內(nèi)存數(shù)據(jù)庫,那么使用我們的版本可能會受益。你可以在golang repo上讀到更多,這里 and 這里
***譯注:`sync.Map`在讀多寫少性能比較好,否則并發(fā)性能很差***
Import導入:go get github.com/orcaman/concurrent-mapGithub地址: https://github.com/orcaman/concurrent-map/tree/v1.0.0說明:分片帶鎖Map,比sync.Map性能高示例
// 創(chuàng)建一個新的 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倍以上,數(shù)據(jù)寫入的越多,性能提升越明顯。 下面是buffer=1024*1024時,寫入數(shù)據(jù)的耗時對比:
4. GoDS (Go Data Structures)
Import導入:go get github.com/emirpasic/gods/...Github地址: https://github.com/emirpasic/gods說明:對象方式的鏈表、隊列、各種樹等多種數(shù)據(jù)結構支持數(shù)據(jù)結構列表
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倍。如果你需要高性能和高生產(chǎn)力,你會喜歡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%。假陽性率越低,需要的內(nèi)存就越多。同樣,容量越高,使用的內(nèi)存就越多。您可以按照以下方式構造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協(xié)程安全支持驅逐、清除和添加條目的事件處理程序 (可選)緩存不存在的時候自動加載緩存 (可選)示例
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數(shù)據(jù)庫庫和服務器。它類似于Redis,但將數(shù)據(jù)存儲在磁盤中。它支持許多數(shù)據(jù)結構,包括kv、list、hash、zset、set。
LedisDB現(xiàn)在支持多個不同的數(shù)據(jù)庫作為后端。
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字節(jié)數(shù)組,而不是一個字節(jié)片。此更改導致的一個損失是表示無效UUID(與NIL 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數(shù)據(jù)庫的Go客戶端。
Import導入:go get github.com/gomodule/redigoGithub地址: https://github.com/gomodule/redigo說明:Redis數(shù)據(jù)庫的客戶端示例
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語言實現(xiàn)的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財產(chǎn)文件。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并發(fā)服務器框架
Based on Golang Lightweight TCP Concurrent server framework(基于Golang輕量級TCP并發(fā)服務器框架).
Import導入:go get github.com/aceld/zinxGithub地址: https://github.com/aceld/zinx說明:TCP并發(fā)服務器框架快速啟動
# 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實現(xiàn)的 高性能,結構化,分等級的日志庫
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 ..")
標簽: