An implementation of Clojure's atoms for Go, using generics.
Arguably much better than the sync/atomic
package, but with the following limitations:
- Work very much in progress. Tests are not yet written. Use at your own risk!
- This package's
Atom
s currently use locks, which somewhat defeats the purpose. I'll fix this in the future.
Note that you must use go 1.18 or later to use this package. Run script/bootstrap
to install it alongside your current Go version.
package main
import (
"fmt"
"time"
"github.com/juxtin/atoms"
)
func inc(i int)int {
return i + 1
}
func main() {
a := atoms.New[int](0)
a.AddWatch("printer", func(_ string, _ *atoms.Atom[int], old, new int) {
fmt.Println("Updated", old, "to", new)
})
a.SetValidator(func(i int)error {
if i > 68 {
return fmt.Errorf("%d must never exceed 68!", i)
}
return nil
})
for i := 0; i < 100; i++ {
go a.Swap(inc)
}
// wait for the goroutines to finish
time.Sleep(10 * time.Millisecond)
fmt.Println("Value of a is:", a.Deref()) // prints "Value of a is: 68"
}