当前位置:网站首页>[go] introduction to exp

[go] introduction to exp

2022-06-09 11:01:00 alwaysrun


go Official experimental package https://pkg.go.dev/golang.org/x/exp Contains many useful packages ; adopt go get golang.org/x/exp Easily accessible .

exp package

exp The package is experimental , It may be merged into the main branch or discarded ; And there is no guarantee that Go1 The compatibility of .

constraints

constraints Some useful generic parameter constraint sets are defined in the package :

  • Complex: The plural
  • Float: Floating point numbers
  • Integer: Integers
    • Signed: Signed integers
    • Unsigned: Unsigned integer
  • Ordered: Numbers and strings

maps

maps Some useful methods are defined in , Used for processing map And type :

  • Clear(m): Empty m Items in ;
  • Clone(m): return m A copy of ( Both keys and values are shallow copies );
  • Copy(dst, src): hold src The entry in is appended to dst in ( The key with the same name is overwritten );
  • DeleteFunc(m, del func(K, V) bool): Delete entries that meet the criteria ;
  • Equal(m1, m2): Compare the two map Whether it is equal or not
  • EqualFunc(m1, m2, eq func(V1, V2) bool): Compare two according to the conditions map Whether it is equal or not ;
  • Keys(m)[]K: return map All the keys in ( Uncertain order );
  • Values(m)[]V: return map All the values in ( Uncertain order );

rand

rand Package to achieve a pseudo-random number generator :

  • ExpFloat64() float64: Returns... In exponential form (0, +math.MaxFloat64] number of rooms ;
  • Float32() float32: return [0.0,1.0) Inter floating point number ;
  • Float64() float64: return [0.0,1.0) Inter floating point number ;
  • Int() int: Returns a nonnegative integer ;
  • Int31() int32: Returns nonnegative 31 An integer ;
  • Int31n(n int32) int32: return [0,n) The whole number between ;
  • Int63() int64: Returns a nonnegative integer ;
  • Int63n(n int64) int64: Returns nonnegative 63 An integer ;
  • Intn(n int) int: return [0,n) The whole number between ;
  • NormFloat64() float64: Returns the normal distribution [-math.MaxFloat64, +math.MaxFloat64] Number between ;
  • Perm(n int) []int: Return contains [0,n) Slice all the numbers ( The order is random );
  • Read(p []byte) (n int, err error): fill p Is a random number ;
  • Seed(seed uint64): Set random seeds ;
  • Shuffle(n int, swap func(i, j int)): Shuffle ( according to i,j Exchange elements );
  • Uint32() uint32: return 32 Bit unsigned integer ;
  • Uint64() uint64: return 64 Bit unsigned integer ;
func main() {
    
	numbers := []byte("12345")
	letters := []byte("ABCDE")
	// Shuffle numbers, swapping corresponding entries in letters at the same time.
	rand.Shuffle(len(numbers), func(i, j int) {
    
		numbers[i], numbers[j] = numbers[j], numbers[i]
		letters[i], letters[j] = letters[j], letters[i]
	})
	for i := range numbers {
    
		fmt.Printf("%c: %c\n", letters[i], numbers[i])
	}
}

slices

slices Some useful methods for processing slices are defined in :

  • BinarySearch(x []E, target E) (int, bool): Bisect the slices in ascending order ( If you find , Return its subscript ; Otherwise, it is the position where it should be inserted );
  • BinarySearchFunc(x []E, target E, cmp func(E, E) int) (int, bool): Binary search in the specified way ;
  • Clip(s S) S: tailoring ( Remove excess unused space );
  • Clone(s S) S: Copy slices ( Elements use light copies );
  • Compact(s S) S: Return compression ( Remove adjacent identical elements , Leave only one copy ) After slicing , The original slice has also been modified ( But the length has not changed );
  • CompactFunc(s S, eq func(E, E) bool) S: Compress in the specified way ;
  • Compare(s1, s2 []E) int: Compare ;
  • CompareFunc(s1 []E1, s2 []E2, cmp func(E1, E2) int) int: Compare ;
  • Contains(s []E, v E) bool: Whether there is ;
  • Delete(s S, i, j int) S: remove [i:j) Inter element ;
  • Equal(s1, s2 []E) bool: Whether it is equal or not ;
  • EqualFunc(s1 []E1, s2 []E2, eq func(E1, E2) bool) bool: Whether it is equal or not ;
  • Grow(s S, n int) S: Add capacity ;
  • Index(s []E, v E) int: return v The first index to appear , No return found -1;
  • IndexFunc(s []E, f func(E) bool) int: Find... As specified ;
  • Insert(s S, i int, v …E) S: stay i Position insert element ;
  • IsSorted(x []E) bool: Determine whether to sort ;
  • IsSortedFunc(x []E, less func(a, b E) bool) bool: Determine whether to sort ;
  • Sort(x []E): Sort ( Ascending );
  • SortFunc(x []E, less func(a, b E) bool): Sort as specified ;
  • SortStableFunc(x []E, less func(a, b E) bool): Stable sequencing ;
func deleteElem() {
    
	s := []int{
    1, 2, 3, 4, 5, 6, 7}
	fmt.Println("before:", s)
	s2 := slices.Delete(s, 0, 1)
	fmt.Println("ori:", s)
	fmt.Println("del:", s2)
}

// before: [1 2 3 4 5 6 7]
// ori: [2 3 4 5 6 7 7]
// del: [2 3 4 5 6 7] 
原网站

版权声明
本文为[alwaysrun]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206091012223089.html

随机推荐