Go-切片类型详解(遍历、内存、追加、插入、删除等)

简介: Go-切片类型详解(遍历、内存、追加、插入、删除等)

上篇文章思考题

Go-数组类型详解

答案:

can not use nums(type [6]int) as type[5]int

注意:一个数组类型,包含元素类型和长度,不同长度,同样的元素也是不一样的类型。因此,今天的切片就很有意义。

简介

  • 切片是引用类型
  • 长度可以变化,容量随长度变化
  • 是结构体-->可查看源代码

切片即动态数组,底层在当前数组不够用时,开辟更大的数组,拷贝后再增加元素。

声明

var 变量名 []type

func make(Type, size ...IntegerType[,capacity]) Type

内建函数make分配并初始化一个类型为切片、映射、或通道的对象。其第一个实参为类型,而非值。make的返回类型与其参数相同,而非指向它的指针。其具体结果取决于具体的类型:

切片:size指定了其长度。该切片的容量等于其长度。切片支持第二个整数实参可用来指定不同的容量;它必须不小于其长度,因此 make([]int, 0, 10) 会分配一个长度为0,容量为10的切片。

capacity可选,默认为指定的长度,make底层也有数组,不可见

源代码查看

src->runtime->slice.go

func makeslice(et *_type, len, cap int) unsafe.Pointer {
  mem, overflow := math.MulUintptr(et.size, uintptr(cap))
  if overflow || mem > maxAlloc || len < 0 || len > cap {
    // NOTE: Produce a 'len out of range' error instead of a
    // 'cap out of range' error when someone does make([]T, bignumber).
    // 'cap out of range' is true too, but since the cap is only being
    // supplied implicitly, saying len is clearer.
    // See golang.org/issue/4085.
    mem, overflow := math.MulUintptr(et.size, uintptr(len))
    if overflow || mem > maxAlloc || len < 0 {
      panicmakeslicelen()
    }
    panicmakeslicecap()
  }
  return mallocgc(mem, et, true)
}

切片声明代码

  var slice []int
  slice1 := make([]int,5)
  fmt.Println("slice slice1:",slice,slice1)

声明并初始化

一般形式

类似数组,直接写后面花括号里面,代码:

  slice2 := []int{1,2,3,4}

引用数组

给出数组数据

arr := [5]int{5,6,7,8,9}

slice[start:end],默认:start=0,end =len(arr)

代码

slice3 := arr[1:]

引用切片

和引用数组类似

slice4 := slice3[1:]

切片及数组在内存的情况,请查看后序内存一节。

遍历

for

  for i:=0;i<len(slice3);i++{
    fmt.Print(slice3[i]," ")
  }

for range

  for _,v := range slice3{
    fmt.Print(v," ")
  }

内存

查看结构体的具体内容

src->reflect->type.go

1. type SliceHeader struct {
2.  Data uintptr
3.  Len  int
4.  Cap  int
5. }

编译时创建切片代码

src->cmd->compile->types

// NewSlice returns the slice Type with element type elem.
func NewSlice(elem *Type) *Type {
  if t := elem.Cache.slice; t != nil {
    if t.Elem() != elem {
      Fatalf("elem mismatch")
    }
    return t
  }
  t := New(TSLICE)
  t.Extra = Slice{Elem: elem}
  elem.Cache.slice = t
  return t
}
  fmt.Printf("&slice1:%p,&slice1[0]:%v\n", &slice1, &slice1[0])
  fmt.Printf("&arr:%p &arr[1]:%v &slice3:%p &slice3[0]:%v\n",&arr,&arr[1],&slice3,&slice3[0])
  fmt.Printf("&slice4:%p &slice4[0]:%v\n",&slice4,&slice4[0])
  arr[2] = 99
  fmt.Println("slice3[1] slice4[0]",slice3[1],slice4[0])

2020062310470442.png

slice1内存

2020062310470442.png

arr、slice3、slice4内存

函数/方法

长度与容量

len、cap函数获取长度和容量

代码

fmt.Println("len(slice3) cap(slice3) len(slice4) cap(slice4):", len(slice3), cap(slice3), len(slice4), cap(slice4))

