tsslice

package
v1.75.6 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2023 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package tsslice provides a collection of thread-safe slice functions that can be safely used between multiple goroutines.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Append

func Append[T any](mux threadsafe.Locker, slice *[]T, item ...T)

Append is a thread-safe version of the Go built-in append function.

Example (Concurrent)
package main

import (
	"fmt"
	"sort"
	"sync"

	"github.com/Vonage/gosrvlib/pkg/threadsafe/tsslice"
)

func main() {
	wg := &sync.WaitGroup{}
	mux := &sync.RWMutex{}

	max := 5
	slice := make([]int, 0, max)

	for i := 0; i < max; i++ {
		wg.Add(1)

		go func(item int) {
			defer wg.Done()

			tsslice.Append(mux, &slice, item)
		}(i)
	}

	wg.Wait()

	sort.Ints(slice)
	fmt.Println(slice)

}
Output:

[0 1 2 3 4]
Example (Multiple)
package main

import (
	"fmt"
	"sync"

	"github.com/Vonage/gosrvlib/pkg/threadsafe/tsslice"
)

func main() {
	mux := &sync.Mutex{}

	slice := make([]string, 0, 2)
	tsslice.Append(mux, &slice, "Hello", "World")

	fmt.Println(slice)

}
Output:

[Hello World]
Example (Simple)
package main

import (
	"fmt"
	"sync"

	"github.com/Vonage/gosrvlib/pkg/threadsafe/tsslice"
)

func main() {
	mux := &sync.Mutex{}

	slice := make([]string, 0, 2)
	tsslice.Append(mux, &slice, "Hello")
	tsslice.Append(mux, &slice, "World")

	fmt.Println(slice)

}
Output:

[Hello World]
Example (Slice)
package main

import (
	"fmt"
	"sync"

	"github.com/Vonage/gosrvlib/pkg/threadsafe/tsslice"
)

func main() {
	mux := &sync.Mutex{}

	slice := make([]string, 0, 2)
	tsslice.Append(mux, &slice, []string{"Hello", "World"}...)

	fmt.Println(slice)

}
Output:

[Hello World]

func Get

func Get[T any](mux threadsafe.RLocker, slice []T, key int) T

Get is a thread-safe function to get a value by key in a slice.

Example
package main

import (
	"fmt"
	"sync"

	"github.com/Vonage/gosrvlib/pkg/threadsafe/tsslice"
)

func main() {
	mux := &sync.RWMutex{}

	slice := []string{"Hello", "World"}
	fmt.Println(tsslice.Get(mux, slice, 0))
	fmt.Println(tsslice.Get(mux, slice, 1))

}
Output:

Hello
World

func Len

func Len[T any](mux threadsafe.RLocker, slice []T) int

Len is a thread-safe function to get the length of a slice.

Example
package main

import (
	"fmt"
	"sync"

	"github.com/Vonage/gosrvlib/pkg/threadsafe/tsslice"
)

func main() {
	mux := &sync.RWMutex{}

	slice := []string{"Hello", "World"}
	fmt.Println(tsslice.Len(mux, slice))

}
Output:

2

func Set

func Set[T any](mux threadsafe.Locker, slice []T, key int, value T)

Set is a thread-safe function to assign a value to a key in a slice.

Example
package main

import (
	"fmt"
	"sync"

	"github.com/Vonage/gosrvlib/pkg/threadsafe/tsslice"
)

func main() {
	mux := &sync.Mutex{}

	slice := make([]string, 2)
	tsslice.Set(mux, slice, 0, "Hello")
	tsslice.Set(mux, slice, 1, "World")

	fmt.Println(slice)

}
Output:

[Hello World]

Types

This section is empty.

Jump to

Keyboard shortcuts

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