Skip to content

2mf8/syncmap

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

syncmap

https://godoc.org/github.com/2mf8/syncmap LICENSE Build Status Go Report Card

A typed implementation of the Go sync.Map using code generation.

Install

go get -u github.com/2mf8/syncmap@master

Examples:

  1. 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"
  1. 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.

How does it work?

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)。

LICENSE

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)

About

A typed implementation of the Go sync.Map using code generation

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 100.0%