golang

package module
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Jan 1, 2026 License: MIT Imports: 9 Imported by: 0

README

Go Language Generic Repository

English | 中文

Caution 🚧🚫⚠️ Do not use in production as it is currently under construct.

Documentation

Index

Examples

Constants

View Source
const (
	Day              = 24 * time.Hour
	DateTimeZone     = "2006-01-02 15:04:05 MST"           // 带时区的普通时间格式
	DateTimeNanoZone = "2006-01-02 15:04:05.999999999 MST" // 带时区和纳秒的普通时间格式
)

Variables

This section is empty.

Functions

func AddDays

func AddDays(t time.Time, days float64) time.Time

AddDays to add days to time. Not change original time, return new time.

添加天数到时间,该函数不会改变原始时间,而是返回一个新的时间。会有时间误差。

Example
package main

import (
	"fmt"
	"time"

	"github.com/keepitlight/golang"
)

func main() {
	s := time.Now()

	d1 := golang.Day + 12*time.Hour + 40*time.Minute

	e1 := golang.AddDays(s, golang.Days(d1))
	e2 := golang.AddDays(s, -golang.Days(d1))

	fmt.Println(e1.Sub(s).Round(time.Second))
	fmt.Println(e2.Sub(s).Round(time.Second))

}
Output:

36h40m0s
-36h40m0s

func AddTime

func AddTime(t time.Time, hours, minutes, seconds, nanoseconds int) time.Time

AddTime to add hours, minutes, seconds, nanoseconds to time. Not change original time, return new time. This function is different from the time.Time.Add method.

添加小时、分钟、秒、纳秒到时间,该函数不会改变原始时间,而是返回一个新的时间。此方法不同于 time.Time.Add 方法不会增加额外的时间。

Example
package main

import (
	"fmt"
	"time"

	"github.com/keepitlight/golang"
)

func main() {
	s := time.Now()

	e1 := golang.AddTime(s, 1, 30, 0, 0)
	e2 := golang.AddTime(s, -1, -30, 0, 0)

	// There was an additional overhead in runtime time.
	// 有增加额外的运行时时间
	c1 := time.Now().Add(time.Hour)
	c2 := time.Now().Add(-time.Hour)

	fmt.Println(e1.Sub(s))
	fmt.Println(e2.Sub(s))

	fmt.Println(e1.Sub(s) == c1.Sub(s))
	fmt.Println(e2.Sub(s) == c2.Sub(s))

}
Output:

1h30m0s
-1h30m0s
false
false

func Apply

func Apply[R ~*E, E any](ss []E, f func(index int, ele R))

Apply to apply the function f to each element and f can modify the element value.

使用函数 f 对每个元素进行操作,允许 f 修改元素的值

Example
package main

import (
	"fmt"
	"github.com/keepitlight/golang"
)

func main() {
	v := []int{1, 2, 3, 4, 5}
	golang.Apply(v, func(index int, ele *int) {
		*ele *= 2
	})
	fmt.Println(v)
}
Output:

[2 4 6 8 10]

func BigEndianBytes

func BigEndianBytes(v uint64) [8]byte

BigEndianBytes to convert uint64 to bytes corresponding to the big-endian byte order.

根据大端字节序将 uint64 转换为字节数组。

Example
package main

import (
	"fmt"
	"github.com/keepitlight/golang"
)

func main() {
	b := golang.BigEndianBytes(0x1234567890ABCDEF)
	fmt.Printf("%x %x %x %x %x %x %x %x\n", b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7])
}
Output:

12 34 56 78 90 ab cd ef

func Cast

func Cast[E, R any](ss []E, cast func(index int, ele E) (v R, omitted bool)) (result []R)

Cast to convert the slice elements to a new type.

转换切片元素类型,使用函数 cast 对每个元素进行转换,返回新的元素值 v 和 omitted 标记, omitted 为 true,则跳过该元素。 新切片的长度小于(如果有元素被跳过)或等于原切片的长度。

