Skip to content

janitorjeff/gosafe

Repository files navigation

gosafe

Go Reference

Store values in a thread-safe way. Supports arbitrary values, slices and maps. Uses generics which means that go 1.18+ is required.

Example

package main

import (
	"github.com/janitorjeff/gosafe"
)

func main() {
	m := gosafe.Map[int, string]{} // equivalent to map[int]string
	m.Set(123, "abc")              // m[123] = "abc"
	n, ok := m.Get(123)            // n, ok := m[123]
	// ...
}

Alongside the thread-safe methods unsafe ones are provided which allow for easy extension.

package main

import (
	"github.com/janitorjeff/gosafe"
)

type CustomSlice struct {
	gosafe.Slice[string]
}

func NewSlice() CustomSlice {
	return CustomSlice{gosafe.Slice[string]{}}
}

func (s *CustomSlice) ChangeElemsWithLengthGreaterThanThree() {
	// make it thread safe
	s.Lock()
	defer s.Unlock()

	// use unsafe methods to avoid mutex deadlock
	for i := 0; i < s.LenUnsafe(); i++ {
		if len(s.GetUnsafe(i)) > 3 {
			s.SetUnsafe(i, "changed")
		}
	}
}

func main() {
	s := NewSlice()
	s.Append("abc", "def", "long-string", "xyz")
	s.ChangeElemsWithLengthGreaterThanThree()
	print(s.Get(0), "\n") // abc
	print(s.Get(2))       // changed
}

About

Store values in a thread-safe way.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages