首页 > 其他分享 >Zgo - slices

Zgo - slices

时间:2024-06-11 11:34:05浏览次数:14  
标签:slices s3 s2 s1 Zgo Println fmt

 

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]

 

标签:slices,s3,s2,s1,Zgo,Println,fmt
From: https://www.cnblogs.com/zhangzhihui/p/18241757

相关文章

  • Zgo - csv_data.go
     packagemainimport("encoding/csv""log""os")typeRecordstruct{NamestringSurnamestringNumberstringLastAccessstring}varmyData=[]Record{}funcreadCSVFile(file......
  • Zgo - randInt, randString
     packagemainimport("fmt""math/rand""strings")const(//AsweonlywanttogetprintableASCIIcharacters,welimittherangeofpseudo-randomnumbers//thatcanbegenerated.Thetotalnumber......
  • Zgo - custom_log.go
     packagemainimport("fmt""io""log""os""path")funcmain(){flag:=os.O_APPEND|os.O_CREATE|os.O_WRONLYlogFile:=path.Join(os.TempDir(),"mGo.log")......
  • Zgo - stats.go
     packagemainimport("fmt""math""os""slices""strconv")funcmain(){args:=os.Argsiflen(args)==1{fmt.Println("Needoneormorearguments!")......
  • Zgo - which.go
     packagemainimport("fmt""os""path/filepath")funcmain(){args:=os.Argsiflen(args)==1{fmt.Println("Pleaseprovideanargument!")return}file:=args......
  • Go - #70: Using mutexes inaccurately with slices and maps
      ......
  • Go 100 mistakes - #26: Slices and memory leaks
        Asaruleofthumb,rememberthatslicingalargesliceorarraycanleadtopotential highmemoryconsumption.Theremainingspacewon’tbereclaimedbytheGC,and wecankeepalargebackingarraydespiteusingonlyafewelements.Using......
  • Go官方放出泛型slices包
    阅读本文大概需要6分钟。slices 标准库是Go1.21新增的一个包,它提供了许多对切片(slices)进行常见操作的泛型函数,可以适用于任何元素类型的切片。切片是Go语言中一种重要的数据结构,它可以动态地存储和管理一组相同类型的元素。切片的底层实现是一个数组,但是切片可以根据需要......
  • Chapter 3.1 复合类型-Arrays,Slices
    数组Arrays数组在Go中很少被直接使用,因为数组的长度被作为类型的一部分被使用[3]int[5]int是不同的类型这个数组和C语言的数组很不一样,C的数组变量就是指向数组的指针,但是offset是0你不能使用一个变量代表数组的长度,类型不是在运行时确定的,它必须在编译时确定,这......
  • Go - Sorting Arrays or Slices
    Problem: Youwanttosortelementsinanarrayorslice.Solution: Forint,float64,andstringarraysorslicesyoucanusesort.Ints,sort.Float64s,andsort.Strings.Youcanalsouseacustomcomparatorbyusingsort.Slice.Forstructs,youcan......