Example
package main

import (
	"fmt"
	"github.com/keepitlight/golang"
)

func main() {
	v := golang.Cast([]int{1, 2, 3, 4, 5}, func(index int, ele int) (v string, omitted bool) {
		return fmt.Sprintf("key%d", ele), ele%2 == 0
	})
	fmt.Println(v)
}
Output:

[key1 key3 key5]

func Days

func Days(duration time.Duration) float64

Days to convert duration to days.

将持续时间转换为天数。

func FoldString

func FoldString(ss ...string) []string

FoldString returns a slice of strings that contains no duplicate strings, interpreted as UTF-8 strings, are equal under simple Unicode case-folding, which is a more general form of case-insensitivity.

返回不重复的字符串(大小写不敏感),不改变切片顺序,重复元素被扔掉

Example
package main

import (
	"fmt"
	"github.com/keepitlight/golang"
)

func main() {
	v := golang.FoldString(
		"a", "b", "c", "d", "A",
		"A", "B", "C", "D", "d",
		"E", "F", "G", "H", "I",
		"e", "f", "g", "h", "i",
		"j", "k", "l", "m", "n",
		"J", "K", "L", "M", "N",
		"o", "p", "q", "r", "s",
		"t", "u", "v", "w", "x",
		"y", "z", "X", "Y", "Z",
	)
	fmt.Println(v)
}
Output:

[a b c d E F G H I j k l m n o p q r s t u v w x y z]

func LittleEndianBytes

func LittleEndianBytes(v uint64) [8]byte

LittleEndianBytes to convert uint64 to bytes corresponding to the little-endian byte order.

根据小端字节序将 uint64 转换为字节数组。

Example
package main

import (
	"fmt"
	"github.com/keepitlight/golang"
)

func main() {
	b := golang.LittleEndianBytes(0x1234567890ABCDEF)
	fmt.Printf("%x %x %x %x %x %x %x %x\n", b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7])
}
Output:

ef cd ab 90 78 56 34 12

func Lookup

func Lookup[E any](ss []E, f func(index int, element E) (found bool)) (found E, index int)

Lookup to look up the element when the f return true.

迭代切片,使用函数 f 对每个元素进行判断,返回判断为 true 的第一个元素。

Example
package main

import (
	"fmt"
	"github.com/keepitlight/golang"
)

func main() {
	v, index := golang.Lookup([]int{1, 2, 3, 4, 5}, func(index int, element int) (found bool) {
		return element == 3
	})
	fmt.Println(v, index)
}
Output:

3 2

func Map

func Map[E any, K comparable, V any](ss []E, f func(index int, ele E) (key K, value V)) (result map[K]V, err error)

Map convert slice to map, use f to convert each element to a new key and value, existing key-value pairs will be overwritten.

以覆盖模式将切片转换为映射,使用函数 cast 对每个元素进行转换,以返回值 key 和 value 作为新的键值对。已存在的键值对将被覆盖。

Example
package main

import (
	"fmt"
	"github.com/keepitlight/golang"
)

func main() {
	v, err := golang.Map([]int{1, 2, 3, 4, 5}, func(index int, ele int) (key string, value int) {
		return fmt.Sprintf("key%d", ele), ele * ele
	})
	fmt.Println(v, err)
}
Output:

map[key1:1 key2:4 key3:9 key4:16 key5:25] <nil>

func MapFunc

func MapFunc[E any, K comparable, V any](ss []E, cast func(index int, ele E) (key K, value V, priority int)) (result map[K]V, err error)

MapFunc to convert slice to map, use f to convert each element to a new key and value, existing key-value pairs be overwritten if priority is greater, the same priority, the existing key is higher.

切片转换为映射,使用函数 f 对每个元素进行转换,返回转换结果。 使用 cast 函数的返回值 priority 决定是否覆盖已存在的键值对,priority 越大,优先级越高,优先级高的覆盖低的,相同时已存在的优先。

Example
package main

import (
	"fmt"
	"github.com/keepitlight/golang"
)

func main() {
	type Preferred int
	const (
		higherIndexPreferred  Preferred = iota // higher index first
		smallerIndexPreferred                  // smaller index first
		greaterValuePreferred                  // higher value first
		smallerValuePreferred                  // smaller value first
	)
	type element struct {
		key   string
		value int
	}
	var preferred = map[string]Preferred{
		"key1": smallerIndexPreferred,
		"key2": higherIndexPreferred,
		"key3": smallerValuePreferred,
		"key4": greaterValuePreferred,
	}
	s := []*element{
		{"key1", 1},
		{"key2", 2},
		{"key3", 3},
		{"key4", 4},
		{"key1", 5},
		{"key2", 6},
		{"key3", 7},
		{"key4", 8},
	}
	v, err := golang.MapFunc(s, func(index int, ele *element) (key string, value int, priority int) {
		key, value = ele.key, ele.value
		switch preferred[ele.key] {
		case smallerIndexPreferred:
			priority = -index
		case smallerValuePreferred:
			priority = -ele.value
		case greaterValuePreferred:
			priority = ele.value
		default:
			priority = index
		}
		return
	})
	fmt.Println(v, err)
}
Output:

map[key1:1 key2:6 key3:3 key4:8] <nil>

func Nonet

func Nonet[A, B, C, D, E, F, G, H, I any](a A, b B, c C, d D, e E, f F, g G, h H, i I) *tuples.Nonet[A, B, C, D, E, F, G, H, I]

Nonet creates a new nonuplet.

创建九元组

func Oct

func Oct[A, B, C, D, E, F, G, H any](a A, b B, c C, d D, e E, f F, g G, h H) *tuples.Oct[A, B, C, D, E, F, G, H]

Oct creates a new octuplet.

创建八元组

func Pair

func Pair[A, B any](a A, b B) *tuples.Pair[A, B]

Pair creates a pair.

创建二元组

func Pick

func Pick[E any](pick func(index int, ele E) bool, vs ...E) (result []E)

Pick to pick the elements when the pick return true.

挑选

Example
package main

import (
	"fmt"
	"github.com/keepitlight/golang"
)

func main() {
	v := golang.Pick(func(index int, ele int) bool { return ele%2 == 0 }, 1, 2, 3, 4, 5)
	fmt.Println(v)
}
Output:

[2 4]

func Quad

func Quad[A, B, C, D any](a A, b B, c C, d D) *tuples.Quad[A, B, C, D]

Quad creates a new quadruplet.

创建四元组

func Quint

func Quint[A, B, C, D, E any](a A, b B, c C, d D, e E) *tuples.Quint[A, B, C, D, E]

Quint creates a new quintuplet.

创建五元组

func Rand

func Rand(buf []byte, options ...RandomOption)

Rand to generate random bytes corresponding to the options.

根据选项生成随机字节。

Example
package main

import (
	"fmt"
	"github.com/keepitlight/golang"
	"slices"
)

func main() {
	var b0 = make([]byte, 10)
	var b1 = make([]byte, 10)
	var b2 = make([]byte, 10)
	var b3 = make([]byte, 10)
	golang.Rand(b1, golang.UsePCG(0))
	golang.Rand(b2, golang.UseChaCha8())
	golang.Rand(b3, golang.CryptoRand())
	fmt.Println(len(b1), len(b2), len(b3))
	fmt.Println(slices.Equal(b1, b0), slices.Equal(b2, b0), slices.Equal(b3, b0))
	fmt.Println(slices.Equal(b1, b2), slices.Equal(b1, b3), slices.Equal(b2, b3))
}
Output:

10 10 10
false false false
false false false

func Reduce

func Reduce[S ~[]E, E any, R any](ss S, f func(R, E) R, initial R) R

Reduce returns the result of reducing the slices using the function f.

收敛,迭代切片,使用函数 f 对每个元素进行计算,并将之前迭代的计算结果作为下一个迭代的输入参数,返回计算结果。

Example
package main

import (
	"fmt"
	"github.com/keepitlight/golang"
)

func main() {
	sum := func(prev, next int) int { return prev + next }
	v := golang.Reduce([]int{1, 2, 3, 4, 5}, sum, 10)
	fmt.Println(v)
}
Output:

25

func RefreshSeed

func RefreshSeed()

RefreshSeed to refresh the random number generator.

刷新随机数生成器种子。

func Repeat

func Repeat[T any](source []T, count int) (result []T)

Repeat returns a slice of elements that contains the given source repeated count times.

将切片 source 的元素重复 count 次。

Example
package main

import (
	"fmt"
	"github.com/keepitlight/golang"
)

func main() {
	v := golang.Repeat([]int{1, 2, 3}, 3)
	fmt.Println(v)
}
Output:

[1 2 3 1 2 3 1 2 3]

func Replace

func Replace[E any](ss []E, f func(index int, ele E) (v E, omitted bool))

Replace to replace the original element if f returns a new value and omitted is false.

替换元素,函数 f 返回值 omitted 为 false 则替换原元素。

Example
package main

import (
	"fmt"
	"github.com/keepitlight/golang"
)

func main() {
	v := []int{1, 2, 3, 4, 5}
	golang.Replace(v, func(index int, ele int) (v int, omitted bool) {
		return ele * ele, ele%2 == 0 // omit even values
	})
	fmt.Println(v)
}
Output:

[1 2 9 4 25]

func Sept

func Sept[A, B, C, D, E, F, G any](a A, b B, c C, d D, e E, f F, g G) *tuples.Sept[A, B, C, D, E, F, G]

Sept creates a new septuplet.

创建七元组

func Sextet

func Sextet[A, B, C, D, E, F any](a A, b B, c C, d D, e E, f F) *tuples.Sextet[A, B, C, D, E, F]

Sextet creates a new sextuplet.

创建六元组

func Shuffle

func Shuffle(length int, swap func(i, j int), options ...RandomOption)

Shuffle the order of elements. length is the number of elements. Shuffle panics if length < 0. swap swaps the elements with indexes i and j.

乱序排序, length 为元素长度,swap 为交换元素函数, 当 length < 0 时 Shuffle 函数会抛出异常

Example
package main

import (
	"fmt"
	"github.com/keepitlight/golang"
	"slices"
)

func main() {
	v1 := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
	v2 := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
	v3 := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

	golang.Shuffle(len(v1), func(i, j int) {
		v1[i], v1[j] = v1[j], v1[i]
	}, golang.UsePCG(0))

	golang.Shuffle(len(v2), func(i, j int) {
		v2[i], v2[j] = v2[j], v2[i]
	}, golang.UseChaCha8())

	golang.Shuffle(len(v3), func(i, j int) {
		v3[i], v3[j] = v3[j], v3[i]
	}, golang.CryptoRand())

	//fmt.Println(v1, v2, v3)
	fmt.Println(
		slices.Equal(v1, v2),
		slices.Equal(v1, v3),
		slices.Equal(v2, v3),
	)
	slices.Sort(v1)
	slices.Sort(v2)
	slices.Sort(v3)
	fmt.Println(
		slices.Equal(v1, v2),
		slices.Equal(v1, v3),
		slices.Equal(v2, v3),
	)
}
Output:

false false false
true true true

func ShuffleSlice

func ShuffleSlice[E any](ss []E, options ...RandomOption)

ShuffleSlice to shuffle the slice using a cryptographically pseudo-random number generator. Argument seeds is optional, used to set the increment seed of PCG random number generator.

使用随机数,随机打乱切片。

Example
package main

import (
	"fmt"
	"github.com/keepitlight/golang"
	"slices"
)

