ZhangZhihui's Blog  

 

package main

import (
    "fmt"
    "slices"
)

func main() {
    s1 := []int{1, 2, -1, -2}
    s2 := slices.Clone(s1)
    fmt.Printf("%p\n", s1)
    fmt.Printf("%p\n", s2)

    s1[2] = 0
    s1[3] = 0
    s1 = slices.Compact(s1)  // This is like the uniq command found on Unix. 
    fmt.Println("s1 (compact):", s1)
    fmt.Println(slices.Contains(s1, 2), slices.Contains(s1, -2))

    s3 := make([]int, 10, 100)
    fmt.Println("Len:", len(s3), "Cap:", cap(s3))
    s3 = slices.Clip(s3)  // Clip removes unused capacity from the slice, returning s[:len(s):len(s)].
    fmt.Println("Len:", len(s3), "Cap:", cap(s3))

    fmt.Println("Min:", slices.Min(s1), "Max:", slices.Max(s1))
    // Replace s2[1] and s2[2]
    s2 = slices.Replace(s2, 1, 3, 100, 200)
    fmt.Println("s2 (replaced):", s2)
    slices.Sort(s2)
    fmt.Println("s2 (sorted):", s2)
}

 

zzh@ZZHPC:/zdata/Github/ztest$ go run main.go
0xc00001e1c0
0xc00001e1e0
s1 (compact): [1 2 0]
true false
Len: 10 Cap: 100
Len: 10 Cap: 10
Min: 0 Max: 2
s2 (replaced): [1 100 200 -2]
s2 (sorted): [-2 1 100 200]

 

posted on 2024-06-11 11:22  ZhangZhihuiAAA  阅读(5)  评论(0编辑  收藏  举报