golang 之 slice

Slice types

A slice is a descriptor for a contiguous segment of an underlying array and provides access to a numbered sequence of elements from that array. A slice type denotes the set of all slices of arrays of its element type. The value of an uninitialized slice is nil.

SliceType = "[" "]" ElementType .

Like arrays, slices are indexable and have a length. The length of a slice s can be discovered by the built-in function len; unlike with arrays it may change during execution. The elements can be addressed by integer indices 0 through len(s)-1. The slice index of a given element may be less than the index of the same element in the underlying array.

A slice, once initialized, is always associated with an underlying array that holds its elements. A slice therefore shares storage with its array and with other slices of the same array; by contrast, distinct arrays always represent distinct storage.

The array underlying a slice may extend past the end of the slice. The capacity is a measure of that extent: it is the sum of the length of the slice and the length of the array beyond the slice; a slice of length up to that capacity can be created by slicing a new one from the original slice. The capacity of a slice a can be discovered using the built-in function cap(a).

A new, initialized slice value for a given element type T is made using the built-in function make, which takes a slice type and parameters specifying the length and optionally the capacity. A slice created with make always allocates a new, hidden array to which the returned slice value refers. That is, executing

make([]T, length, capacity)

produces the same slice as allocating an array and slicing it, so these two expressions are equivalent:

make([]int, 50, 100)
new([100]int)[0:50]

Like arrays, slices are always one-dimensional but may be composed to construct higher-dimensional objects. With arrays of arrays, the inner arrays are, by construction, always the same length; however with slices of slices (or arrays of slices), the inner lengths may vary dynamically. Moreover, the inner slices must be initialized individually.

时间: 2024-07-29 16:49:08

golang 之 slice的相关文章

golang的slice作为函数参数传值的坑

直接贴代码 func sliceModify(slice []int) { // slice[0] = 88 slice = append(slice, 6) } func main() { slice := []int{1, 2, 3, 4, 5} sliceModify(slice) fmt.Println(slice) } 返回的没变,坑爹的,这个设计太那啥了,可以正确跑出效果的版本如下: func sliceModify(slice *[]int) { *slice = append(*

golang的slice了解及验证

golang 中slice的操作 golang收获 如果只是分配了 var st []SelfType, 进行赋值,那么就会是吧,除非使用 make 分配一个内存空间 注意, slice在make的时候可以制定 len, cap长度, len,cap的区别在于: len是当前slice中的element的长度, cap是slice的长度(在自动扩展之后) slice在使用的时候最好要区分出数据量的大小是否可以预期,如果是数据量很大,且不确认会有多少的时候,使用append的时候会浪费时间在内存c

Golang:slice之append时原数组发生变化的问题

使用append可以在slice之后追求元素,例如 nums:=[]int{1,2,3} result:=append(nums,4) fmt.Println(result) 这段代码很简单,输出result的值为:[1 2 3 4] 问题在于,进行这种操作时,原来的slice(即nums)所基于的数组的值会不会发生变化呢?在Golang中,如果有多个slice基于了同一个数组,则这些slice的数据是共享的(而不是每个slice复制一份).也就说,如果改变了数组的内容,则基于它的所有slice

golang array, slice, string笔记

本来想写一篇关于golang io的笔记,但是在学习io之前必须了解array, slice, string概念,因此将在下篇写golang io. array: 数组的长度是该数组类型的一部分,例如var buffer [256]byte 的类型是[256]byte, len(buffer)总是返回256. slice: 一个slice描述一个数组中连续的一部分,例如var slice = buffer[100:250].slice也可以从slice产生,如var slice2 = slice

Golang Clearing slice

//first method : slice = nil // second method : slice = slice[0:0] Source page : https://www.socketloop.com/tutorials/golang-clearing-slice

golang切片slice

切片slice是引用类型 len()函数获取元素的个数 cap()获取数组的容量 1.申明方式 (1)var a []int 与数组不同的是他不申明长度(2)s2 := make([]int, 3, 10) //元素的类型,元素的数量,元素的容量  fmt.Println(len(s2), cap(s2)) 输出元素的数量和容量 2.讲数组转换成切片 2 a := [10]int{} 3 fmt.Println(a) 4 s1 := a[:10] //取前10个元素 [5:]取 5-最后的元素

使用golang的slice来模拟栈

slice(切片):底层数据结构是数组 stack(栈):一种先进后出的数据结构 普通版的模拟写入和读取的栈 package main import "fmt" //栈的特点是先进后出 //使用一个切片的全局变量来模拟栈 var stack []int //向栈中添加数据 func push(value int) { stack = append(stack, value) } //从栈中获取数据 func pop() (int, bool) { ok := false value :

golang中slice处理遇到的一个关于引用的坑

前两天在解扫地机器人算法的问题时,遇到一个坑 部分代码如下: func move2(startPoint Point) [][]Point { allFootPrint := [][]Point{{startPoint}} for i := 0; i < N; i++ { allNewFootPrint := make([][]Point, 0) for len(allFootPrint) > 0 { curFootPrint := allFootPrint[len(allFootPrint)

Golang后台开发初体验

转自:http://blog.csdn.net/cszhouwei/article/details/37740277 补充反馈 slice 既然聊到slice,就不得不提它的近亲array,这里不太想提值类型和引用类型的概念(个人觉得其实都是值类型),golang的array其实可以假想为C的struct类型,只是struct通过变量名来访问成员(如xxx.yyy),而array通过下标来访问成员(如xxx[3]),具体内存布局如下图所示: 图 1 golang的array内存布局 显然gola