func main() {
	v1 := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
	v2 := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
	v3 := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

	golang.ShuffleSlice(v1, golang.UsePCG(0))
	golang.ShuffleSlice(v2, golang.UseChaCha8())
	golang.ShuffleSlice(v3, golang.CryptoRand())

	//fmt.Println(v1, v2, v3)
	fmt.Println(
		slices.Equal(v1, v2),
		slices.Equal(v1, v3),
		slices.Equal(v2, v3),
	)
	slices.Sort(v1)
	slices.Sort(v2)
	slices.Sort(v3)
	fmt.Println(
		slices.Equal(v1, v2),
		slices.Equal(v1, v3),
		slices.Equal(v2, v3),
	)
}
Output:

false false false
true true true

func ShuffledSlice

func ShuffledSlice[E any](ss []E, options ...RandomOption) (result []E)

ShuffledSlice return a new slice using a random number generator. Argument seeds is optional, used to set the increment seed of PCG random number generator.

返回一个新的使用随机数随机打乱的切片。seeds 为可选参数,用于设置 PCG 随机数生成器的增量种子,其它随机数类型不需要。

Example
package main

import (
	"fmt"
	"github.com/keepitlight/golang"
	"slices"
)

func main() {
	s := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
	v1 := golang.ShuffledSlice(s, golang.UsePCG(0))
	v2 := golang.ShuffledSlice(s, golang.UseChaCha8())

	fmt.Println(slices.Equal(s, v1), slices.Equal(s, v2), slices.Equal(v1, v2))
	slices.Sort(v1)
	slices.Sort(v2)
	fmt.Println(slices.Equal(s, v1), slices.Equal(s, v2), slices.Equal(v1, v2))
}
Output:

false false false
true true true

func Slices

func Slices[T any](length int, initial ...T) (result []T)

Slices to create a slice of argument length, and fill it with the given initial element, which may be empty.

创建一个指定长度的切片,并使用给定的元素 initial 按序对结果切片进行填充, 如果 initial 为空,则创建一个指定长度的切片,不填充任何元素, 如果 initial 的长度大于 length,则截取 initial 的前 length 个元素填充到结果切片中, 如果 initial 的长度小于 length,则重复 initial 的元素填充到结果切片中。

Example
package main

import (
	"fmt"
	"github.com/keepitlight/golang"
)

func main() {
	v := golang.Slices(12, 1, 2, 3, 4, 5)
	fmt.Println(v)
}
Output:

[1 2 3 4 5 1 2 3 4 5 1 2]

func T3

func T3[A, B, C any](a A, b B, c C) *tuples.Triplet[A, B, C]

T3 creates a new Triplet.

创建三元组

func T4

func T4[A, B, C, D any](a A, b B, c C, d D) *tuples.Quad[A, B, C, D]

T4 creates a new Quad(quadruplet).

创建四元组

func T5

func T5[A, B, C, D, E any](a A, b B, c C, d D, e E) *tuples.Quint[A, B, C, D, E]

T5 creates a new Quint(quintuplet).

创建五元组

func T6

func T6[A, B, C, D, E, F any](a A, b B, c C, d D, e E, f F) *tuples.Sextet[A, B, C, D, E, F]

T6 creates a new Sextet(sextuplet).

创建六元组

func T7

func T7[A, B, C, D, E, F, G any](a A, b B, c C, d D, e E, f F, g G) *tuples.Sept[A, B, C, D, E, F, G]

T7 creates a new Sept(septuplet).

创建七元组

func T8

func T8[A, B, C, D, E, F, G, H any](a A, b B, c C, d D, e E, f F, g G, h H) *tuples.Oct[A, B, C, D, E, F, G, H]

T8 creates a new Oct(octuplet).

创建八元组

func T9

func T9[A, B, C, D, E, F, G, H, I any](a A, b B, c C, d D, e E, f F, g G, h H, i I) *tuples.Nonet[A, B, C, D, E, F, G, H, I]

T9 creates a new Nonet(nonuplet).

创建九元组

func TimeCompare

func TimeCompare(a, b *time.Time) int

func Triplet

func Triplet[A, B, C any](a A, b B, c C) *tuples.Triplet[A, B, C]

Triplet creates a new triplet.

创建三元组

func Tuple

func Tuple[A, B any](a A, b B) *tuples.Pair[A, B]

Tuple creates a new Pair.

创建二元组

Example
package main

import (
	"fmt"

	"github.com/keepitlight/golang"
)

func main() {
	t := golang.Tuple(0, "a")
	fmt.Println(t.Value())
}
Output:

0 a

func Tuple3

func Tuple3[A, B, C any](a A, b B, c C) *tuples.Triplet[A, B, C]

Tuple3 creates a new Triplet.

创建三元组

Example
package main

import (
	"fmt"

	"github.com/keepitlight/golang"
)

func main() {
	t := golang.Tuple3(0, "a", true)
	fmt.Println(t.Value())
}
Output:

0 a true

func Tuple4

func Tuple4[A, B, C, D any](a A, b B, c C, d D) *tuples.Quad[A, B, C, D]

Tuple4 creates a new Quad(quadruplet).

创建四元组

Example
package main

import (
	"fmt"

	"github.com/keepitlight/golang"
)

func main() {
	t := golang.Tuple4(0, "a", true, 1.1)
	fmt.Println(t.Value())
}
Output:

0 a true 1.1

func Tuple5

func Tuple5[A, B, C, D, E any](a A, b B, c C, d D, e E) *tuples.Quint[A, B, C, D, E]

Tuple5 creates a new Quint(quintuplet).

创建五元组

Example
package main

import (
	"fmt"
	"time"

	"github.com/keepitlight/golang"
)

func main() {
	n := time.Date(2024, 8, 8, 8, 8, 8, 888, time.UTC)
	t := golang.Tuple5(0, "a", true, 1.1, n)
	fmt.Println(t.Value())
}
Output:

0 a true 1.1 2024-08-08 08:08:08.000000888 +0000 UTC

func Tuple6

func Tuple6[A, B, C, D, E, F any](a A, b B, c C, d D, e E, f F) *tuples.Sextet[A, B, C, D, E, F]

Tuple6 creates a new Sextet(sextuplet).

创建六元组

func Tuple7

func Tuple7[A, B, C, D, E, F, G any](a A, b B, c C, d D, e E, f F, g G) *tuples.Sept[A, B, C, D, E, F, G]

Tuple7 creates a new Sept(septuplet).

创建七元组

func Tuple8

func Tuple8[A, B, C, D, E, F, G, H any](a A, b B, c C, d D, e E, f F, g G, h H) *tuples.Oct[A, B, C, D, E, F, G, H]

Tuple8 creates a new Oct(octuplet).

创建八元组

func Tuple9

func Tuple9[A, B, C, D, E, F, G, H, I any](a A, b B, c C, d D, e E, f F, g G, h H, i I) *tuples.Nonet[A, B, C, D, E, F, G, H, I]

Tuple9 creates a new Nonet(nonuplet).

创建九元组

func Unique

func Unique[E comparable](ss []E) (re []E)

Unique returns a slice of elements that contains no duplicate elements, keeping the original order

返回不重复的元素切片,保持非重复切片元素的相对顺序,索引大的重复元素被扔掉。 注意,不同于 pie.Unique 方法与 slices.Compact 方法

func UniqueFunc

func UniqueFunc[E any](ss []E, equal func(a, b E) bool) (result []E)

UniqueFunc returns a slice of elements that contains no duplicate elements, keeping the original order

根据比较方法的结果返回不重复的元素切片,不改变切片顺序,检索到重复的元素时,索引大的重复元素被扔掉

Example
package main

import (
	"fmt"
	"github.com/keepitlight/golang"
)

func main() {
	v := golang.UniqueFunc([]int{1, 1, 2, 3, 4, 5, 4, 5}, func(a, b int) bool {
		return a == b
	})
	fmt.Println(v)
}
Output:

[1 2 3 4 5]

func UniqueString

func UniqueString(ss ...string) []string