追加与拷贝

append

func append(slice []Type, elems ...Type) []Type

内建函数append将元素追加到切片的末尾。若它有足够的容量,其目标就会重新切片以容纳新的元素。否则,就会分配一个新的基本数组。append返回更新后的切片,因此必须存储追加后的结果。

查看slice增长源代码

src->runtime->slice.go

// growslice handles slice growth during append.
// It is passed the slice element type, the old slice, and the desired new minimum capacity,
// and it returns a new slice with at least that capacity, with the old data
// copied into it.
// The new slice's length is set to the old slice's length,
// NOT to the new requested capacity.
// This is for codegen convenience. The old slice's length is used immediately
// to calculate where to write new values during an append.
// TODO: When the old backend is gone, reconsider this decision.
// The SSA backend might prefer the new length or to return only ptr/cap and save stack space.
func growslice(et *_type, old slice, cap int) slice {
  if raceenabled {
    callerpc := getcallerpc()
    racereadrangepc(old.array, uintptr(old.len*int(et.size)), callerpc, funcPC(growslice))
  }
  if msanenabled {
    msanread(old.array, uintptr(old.len*int(et.size)))
  }
  if cap < old.cap {
    panic(errorString("growslice: cap out of range"))
  }
  if et.size == 0 {
    // append should not create a slice with nil pointer but non-zero len.
    // We assume that append doesn't need to preserve old.array in this case.
    return slice{unsafe.Pointer(&zerobase), old.len, cap}
  }
  newcap := old.cap
  doublecap := newcap + newcap
  if cap > doublecap {
    newcap = cap
  } else {
    if old.cap < 1024 {
      newcap = doublecap
    } else {
      // Check 0 < newcap to detect overflow
      // and prevent an infinite loop.
      for 0 < newcap && newcap < cap {
        newcap += newcap / 4
      }
      // Set newcap to the requested cap when
      // the newcap calculation overflowed.
      if newcap <= 0 {
        newcap = cap
      }
    }
  }
  var overflow bool
  var lenmem, newlenmem, capmem uintptr
  // Specialize for common values of et.size.
  // For 1 we don't need any division/multiplication.
  // For sys.PtrSize, compiler will optimize division/multiplication into a shift by a constant.
  // For powers of 2, use a variable shift.
  switch {
  case et.size == 1:
    lenmem = uintptr(old.len)
    newlenmem = uintptr(cap)
    capmem = roundupsize(uintptr(newcap))
    overflow = uintptr(newcap) > maxAlloc
    newcap = int(capmem)
  case et.size == sys.PtrSize:
    lenmem = uintptr(old.len) * sys.PtrSize
    newlenmem = uintptr(cap) * sys.PtrSize
    capmem = roundupsize(uintptr(newcap) * sys.PtrSize)
    overflow = uintptr(newcap) > maxAlloc/sys.PtrSize
    newcap = int(capmem / sys.PtrSize)
  case isPowerOfTwo(et.size):
    var shift uintptr
    if sys.PtrSize == 8 {
      // Mask shift for better code generation.
      shift = uintptr(sys.Ctz64(uint64(et.size))) & 63
    } else {
      shift = uintptr(sys.Ctz32(uint32(et.size))) & 31
    }
    lenmem = uintptr(old.len) << shift
    newlenmem = uintptr(cap) << shift
    capmem = roundupsize(uintptr(newcap) << shift)
    overflow = uintptr(newcap) > (maxAlloc >> shift)
    newcap = int(capmem >> shift)
  default:
    lenmem = uintptr(old.len) * et.size
    newlenmem = uintptr(cap) * et.size
    capmem, overflow = math.MulUintptr(et.size, uintptr(newcap))
    capmem = roundupsize(capmem)
    newcap = int(capmem / et.size)
  }
  // The check of overflow in addition to capmem > maxAlloc is needed
  // to prevent an overflow which can be used to trigger a segfault
  // on 32bit architectures with this example program:
  //
  // type T [1<<27 + 1]int64
  //
  // var d T
  // var s []T
  //
  // func main() {
  //   s = append(s, d, d, d, d)
  //   print(len(s), "\n")
  // }
  if overflow || capmem > maxAlloc {
    panic(errorString("growslice: cap out of range"))
  }
  var p unsafe.Pointer
  if et.ptrdata == 0 {
    p = mallocgc(capmem, nil, false)
    // The append() that calls growslice is going to overwrite from old.len to cap (which will be the new length).
    // Only clear the part that will not be overwritten.
    memclrNoHeapPointers(add(p, newlenmem), capmem-newlenmem)
  } else {
    // Note: can't use rawmem (which avoids zeroing of memory), because then GC can scan uninitialized memory.
    p = mallocgc(capmem, et, true)
    if lenmem > 0 && writeBarrier.enabled {
      // Only shade the pointers in old.array since we know the destination slice p
      // only contains nil pointers because it has been cleared during alloc.
      bulkBarrierPreWriteSrcOnly(uintptr(p), uintptr(old.array), lenmem-et.size+et.ptrdata)
    }
  }
  memmove(p, old.array, lenmem)
  return slice{p, old.len, newcap}
}

代码

  newSlice3 := append(slice3, 110,119)
  newSlice4 := append(slice4,slice3...)

拷贝

func copy(dst, src []Type) int

内建函数copy将元素从来源切片复制到目标切片中,也能将字节从字符串复制到字节切片中。copy返回被复制的元素数量,它会是 len(src) 和 len(dst) 中较小的那个。来源和目标的底层内存可以重叠。

注意:目标长度放不下时,后序的就不再拷贝了

代码

copy(slice3, slice1)

排序

func Ints(a []int)

Ints函数将a排序为递增顺序。

代码

sort.Ints(newSlice3)

插入与删除

没有,自己实现,见后面

函数传参

值传递

func byteInsert(b []byte,index int,data byte) []byte{
  //-----索引越界------
  if index<0 || index>len(b){
    return []byte{}
  }
  //------前插-------
  if index == 0{
    return append([]byte{data}, b...)
  }
  //------尾插-------
  if index == len(b){
    return append(b, data)
  }
  //------中间插------
  tmp := append(b[:index],data)
  return append(tmp,b[index:]...)
}

使用

    //-----值传递------
    hello := []byte("el")
    hello = byteInsert(hello,0,'h')
  fmt.Printf("hello:%c\n",hello)
    hello = byteInsert(hello,2,'l')
  fmt.Printf("hello:%c\n",hello)
    hello = byteInsert(hello,4,'o')
  fmt.Printf("hello:%c\n",hello)

引用传递

func byteDelete(b *[]byte,index int){
  //-----索引越界------
  if index<0 || index>=len(*b){
    return
  }
  //------前删-------
  if index == 0{
    *b = (*b)[1:]
    return
  }
  //------尾删-------
  if index == len(*b)-1{
    *b = (*b)[:len(*b)-1]
    return
  }
  //------中间删------
  *b = append((*b)[0:index],(*b)[index+1:]...)
  return
}

使用

  world := []byte("world")
  byteDelete(&world,0)
  fmt.Printf("world:%c\n",world)
  byteDelete(&world,3)
  fmt.Printf("world:%c\n",world)
  byteDelete(&world,1)
  fmt.Printf("world:%c\n",world)

注意事项

  • 引用数组,左闭右开
  • append新的数组给切片,字符串可添加到[]byte切片
  • copy不扩容,切片满了为止

全部代码

