syncx

package
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2023 License: Apache-2.0 Imports: 1 Imported by: 12 modules, 11 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Map

type Map[K comparable, V any] struct {
	// contains filtered or unexported fields
}

Map 是对 sync.Map 的一个泛型封装 要注意,K 必须是 comparable 的,并且谨慎使用指针作为 K。 使用指针的情况下,两个 key 是否相等,仅仅取决于它们的地址 而不是地址指向的值。可以参考 Load 测试。 注意,key 不存在和 key 存在但是值恰好为零值(如 nil),是两码事

func (*Map[K, V]) Delete

func (m *Map[K, V]) Delete(key K)

Delete 删除键值对

Example
var m Map[string, int]
m.Store("key1", 123)
val, ok := m.Load("key1")
if ok {
	fmt.Printf("key1 = %d\n", val)
}
m.Delete("key1")
_, ok = m.Load("key1")
if !ok {
	fmt.Println("key1 已被删")
}
Output:
key1 = 123
key1 已被删

func (*Map[K, V]) Load

func (m *Map[K, V]) Load(key K) (value V, ok bool)

Load 加载键值对

Example
var m Map[string, int]
m.Store("key1", 123)
val, ok := m.Load("key1")
if ok {
	fmt.Println(val)
}
Output:
123

func (*Map[K, V]) LoadAndDelete

func (m *Map[K, V]) LoadAndDelete(key K) (value V, loaded bool)

LoadAndDelete 加载并且删除一个键值对

Example
var m = Map[string, *User]{}
_, loaded := m.LoadAndDelete("Tom")
fmt.Println(loaded)
m.Store("Tom", nil)
val, loaded := m.LoadAndDelete("Tom")
if loaded {
	fmt.Printf("key=Tom, val=%v 被删除\n", val)
}
m.Store("Tom", &User{Name: "Tom"})
val, loaded = m.LoadAndDelete("Tom")
if loaded {
	fmt.Printf("key=Tom, val=%v 被删除\n", val)
}
Output:
false
key=Tom, val=<nil> 被删除
key=Tom, val=&{Tom} 被删除

func (*Map[K, V]) LoadOrStore

func (m *Map[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool)

LoadOrStore 加载或者存储一个键值对

Example
var m = Map[string, *User]{}
_, loaded := m.LoadOrStore("Tom", &User{Name: "Tom"})
// 执行存储
if !loaded {
	fmt.Println("设置了新值 Tom")
}

_, loaded = m.LoadOrStore("Tom", &User{Name: "Tom-copy"})
// Tom 这个 key 已经存在,执行加载
if loaded {
	fmt.Println("加载旧值 Tom")
}

_, loaded = m.LoadOrStore("Jerry", nil)
// 执行存储,注意值是 nil
if !loaded {
	fmt.Println("设置了新值 nil")
}
val, loaded := m.LoadOrStore("Jerry", &User{Name: "Jerry"})
// Jerry 这个 key 已经存在,执行加载,于是把原本的 nil 加载出来
if loaded {
	fmt.Printf("加载旧值 %v", val)
}
Output:
设置了新值 Tom
加载旧值 Tom
设置了新值 nil
加载旧值 <nil>

func (*Map[K, V]) Range

func (m *Map[K, V]) Range(f func(key K, value V) bool)

Range 遍历, f 不能为 nil 传入 f 的时候,K 和 V 直接使用对应的类型,如果 f 返回 false,那么就会中断遍历

Example
var m Map[string, int]
m.Store("Tom", 18)
m.Store("Jerry", 35)
var sum int
m.Range(func(key string, val int) bool {
	sum += val
	return true
})
fmt.Println(sum)
Output:
53

func (*Map[K, V]) Store

func (m *Map[K, V]) Store(key K, value V)

Store 存储键值对

Example
var m Map[string, int]
m.Store("key1", 123)
val, ok := m.Load("key1")
if ok {
	fmt.Printf("key1 = %d\n", val)
}
Output:
key1 = 123

type Pool

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

Pool 是对 sync.Pool 的简单封装 会有一些性能损耗,但是基本可以忽略不计。担忧性能问题的可以参考

func NewPool

func NewPool[T any](factory func() T) *Pool[T]

NewPool 创建一个 Pool 实例 factory 必须返回 T 类型的值,并且不能返回 nil

func (*Pool[T]) Get

func (p *Pool[T]) Get() T

Get 取出一个元素

func (*Pool[T]) Put

func (p *Pool[T]) Put(t T)

Put 放回去一个元素

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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