UniqueString returns a slice of strings that contains no duplicate strings, interpreted as UTF-8 strings.

返回不重复的字符串(大小写敏感),不改变切片顺序,重复元素被扔掉

Example
package main

import (
	"fmt"
	"github.com/keepitlight/golang"
)

func main() {
	v := golang.UniqueString(
		"a", "b", "c", "d", "a",
		"a", "b", "c", "d", "d",
		"e", "f", "g", "h", "i",
		"e", "f", "g", "h", "i",
		"j", "k", "l", "m", "n",
		"j", "k", "l", "m", "n",
		"o", "p", "q", "r", "s",
		"o", "p", "q", "r", "s",
		"t", "u", "v", "w", "x",
		"t", "u", "v", "w", "x",
		"y", "z", "x", "y", "z",
	)
	fmt.Println(v)
}
Output:

[a b c d e f g h i j k l m n o p q r s t u v w x y z]

Types

type CryptoSource

type CryptoSource struct{}

func (*CryptoSource) Uint64

func (c *CryptoSource) Uint64() uint64

type Interval

type Interval[T any] interface {
	Range[T]
	// Next return current value and advances the interval to the next value
	//
	// 返回当前值并移动区间到下一个值
	Next() (current T, end bool)
	// Init initializes or resets the interval
	//
	// 初始化或重置区间
	Init()
}

Interval declares an interval of type T

区间,表示一组连续值的范围

type RWLocker

type RWLocker interface {
	RLock()
	RUnlock()
}

type RandomOption

type RandomOption func(r *randOptions)

func CryptoRand

func CryptoRand() RandomOption

func RefreshSeedDuration

func RefreshSeedDuration(d time.Duration) RandomOption

func UseChaCha8

func UseChaCha8() RandomOption

func UsePCG

func UsePCG(incrementSeed uint64) RandomOption

type RandomSourceType

type RandomSourceType int // 随机源类型
const (
	// PCG is a family of simple fast space-efficient statistically good algorithms for random number generation.
	//
	// 来源:PCG 是一类随机数生成器的统称,它基于线性同余方法,并通过使用非线性操作(如XOR和旋转)来提高随机性。
	// PCG 的设计目标是在提供良好随机性的前提下,达到高性能。
	//
	// 特性:PCG 生成器具有周期长、状态空间大、可重复性和可跳转性等优点,这意味着它可以生成大量的随机数序列,
	// 而且这些序列之间不易出现重复。
	//
	// 用途:PCG 适合于需要高吞吐量和低延迟的随机数生成场景,尤其是在多线程环境中,因为 PCG 支持并行生成独立的随机数流。
	PCG RandomSourceType = iota
	// ChaCha8 is a fast, simple, and secure stream cipher.
	//
	// 来源:ChaCha8 是基于 ChaCha 流密码的变体,它被设计用于加密和随机数生成。ChaCha8 使用较少的轮次(8轮),因此与标准 ChaCha20 相比,
	// 它的速度更快,但安全级别较低。
	//
	// 安全性:尽管 ChaCha8 不如 ChaCha20 安全,但它仍然提供了足够的随机性,适用于大多数非加密应用。由于它源自加密算法,
	// ChaCha8 提供了良好的统计特性。
	//
	// 用途:ChaCha8 适合于需要快速生成高质量随机数的场景,例如游戏、模拟和一般用途的随机化需求。
	ChaCha8
	// Crypto is a cryptographically secure random number generator.
	//
	// 来源:Crypto 是一个加密安全的随机数生成器。
	Crypto
)

type Range

type Range[T any] interface {
	// Bounds returns the lower and upper bounds of the range
	//
	// 返回范围的下限和上限
	Bounds() (lower, upper T)
	// Comparer returns a function that compares two values of type T
	//
	// 返回一个比较两个值类型的函数
	Comparer() func(a, b T) int
}

Range declares a range of type T

范围,值域

Directories

Path Synopsis
i18n

Jump to

Keyboard shortcuts

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