atomicslicer

command module
v0.0.0-...-d5673d6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 24, 2019 License: GPL-3.0 Imports: 4 Imported by: 0

README

pipeline status coverage report

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.

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL