Golang超实用第三方库合集
Golang超实用第三方库合集
1. fasthttp
- Import导入:go get github/valyala/fasthttp
- Github地址:
- 说明:高性能HTTP服务框架
示例
代码语言:go
复制
代码语言:javascript代码运行次数:0运行复制// net/http code
m := &http.ServeMux{}
m.HandleFunc("/foo", fooHandlerFunc)
m.HandleFunc("/bar", barHandlerFunc)
m.Handle("/baz", bazHandler)
http.ListenAndServe(":80", m)
代码语言:go
复制
代码语言:javascript代码运行次数:0运行复制// 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.StatusotFound)
}
}
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 这里
代码语言:txt
复制
代码语言:javascript代码运行次数:0运行复制***译注:`sync.Map`在读多写少性能比较好,否则并发性能很差***
- Import导入:go get github/orcaman/concurrent-map
- Github地址: .0.0
- 说明:分片带锁Map,比sync.Map性能高
示例
代码语言:go
复制
代码语言:javascript代码运行次数:0运行复制 // 创建一个新的 map.
m := ()
// 设置变量m一个键为“foo”值为“bar”键值对
m.Set("foo", "bar")
// 从m中获取指定键值.
if tmp, ok := m.Get("foo"); ok {
bar := tmp.(string)
}
// 删除键为“foo”的项
m.Remove("foo")
. lockfree
- Import导入:go get github/bruceshao/lockfree
- Github地址:
- 说明:高性能无锁队列
性能对比
整体上来看,Disruptor(lockfree)在写入和读取上的性能大概都在channel的7倍以上,数据写入的越多,性能提升越明显。 下面是buffer=1024*1024时,写入数据的耗时对比:
4. GoDS (Go Data Structures)
- Import导入:go get github/emirpasic/gods/...
- Github地址:
- 说明:对象方式的链表、队列、各种树等多种数据结构
支持数据结构列表
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
代码语言:txt
复制
代码语言:javascript代码运行次数:0运行复制Gin是一个用Go编写的web框架。由于httprouter,它具有类似马提尼的API,性能提高了40倍。如果你需要高性能和高生产力,你会喜欢Gin。
- Import导入:go get github/gin-Gonic/gin
- Github地址:
- 说明:高性能的web 框架
示例
代码语言:go
复制
代码语言:javascript代码运行次数:0运行复制package main
import (
"net/http"
"github/gin-gonic/gin"
)
func main() {
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
c.JSO(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/robfig/cron
- Github地址:
- 说明:支持多种多样的灵活的定时器,有着丰富完善的定时器表达式
示例
代码语言:go
复制
代码语言:javascript代码运行次数:0运行复制c := ()
c.AddFunc("0 0 * * * *", func() { fmt.Println("Every hour on the half hour") })
c.AddFunc("@hourly", func() { fmt.Println("Every hour") })
c.AddFunc("@every 1h0m", 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
- Import导入:go get github/deckarep/golang-set
- Github地址:
- 说明:包含线程安全和非线程安全的高性能集合
示例
代码语言:go
复制
代码语言:javascript代码运行次数:0运行复制// Syntax example, doesn't compile.
mySet := [T]() // where T is some concrete comparable type.
// Therefore this code creates an int set
mySet := [int]()
// Or perhaps you want a string set
mySet := [string]()
type myStruct {
name string
age uint8
}
// Alternatively a set of structs
mySet := [myStruct]()
// Lastly a set that can hold anything using the any or empty interface keyword: interface{}. This is effectively removes type safety.
mySet := [any]()
代码语言:go
复制
代码语言:javascript代码运行次数:0运行复制package main
import (
"fmt"
mapset "github/deckarep/golang-set/v2"
)
func main() {
// Create a string-based set of required classes.
required := [string]()
required.Add("cooking")
required.Add("english")
required.Add("math")
required.Add("biology")
// Create a string-based set of science classes.
sciences := [string]()
sciences.Add("biology")
sciences.Add("chemistry")
// Create a string-based set of electives.
electives := [string]()
electives.Add("welding")
electives.Add("music")
electives.Add("automotive")
// Create a string-based set of bonus programming classes.
bonus := [string]()
bonus.Add("beginner go")
bonus.Add("python for dummies")
}
8. Bloom filters 布隆过滤器
Bloom过滤器是集合的简洁/压缩表示,其中主要要求是进行成员查询;即项目是否是集合的成员。当元素确实存在时,Bloom过滤器将始终正确地报告集合中元素的存在。Bloom过滤器可以使用比原始集合少得多的存储空间,但它允许一些“误报”:它有时可能会报告某个元素在集合中,而不是在集合中。
当你构建时,你需要知道你有多少元素(期望的容量),以及你愿意容忍的期望假阳性率是多少。常见的假阳性率为1%。假阳性率越低,需要的内存就越多。同样,容量越高,使用的内存就越多。您可以按照以下方式构造Bloom过滤器,该过滤器能够接收100万个元素,误报率为1%。
- Import导入:go get github/bits-and-blooms/bloom
- Github地址:
- 说明:Go版本的布隆过滤器
示例
代码语言:go
复制
代码语言:javascript代码运行次数:0运行复制 filter := (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 uint2 to the filter
i := uint2(100)
n1 := make([]byte, 4)
binary.BigEndian.PutUint2(n1, i)
filter.Add(n1)
9. GCache
golang的缓存库。它支持可擦除缓存、LFU、LRU和ARC。
- Import导入:go get github/bluele/gcache
- Github地址:
- 说明:Go语言的缓存库,支持LRU、LFU和ARC算法
特性
- 支持可擦除缓存、LFU、LRU和ARC
- 协程安全
- 支持驱逐、清除和添加条目的事件处理程序 (可选)
- 缓存不存在的时候自动加载缓存 (可选)
示例
代码语言:go
复制
代码语言:javascript代码运行次数:0运行复制package main
import (
"github/bluele/gcache"
"fmt"
)
func main() {
gc := (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编写的高性能oSQL数据库库和服务器。它类似于Redis,但将数据存储在磁盘中。它支持许多数据结构,包括kv、list、hash、zset、set。
LedisDB现在支持多个不同的数据库作为后端。
- Import导入:go get github/ledisdb/ledisdb/ledis
- Github地址:
- 说明:Go语言版本的redis
示例
代码语言:go
复制
代码语言:javascript代码运行次数:0运行复制import (
lediscfg "github/ledisdb/ledisdb/config"
"github/ledisdb/ledisdb/ledis"
)
// Use Ledis's default config
cfg := ()
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/pboman/uuid
包(以前名为code.google/p/go-uuid
)。它与这些早期包的不同之处在于uuid是一个16字节数组,而不是一个字节片。此更改导致的一个损失是表示无效UUID(与IL UUID相比)的能力。
- Import导入:go get github/google/uuid
- Github地址:
- 说明:生成uuid
示例
代码语言:go
复制
代码语言:javascript代码运行次数:0运行复制package uuid
import (
"fmt"
"testing"
"github/google/uuid"
)
func TestUuid(t *testing.T) {
for i := 0; i <= 10; i++ {
uuid, _ := ()
fmt.Println(uuid)
}
}
结果
代码语言:txt
复制
代码语言:javascript代码运行次数:0运行复制=== RU 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客户端。
- Import导入:go get github/gomodule/redigo
- Github地址:
- 说明:Redis数据库的客户端
示例
代码语言:go
复制
代码语言:javascript代码运行次数:0运行复制package main
import (
"os"
"github/gomodule/redigo/redis"
)
func main() {
c, err := redis.DialURL(os.Getenv("REDIS_URL"))
if err != nil {
// handle connection error
}
defer c.Close()
}
1. gRPC-Go
- Import导入:go get -u google.golang/grpc
- Github地址:
- 说明:go语言实现的grpc
示例
Follow these setup to run the quick start example:
- Get the code:
代码语言:text
复制
代码语言:javascript代码运行次数:0运行复制$ go get google.golang/grpc/examples/helloworld/greeter_client
$ go get google.golang/grpc/examples/helloworld/greeter_server
- Run the server:
代码语言:text
复制
代码语言:javascript代码运行次数:0运行复制$ $(go env GOPATH)/bin/greeter_server &
- Run the client:
代码语言:text
复制
代码语言:javascript代码运行次数:0运行复制$ $(go env GOPATH)/bin/greeter_client
Greeting: Hello world
14. Viper配置解析
Viper需要最少的配置,因此它知道在哪里查配置文件。Viper支持JSO、TOML、YAML、HCL、II、envfile和Java财产文件。Viper可以搜索多个路径,但目前单个Viper实例仅支持单个配置文件。Viper不默认任何配置搜索路径,将默认决策留给应用程序。
- Import导入:go get github/spf1/viper
- Github地址:
- 说明:配置文件解析库,功能及其强大
示例
代码语言:go
复制
代码语言:javascript代码运行次数:0运行复制viper.SetConfigame("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并发服务器框架).
- Import导入:go get github/aceld/zinx
- Github地址:
- 说明:TCP并发服务器框架
快速启动
代码语言:shell
复制
代码语言:javascript代码运行次数:0运行复制# clone from git
$ git clone .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
服务端
代码语言:go
复制
代码语言:javascript代码运行次数:0运行复制func main() {
//1 Create the server object
s := ()
//2 Configure user-defined routes and services
s.AddRouter(0, &PingRouter{})
// Start the service
s.Serve()
}
16. 时间工具
- Import导入:go get github/jinzhu/now
- Github地址:
- 说明:Go语言的时间工具集,各种时间的获取、转换等等
示例
代码语言:go
复制
代码语言:javascript代码运行次数:0运行复制import "github/jinzhu/now"
() // 201-11-18 17:51:49.12456789 Mon
now.BeginningOfMinute() // 201-11-18 17:51:00 Mon
now.BeginningOfHour() // 201-11-18 17:00:00 Mon
now.BeginningOfDay() // 201-11-18 00:00:00 Mon
now.BeginningOfWeek() // 201-11-17 00:00:00 Sun
now.BeginningOfMonth() // 201-11-01 00:00:00 Fri
now.BeginningOfQuarter() // 201-10-01 00:00:00 Tue
now.BeginningOfYear() // 201-01-01 00:00:00 Tue
now.EndOfMinute() // 201-11-18 17:51:59.999999999 Mon
now.EndOfHour() // 201-11-18 17:59:59.999999999 Mon
now.EndOfDay() // 201-11-18 2:59:59.999999999 Mon
now.EndOfWeek() // 201-11-2 2:59:59.999999999 Sat
now.EndOfMonth() // 201-11-0 2:59:59.999999999 Sat
now.EndOfQuarter() // 201-12-1 2:59:59.999999999 Tue
now.EndOfYear() // 201-12-1 2:59:59.999999999 Tue
now.WeekStartDay = time.Monday // Set Monday as first day, default is Sunday
now.EndOfWeek() // 201-11-24 2:59:59.999999999 Sun
代码语言:go
复制
代码语言:javascript代码运行次数:0运行复制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(201, 11, 18, 17, 51, 49, 12456789, ().Location()) // // 201-11-18 17:51:49.12456789 Mon
myConfig.With(t).BeginningOfWeek() // 201-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”
- Import导入:go get github/json-iterator/go
- Github地址:
- 说明:100%兼容encoding/json的更高效的json解析库
ns/op | allocation bytes | allocation times | |
---|---|---|---|
std decode | 5510 ns/op | 1960 B/op | 99 allocs/op |
easyjson decode | 8499 ns/op | 160 B/op | 4 allocs/op |
jsoniter decode | 562 ns/op | 160 B/op | allocs/op |
std encode | 221 ns/op | 712 B/op | 5 allocs/op |
easyjson encode | 88 ns/op | 576 B/op | allocs/op |
jsoniter encode | 87 ns/op | 84 B/op | 4 allocs/op |
示例
100% 与标准lib的兼容性
Replace
代码语言:go
复制
代码语言:javascript代码运行次数:0运行复制import "encoding/json"
json.Marshal(&data)
with
代码语言:go
复制
代码语言:javascript代码运行次数:0运行复制import jsoniter "github/json-iterator/go"
var json = jsoniter.ConfigCompatibleWithStandardLibrary
json.Marshal(&data)
Replace
代码语言:go
复制
代码语言:javascript代码运行次数:0运行复制import "encoding/json"
json.Unmarshal(input, &data)
with
代码语言:go
复制
代码语言:javascript代码运行次数:0运行复制import jsoniter "github/json-iterator/go"
var json = jsoniter.ConfigCompatibleWithStandardLibrary
json.Unmarshal(input, &data)
18. zap 日志
基于Golang实现的 高性能,结构化,分等级的日志库
- Import导入:go get -u go.uber/zap
- Github地址:
- 说明:功能强大的日志输出库
示例
代码语言:go
复制
代码语言:javascript代码运行次数:0运行复制logger, _ := ()
defer logger.Sync()
logger.Info("failed to fetch URL",
// Structured context as strongly typed Field values.
zap.String("url", url),
zap.Int("attempt", ),
zap.Duration("backoff", time.Second),
)
19. color 打印输出
- Import导入:go get github/fatih/color
- Github地址:
- 说明:打印输出带有自定义颜的日志
示例
代码语言:go
复制
代码语言:javascript代码运行次数:0运行复制// Print with default helper functi
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 ..")
#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格
推荐阅读
留言与评论(共有 20 条评论) |
本站网友 上海月星环球港 | 20分钟前 发表 |
59.999999999 Sat now.EndOfMonth() // 201-11-0 2 | |
本站网友 不开心与没烦恼 | 11分钟前 发表 |
= uint2(100) n1 | |
本站网友 博爱租房 | 8分钟前 发表 |
比sync.Map性能高示例代码语言:go复制代码语言:javascript代码运行次数:0运行复制 // 创建一个新的 map. m | |
本站网友 陆东福 | 2分钟前 发表 |
而不是在集合中 | |
本站网友 武汉韩语培训班 | 17分钟前 发表 |
= [string]() type myStruct { name string age uint8 } // Alternatively a set of structs mySet | |
本站网友 从明天起做一个幸福的人 | 15分钟前 发表 |
} t | |
本站网友 孟非面馆 | 29分钟前 发表 |
常见的假阳性率为1% | |
本站网友 月经期间怎么减肥 | 18分钟前 发表 |
分等级的日志库Import导入:go get -u go.uber/zapGithub地址: 说明:功能强大的日志输出库示例代码语言:go复制代码语言:javascript代码运行次数:0运行复制logger | |
本站网友 宝安体育馆游泳池 | 19分钟前 发表 |
如果你需要高性能和高生产力 | |
本站网友 素什么白马 | 8分钟前 发表 |
fasthttp.StatusotFound) } } fasthttp.ListenAndServe(" | |
本站网友 网络购物的发展 | 20分钟前 发表 |
}) }) r.Run() // listen and serve on 0.0.0.0 | |
本站网友 瘦胳膊的方法 | 19分钟前 发表 |
59.999999999 Mon now.EndOfDay() // 201-11-18 2 | |
本站网友 顾城一代人 | 13分钟前 发表 |
49.12456789 Mon now.BeginningOfMinute() // 201-11-18 17 | |
本站网友 美一 | 16分钟前 发表 |
_ | |
本站网友 茅台官网 | 21分钟前 发表 |
00 Mon myConfig.Parse("2002-10-12 22 | |
本站网友 办公室管理 | 6分钟前 发表 |
59 | |
本站网友 冯晓静 | 13分钟前 发表 |
to test if "Love" is in bloom | |
本站网友 热键被占用 | 16分钟前 发表 |
II | |
本站网友 smec | 21分钟前 发表 |
01") // 2002-10-12 22 |