A typed implementation of the Go sync.Map
using code generation.
go get -u github.com/2mf8/syncmap@master
- Using CLI
$ syncmap -name IntMap "map[int]int"
$ syncmap -name RequestMap -pkg mypkg "map[string]*http.Request"
Or:
$ go run github.com/2mf8/syncmap -name IntMap "map[int]int"
-
Using
go generate
.- Add a directive with map definition:
//go:generate go run github.com/2mf8/syncmap -name WriterMap map[string]io.Writer //go:generate go run github.com/2mf8/syncmap -name Requests map[string]*http.Request
- Then, run
go generate
on this package.
See testdata/gen.go for more examples.
- Add a directive with map definition:
syncmap
didn't copy the code of sync/map.go
and replace its identifiers. Instead, it reads the sync/map.go
from
your GOROOT
, parses it into an *ast.File
, and runs a few mutators that bring it to the desired state.
Check the code for more information.
How can we make sure it will continue to work? - I'm running a daily CI test on TravisCI.
生成的并不完美,需要手动修改 CompareAndSwap
函数(参数 old
需要改为 old, new
),此外可能还需要修改 Swap
函数的返回值(return nil, false
)。
I am providing code in the repository to you under MIT license. Because this is my personal repository, the license you receive to my code is from me and not my employer (Facebook)