concurrency

package
v1.2.13 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Await

func Await[out any](promise Promise[out]) (result out, err error)

Await await promise example:

func main() {
	promise := Async(func() string {
		return "test"
	})

	result, err := Await(promise)
	if err != nil {
		fmt.Println(err)
	}

	fmt.Println(result)
}

then output: test

func RecoverErr

func RecoverErr(e any) error

RecoverErr recover panic to error, must be used in defer example:

func main() {
	defer func() {
		if err := RecoverErr(recover()); err != nil {
			fmt.Println(err)
		}
	}()

	panic("test")
}

then output: test

Types

type ConcurrentResult added in v1.2.10

type ConcurrentResult[T any] struct {
	// contains filtered or unexported fields
}

type HashMapNodeOption added in v1.2.10

type HashMapNodeOption uint64
const (
	HashMapNodeOptionSmallSize  HashMapNodeOption = 7
	HashMapNodeOptionMediumSize HashMapNodeOption = 47
	HashMapNodeOptionLargeSize  HashMapNodeOption = 97
	HashMapNodeOptionExtraSize  HashMapNodeOption = 997
	HashMapNodeOptionHugeSize   HashMapNodeOption = 9973
)

func CustomHashMapNodeOptions added in v1.2.10

func CustomHashMapNodeOptions(want uint64) HashMapNodeOption

CustomHashMapNodeOptions returns a custom HashMapNodeOption

type Map added in v1.2.10

type Map[K comparable, V any] interface {
	// Get returns value by key
	// example:
	//
	//	m := NewMap[int, string]()
	//	m.Set(1, "test")
	// then
	// m.Get(1) -> "test", true
	Get(key K) (V, bool)

	// Set sets value by key
	// example:
	//
	//	m := NewMap[int, string]()
	//	m.Set(1, "test")
	// then
	// m.Get(1) -> "test", true
	Set(key K, value V)

	// Delete deletes value by key
	// example:
	//
	//	m := NewMap[int, string]()
	//	m.Set(1, "test")
	//	m.Delete(1)
	// then
	// m.Get(1) -> "", false
	Delete(key K)

	// Keys returns all keys in map
	// example:
	//
	//	m := NewMap[int, string]()
	//	m.Set(1, "test")
	//	m.Set(2, "test2")
	// then
	// m.Keys() -> [1, 2]
	Keys() []K

	// Values returns all values in map
	// example:
	//
	//	m := NewMap[int, string]()
	//	m.Set(1, "test")
	//	m.Set(2, "test2")
	// then
	// m.Values() -> ["test", "test2"]
	Values() []V

	// Range iterates all items in map
	// example:
	//
	//	m := NewMap[int, string]()
	//	m.Set(1, "test")
	//	m.Set(2, "test2")
	//	m.Range(func(key int, value string) {
	//		fmt.Println(key, value)
	//	})
	// then
	// 1 test
	// 2 test2
	Range(f func(key K, value V))

	// Origin returns a copy of origin map
	// example:
	//
	//	m := NewMap[int, string]()
	//	m.Set(1, "test")
	//	m.Set(2, "test2")
	//	o := m.Origin()
	// then
	//  o -> {1: "test", 2: "test2"}
	Origin() map[K]V

	// Length returns the length of map
	// example:
	//
	//	m := NewMap[int, string]()
	//	m.Set(1, "test")
	//	m.Set(2, "test2")
	// then
	// m.Length() -> 2
	Length() int

	// Clear clears all items in map
	// example:
	//
	//	m := NewMap[int, string]()
	//	m.Set(1, "test")
	//	m.Set(2, "test2")
	//	m.Clear()
	// then
	// m.Length() -> 0
	Clear()
}

Map is a thread-safe map

func NewHashMap added in v1.2.10

func NewHashMap[K comparable, V any](maxNodes HashMapNodeOption) Map[K, V]

NewHashMap returns a new thread-safe map with hash function

Attention: the performance of hashMap is much better than Map, but this structure CANNOT be used for persistence, the hash value of the key will change after restarting the program

func NewMap added in v1.2.10

func NewMap[K comparable, V any]() Map[K, V]

NewMap returns a new thread-safe map

type Promise

type Promise[T any] chan ConcurrentResult[T]

func Async

func Async[out any](fn func() out) (promise Promise[out])

Async async execute function example:

func main() {
	promise := Async(func() string {
		return "test"
	})

	result, err := Await(promise)
	if err != nil {
		fmt.Println(err)
	}

	fmt.Println(result)
}

then output: test

type Slice added in v1.2.10

type Slice[T any] interface {
	// Append appends item to the end of slice
	// example:
	//
	//	slice := NewSlice[int]()
	//	slice.Append(1)
	//	slice.Append(2)
	// then
	// slice.Items() -> [1, 2]
	Append(item T)

	// Appends appends items to the end of slice
	// example:
	//
	//	slice := NewSlice[int]()
	//	slice.Appends(1, 2)
	// then
	// slice.Items() -> [1, 2]
	Appends(items ...T)

	// SubSlice returns a new slice from start to end, means [start, end)
	// example:
	//
	//	slice := NewSlice[int]()
	//	slice.Appends(1, 2, 3, 4, 5)
	//	slice = slice.SubSlice(1, 3)
	// then
	// slice.Items() -> [2, 3]
	SubSlice(start, end int) Slice[T]

	// Items returns all items in slice
	// example:
	//
	//	slice := NewSlice[int]()
	//	slice.Appends(1, 2, 3, 4, 5)
	// then
	// slice.Items() -> [1, 2, 3, 4, 5]
	Items() []T

	// Get returns item by index
	// example:
	//
	//	slice := NewSlice[int]()
	//	slice.Appends(1, 2, 3, 4, 5)
	// then
	// slice.Get(0) -> 1
	Get(index int) T

	// Set sets item by index
	// example:
	//
	//	slice := NewSlice[int]()
	//	slice.Appends(1, 2, 3, 4, 5)
	//	slice.Set(0, 2)
	// then
	// slice.Items() -> [2, 2, 3, 4, 5]
	Set(index int, item T)

	// Length returns the length of slice
	// example:
	//
	//	slice := NewSlice[int]()
	//	slice.Appends(1, 2, 3, 4, 5)
	// then
	// slice.Length() -> 5
	Length() int

	// Capacity returns the capacity of slice
	// example:
	//
	//	slice := NewSlice[int]()
	//	slice.Appends(1, 2, 3, 4, 5)
	// then
	// slice.Capacity() -> 5
	Capacity() int
}

Slice is a thread-safe slice

func NewSlice added in v1.2.10

func NewSlice[T any](items ...T) Slice[T]

Jump to

Keyboard shortcuts

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