package main
import (
  "fmt"
  "sort"
)
func byteInsert(b []byte,index int,data byte) []byte{
  //-----索引越界------
  if index<0 || index>len(b){
    return []byte{}
  }
  //------前插-------
  if index == 0{
    return append([]byte{data}, b...)
  }
  //------尾插-------
  if index == len(b){
    return append(b, data)
  }
  //------中间插------
  tmp := append(b[:index],data)
  return append(tmp,b[index:]...)
}
func byteDelete(b *[]byte,index int){
  //-----索引越界------
  if index<0 || index>=len(*b){
    return
  }
  //------前删-------
  if index == 0{
    *b = (*b)[1:]
    return
  }
  //------尾删-------
  if index == len(*b)-1{
    *b = (*b)[:len(*b)-1]
    return
  }
  //------中间删------
  *b = append((*b)[0:index],(*b)[index+1:]...)
  return
}
func main() {
  //-----------------------声明--------------------
  var slice []int
  slice1 := make([]int,5)
  fmt.Println("slice slice1:",slice,slice1)
  //--------------------声明并初始化-----------------
  //---------一般形式---------
  slice2 := []int{1,2,3,4}
  //---------从数组切片--------
  arr := [5]int{5,6,7,8,9}
  slice3 := arr[1:]
  //---------从切片切片-------
  slice4 := slice3[1:]
  fmt.Println("slice2 slice3 slice4:",slice2,slice3,slice4)
  //--------------------遍历-----------------------
  //-----for-----
  fmt.Println("slice3:")
  for i:=0;i<len(slice3);i++{
    fmt.Print(slice3[i]," ")
  }
  fmt.Println()
  //-----for range----
  fmt.Println("slice3:")
  for _,v := range slice3{
    fmt.Print(v," ")
  }
  fmt.Println()
  //----------------内存---------------------------
  fmt.Printf("&slice1:%p,&slice1[0]:%v\n", &slice1, &slice1[0])
  fmt.Printf("&arr:%p &arr[1]:%v &slice3:%p &slice3[0]:%v\n",&arr,&arr[1],&slice3,&slice3[0])
  fmt.Printf("&slice4:%p &slice4[0]:%v\n",&slice4,&slice4[0])
  arr[2] = 99
  fmt.Println("slice3[1] slice4[0]",slice3[1],slice4[0])
  //-----------------函数-------------------------
  //-------长度和容量----------
  fmt.Println("len(slice3) cap(slice3) len(slice4) cap(slice4):", len(slice3), cap(slice3), len(slice4), cap(slice4))
  //-------添加和拷贝------------
  newSlice3 := append(slice3, 110,119)
  newSlice4 := append(slice4,slice3...)
  copy(slice3, slice1)
  fmt.Println("slice3追加后 slice4追加slice3后 slice3拷贝slice1后:",newSlice3,newSlice4,slice3)
    //--------排序------------------
    sort.Ints(newSlice3)
  fmt.Println("升序newSlice3:",newSlice3)
  //---------------插入和删除---------------------
    //-----值传递------
    hello := []byte("el")
    hello = byteInsert(hello,0,'h')
  fmt.Printf("hello:%c\n",hello)
    hello = byteInsert(hello,2,'l')
  fmt.Printf("hello:%c\n",hello)
    hello = byteInsert(hello,4,'o')
  fmt.Printf("hello:%c\n",hello)
  //------引用传递---------
  world := []byte("world")
  byteDelete(&world,0)
  fmt.Printf("world:%c\n",world)
  byteDelete(&world,3)
  fmt.Printf("world:%c\n",world)
  byteDelete(&world,1)
  fmt.Printf("world:%c\n",world)
}

结果截图

2020062310470442.png

参考

Go标准库-内建函数

Go标准库-sort

更多Go相关内容:Go-Golang学习总结笔记

有问题请下方评论,转载请注明出处,并附有原文链接,谢谢!如有侵权,请及时联系。

