Coverage Report
AtomicSlicer
AtomicSlicer is a GoLang generation tool that generates code for accessing
slices of structures and interfaces in a thread safe manner. When using go-routines
it is common to collect results in a slice, but slices in GoLang are not thread-safe.
For example to append a string the slice itself is modified:
var stringSlice []string
stringSlice := append(stringSlice, "myString")
QuickStart
Suppose there is a structure:
type MyStruct struct {
Field1 int
Field2 string
// and more fields
}
If we want to access a slice of MyStruct
concurrently, we can generate the code with:
//go:generate atomicslicer MyStruct
A file will be generated starting with generated_
and place in the same directory wherever
MyStruct
is defined.
We can then create and access a slice of MyStruct via methods:
package example
import(
"sync"
)
type MyStruct struct {
Field1 int
Field2 string
// and more fields
}
func main() {
myStructSlice := NewMyStructAtomicSlice()
waitGroup := new(sync.WaitGroup)
for i := 0; i < 100; i++ {
go func(i int) {
myStructSlice.Append(&MyStruct{Field1: i})
}(i)
}
}
In this contrived example, we iterate a hundred times creating a Go-routine in each iteration. In the
Go-routine, we append to the slice.