// 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)正如 这里 和 这里所描述的, 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")整体上来看,Disruptor(lockfree)在写入和读取上的性能大概都在channel的7倍以上,数据写入的越多,性能提升越明显。 下面是buffer=1024*1024时,写入数据的耗时对比:
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 |
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")}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).// 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")}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)golang的缓存库。它支持可擦除缓存、LFU、LRU和ARC。
Import导入:go get github.com/bluele/gcacheGithub地址: https://github.com/bluele/gcache说明:Go语言的缓存库,支持LRU、LFU和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)}Ledisdb是一个用Go编写的高性能NoSQL数据库库和服务器。它类似于Redis,但将数据存储在磁盘中。它支持许多数据结构,包括kv、list、hash、zset、set。
LedisDB现在支持多个不同的数据库作为后端。
Import导入:go get github.com/ledisdb/ledisdb/ledisGithub地址: https://github.com/ledisdb/ledisdb说明:Go语言版本的redisimport ( 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)uuid包基于RFC 4122和DCE 1.1:身份验证和安全服务生成和检查uuid。
此包基于 github.com/pboman/uuid包(以前名为code.google.com/p/go-uuid)。它与这些早期包的不同之处在于uuid是一个16字节数组,而不是一个字节片。此更改导致的一个损失是表示无效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)PASSRedigo是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()} 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_serverRun the server:$ $(go env GOPATH)/bin/greeter_server &Run the client:$ $(go env GOPATH)/bin/greeter_clientGreeting: Hello worldViper需要最少的配置,因此它知道在哪里查找配置文件。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))}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()}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 Sunlocation, 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"高性能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)基于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),)// 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 ..")
正如这里和这里所描述的,Go语言原生的map类型并不支持并发读写。con...
第八章(肖战水仙,角色设定为剧情,勿上升)北堂墨染心不在焉用完...
来源:长江商报延续2021年的亮眼业绩,氮肥龙头企业湖北宜化(00042...
1、做法一,酱鸡爪材料鸡爪,糖,甜辣酱,老抽,酱油,姜片,花椒,...
3月13日电,欧洲最大数字资产投资及交易公司CoinShares称其在硅谷银...
借贷款是很多人在生活中面临用钱困境时会采取的一个方法,而提供贷...
1、1来到建业县阶(建业衙门30、19)2、知府让你去问剑大师(建业衙门2...
一、外卖骑手为何被个体户化3月12日上午,最高人民法院开展《最高人...
1、效果:滋养皮肤,修复损伤2、抗氧化,改善眼部问题3、淡化黑眼圈...
近日,2023昆明制造业高质量发展峰会暨“科创中国”智能制造科技服...
新一股冷空气已抵穗!降温幅度不大,但送雨送清凉
观音出家日的日期是多少,观音出家日介绍很多人还不知道,现在让我...
“目前已经面试了55场,拿到了4个offer ”今年的应届毕业生柳源(化...
对于笔记本电脑鼠标触摸板没反应这个问题感兴趣的朋友应该很多,这...
1、作为非常常用的压缩文件格式,我们生活中见到的cab文件非常多,...
现身终结阵容!卢:戈登拉空间的能力很重要他也能让球队镇静,快船队...
1、巨磁阻效应(GiantMagnetoresistance,缩写:GMR)是一种量子力...
养老保险计算公式2020,养老保险计算公式很多人还不知道,现在让我...
1、交通tuktuk在普吉镇拉农路上(机场对面),经过几个主要海滩,每半...
1、青云台男主是小昭王谢容与,女主是温青唯,又名小野。2、《青云...
1、将莲藕和生姜去皮,莲藕切片。2、然后把姜剁碎,把剁碎的姜捣碎...
这种主要是因为mvn本地仓库里有些包拉取的有问题,找出来然后删除即...
1、055-79000歌手:吴京2、鸳鸯蝴蝶一起飞。3、所有的爱都是醉人的...
1、多面体欧拉定理是指对于简单多面体,其顶点数V、棱数E及面数F间...
1、 导语:在复习过程中,不妨将要求掌握的知识分成几大块和若干...
韩国高兴早了,19年不败纪录作古,国青2大核心缺阵逼近世青赛,韩国,...
1、再跟小弟说一下六个环节中的“全漏”,就是不再担心生死。2、错...
1、寒梅著花未2、梅花恼破梦魂3、如雪寒梅若水十七4、山里梅子5、十...
1、孙思邈。本文到此分享完毕,希望对大家有所帮助。
1、去百度文库,查看完整内容>内容来自用户:bpsoc3449党风廉政领导...