Go语言现代web开发06 数组和切片

Arrays are complex data types that can be defined as a collection of elements of the same type. Individual elements can be referenced by an index that corresponds to the position of the element in the array. The first element of the array will have the index 0.

数组是复杂的数据类型,可以定义为具有相同类型的元素的集合。单个元素可以通过对应于元素在数组中的位置的索引来引用。数组的第一个元素将具有索引0。

In order to declare an array, we must provide the array length (between square brackets) and the type of element. This is how we declare an array of six integers:

为了声明数组,必须提供数组长度(在方括号之间)和元素类型。下面是声明一个包含六个整数的数组的方法:

var a [6]int

Elements of the array can be initialized. Without initialization, default values will be assigned to elements (in our case, all six elements of the array will be initialized with the value 0). The next example will create and initialize an array of six integers.

数组的元素可以初始化。在没有初始化的情况下,将给元素分配默认值(在本例中,数组的所有六个元素都将初始化为值0)。

a := [6]int{1,2,3,4,5,6}

Arrays have one critical limitation. Length is a part of the array type, so the array cannot be resized. This means that we need to know the exact size of the array during the development of our program, but that is not always possible.

数组有一个关键的限制。长度是数组类型的一部分,因此不能调整数组的大小。这意味着我们需要在程序开发过程中知道数组的确切大小,但这并不总是可能的。

Fortunately, Go provides a solution in the form of slices.
幸运的是,Go提供了切片形式的解决方案。

Slices are complex data types, loosely tied with arrays. As we mentioned before, arrays have fixed sizes. In order to provide a workaround for this limitation, slices are introduced. They do not store any data, they just point to an array. If we need a larger slice, a new underlying array will be initialized. Any change to the array will affect all slices that reference it.

切片是复杂的数据类型,松散地与数组绑定在一起。正如我们前面提到的,数组有固定的大小。为了解决这个限制,我们引入了切片。它们不存储任何数据,它们只是指向一个数组。如果需要更大的切片,将初始化一个新的底层数组。对数组的任何更改都会影响所有引用它的切片。

Slice is defined with two attributes, length and capacity. Length represents the number of elements that the slice contains, while capacity represents the number of elements in the underlying array (counting from the first element of the slice). Length and capacity attributes can be obtained with len(s) and cap(s) functions, where a represents a slice.

切片由两个属性定义,长度和容量。Length表示片包含的元素数量,而capacity表示底层数组中的元素数量(从片的第一个元素开始计数)。长度和容量属性可以通过len(s)和cap(s)函数获得,其中a表示片。

This small block of code will create a slice where the length is equal to 5 and the capacity is equal to 7.

这一小段代码将创建一个切片,其中长度等于5,容量等于7。

a := [10]int{1, 4, 9, 16, 25, 36, 49, 64, 81, 100}
s := a[3:8]

We can use the make() function for slice creation. The function receives three arguments, slice type, length, and capacity. Capacity can be omitted and in that case, the length value will also be assigned to capacity. When we use a make() function, a new array will be created, initialized with default values for the specified type, and returns a slice that refers to an array. Here is a simple example of how to create an integers slice with the make() function.

我们可以使用make()函数来创建切片。该函数接收三个参数:切片类型、长度和容量。容量可以省略,在这种情况下,长度值也将分配给容量。当我们使用make()函数时,将创建一个新的数组,用指定类型的默认值初始化,并返回一个指向数组的切片。下面是如何使用make()函数创建整数切片的简单示例。

s = make([]int, 10)

It is possible to create a slice from another slice. The new slice will reference the same array as the original slice.

可以从另一个片创建一个片。新切片将引用与原始切片相同的数组。

Slices are defined with two indexes which represent low and high bound, respectively. Element of the original array with index defined with low bound will be included in the created slice. On the other hand, the element of the original array with an index defined with a high bound will be excluded from the created slice. In the next example, a new slice will be created and will include elements with indexes from 1 to 5 of the underlying array.

切片用两个索引定义,分别表示下限和上界。原始数组中索引定义为下界的元素将包含在创建的切片中。另一方面,原始数组中索引定义为高边界的元素将从创建的切片中排除。在下一个示例中,将创建一个新片,其中将包含索引为基础数组1到5的元素。

var s []int = a[1:6]

Bounds can be omitted, although colon must be present. When some of the bounds are omitted, the default value will be used. The default value for the low bound is 0, while the default value for the high bound is equal to the length of the underlying slice. So, for the array of length 5, these slice expressions are equivalent:

边界可以省略,但必须有冒号。当省略某些边界时,将使用默认值。下界的默认值是0,而上界的默认值等于底层切片的长度。因此,对于长度为5的数组,这些切片表达式是等价的:

var s1 []int = a[0:5]
var s2 []int = a[:5]
var s3 []int = a[0:]
var s4 []int = a[]

We can initialize slices, similar to arrays. This expression will create an array of length 5 and a slice that references that array.

我们可以初始化切片,类似于数组。这个表达式将创建一个长度为5的数组和一个引用该数组的切片。

s := []int{1,4,9,16,25}

We can use the append() function to add elements at the end of the slice. The function receives two arguments, a slice and a list of elements to append. The append function will return a slice that contains all elements of the original slice and new values. If the underlying array is too small for all new elements, a new bigger array will be allocated. We should be very careful with the append() function in order to avoid unnecessary memory allocation. This expression will add two elements to the integer slice.

我们可以使用append()函数在切片的末尾添加元素。该函数接收两个参数,一个切片和一个要追加的元素列表。append函数将返回一个切片,其中包含原始切片的所有元素和新值。如果底层数组对于所有新元素来说太小,则会分配一个新的更大的数组。我们应该非常小心地使用append()函数,以避免不必要的内存分配。该表达式将向整型切片添加两个元素。

s = append(s, 3, 33)

It is also possiable to append one slice to another in the following way:

还可以用以下方式将一个切片附加到另一个切片:

s1 := []int{1, 11, 111}
s2 := []int{2, 22, 222}
s1 = append(s1, s2...)

The default value for slice is nil. Nil slices have no underlying array and have a length and capacity equal to zero.

slice的默认值是nil。Nil切片没有底层数组,长度和容量都等于零。

It’s important to point out that slices can contain any type, including other slices.

需要指出的是,切片可以包含任何类型,包括其他切片。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Python私教

创业不易,请打赏支持我一点吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值