相关文章
|
存储 JSON 安全
Go语言切片,使用技巧与避坑指南
Go语言中的切片(Slice)是动态引用数组的高效数据结构,支持扩容与截取。本文从切片基础、常用操作到高级技巧全面解析,涵盖创建方式、`append`扩容机制、共享陷阱及安全复制等内容。通过代码示例详解切片特性,如预分配优化性能、区分`nil`与空切片、处理多维切片等。掌握这些核心知识点,可编写更高效的Go代码。
581 2
|
弹性计算 定位技术 数据中心
阿里云服务器配置选择方法:付费类型、地域及CPU内存配置全解析
阿里云服务器怎么选?2025最新指南:就近选择地域,降低延迟;长期使用选包年包月,短期灵活选按量付费;企业选2核4G5M仅199元/年,个人选2核2G3M低至99元/年,高性价比爆款推荐,轻松上云。
1457 11
|
Java 编译器 Go
【Golang】(5)Go基础的进阶知识!带你认识迭代器与类型以及声明并使用接口与泛型!
好烦好烦好烦!你是否还在为弄不懂Go中的泛型和接口而烦恼?是否还在苦恼思考迭代器的运行方式和意义?本篇文章将带你了解Go的接口与泛型,还有迭代器的使用,附送类型断言的解释
585 3
|
测试技术 Go API
Go 切片导致 rand.Shuffle 产生重复数据的原因与解决方案
在 Go 语言开发中,使用切片时由于其底层数据共享特性,可能会引发意想不到的 Bug。本文分析了 `rand.Shuffle` 后切片数据重复的问题,指出原因在于切片是引用类型,直接赋值会导致底层数组共享,进而影响原始数据。解决方案是使用 `append` 进行数据拷贝,确保独立副本,避免 `rand.Shuffle` 影响原始数据。总结强调了切片作为引用类型的特性及正确处理方法,确保代码稳定性和正确性。
493 82
|
数据采集 机器学习/深度学习 存储
Go语言实战案例 - 找出切片中的最大值与最小值
本案例通过实现查找整数切片中的最大值与最小值,帮助初学者掌握遍历、比较和错误处理技巧,内容涵盖算法基础、应用场景及完整代码示例,适合初学者提升编程能力。
|
存储 JSON Go
Go语言之空接口与类型断言
本文介绍了 Go 语言中空接口(`interface{}`)和类型断言的核心概念及其应用。空接口可存储任意类型数据,适用于通用函数、动态数据结构与 JSON 解析等场景;类型断言用于将接口变量还原为具体类型,推荐使用带 `ok` 的写法以避免程序崩溃。此外,文章通过示例讲解了 `type switch` 类型判断与 JSON 处理技巧,并总结了空接口的注意事项,强调滥用可能导致类型安全性降低。内容深入浅出,帮助开发者灵活运用这些特性。
475 15
|
算法 Go
Go语言模拟集合类型-《Go语言实战指南》
在 Go 语言中,虽然没有内建的集合(Set)类型,但可以通过 `map` 实现其功能。常用方式包括 `map[T]bool` 和更节省内存的 `map[T]struct{}`。前者以布尔值表示元素存在性,后者利用零内存开销的空结构体。文章介绍了集合的基本操作(添加、删除、判断、遍历),并通过封装示例展示如何创建自定义 Set 类型。这种实现方式适用于去重、唯一标记及集合运算等场景,简洁高效且易于扩展。
|
存储 安全 Go
Map的遍历与判断键是否存在-《Go语言实战指南》
本文介绍了 Go 语言中对 `map` 的常见操作,包括遍历所有项和判断键是否存在。通过 `for range` 可以遍历 `map` 的键值对、仅键或仅值(需忽略键)。注意,`map` 遍历顺序是随机的。判断键是否存在时,使用双赋值语法 `value, ok := map[key]`,其中 `ok` 表示键是否存在。直接访问不存在的键会返回类型的零值,可能导致逻辑错误。掌握这些机制可更安全高效地处理键值对数据。
|
安全 Go 开发者
Go语言之切片的原理与用法 - 《Go语言实战指南》
切片(slice)是Go语言中用于处理变长数据集合的核心结构,基于数组的轻量级抽象,具有灵活高效的特点。切片本质是一个三元组:指向底层数组的指针、长度(len)和容量(cap)。本文详细介绍了切片的声明与初始化方式、基本操作(如访问、修改、遍历)、长度与容量的区别、自动扩容机制、共享与副本处理、引用类型特性以及常见陷阱。通过理解切片的底层原理,开发者可以更高效地使用这一数据结构,优化代码性能。
585 13
|
Go 索引
Go语言中使用切片需要注意什么?
本文详细讲解了Go语言中切片(Slice)的使用方法与注意事项。切片是对数组连续片段的引用,具有灵活的操作方式。文章从定义与初始化、长度与容量、自动扩容、共享底层数组、复制、边界检查、零值到拼接等方面展开,并配以示例代码演示。通过学习,读者可深入了解切片的工作原理及优化技巧,避免常见陷阱,提升编程效率与代码质量。
387 2