golang 数组和切片_了解Go中的数组和切片

golang 数组和切片

介绍 (Introduction)

In Go, arrays and slices are data structures that consist of an ordered sequence of elements. These data collections are great to use when you want to work with many related values. They enable you to keep data together that belongs together, condense your code, and perform the same methods and operations on multiple values at once.

在Go中, 数组切片是由元素的有序序列组成的数据结构 。 当您要使用许多相关值时,这些数据收集非常有用。 它们使您可以将属于一起的数据保持在一起,压缩代码,并一次对多个值执行相同的方法和操作。

Although arrays and slices in Go are both ordered sequences of elements, there are significant differences between the two. An array in Go is a data structure that consists of an ordered sequence of elements that has its capacity defined at creation time. Once an array has allocated its size, the size can no longer be changed. A slice, on the other hand, is a variable length version of an array, providing more flexibility for developers using these data structures. Slices constitute what you would think of as arrays in other languages.

尽管Go中的数组和切片都是元素的有序序列,但是两者之间存在显着差异。 Go中的数组是一种数据结构 ,它由元素的有序序列组成,并在创建时定义了其容量。 数组分配大小后,就无法再更改大小。 另一方面, 切片是数组的可变长度版本,为使用这些数据结构的开发人员提供了更大的灵活性。 切片构成了其他语言中的数组。

Given these differences, there are specific situations when you would use one over the other. If you are new to Go, determining when to use them can be confusing: Although the versatility of slices make them a more appropriate choice in most situations, there are specific instances in which arrays can optimize the performance of your program.

鉴于这些差异,在某些情况下您会使用一种方法。 如果您不熟悉Go,那么确定何时使用它们可能会造成混淆:尽管slice的多功能性使其在大多数情况下成为更合适的选择,但在某些特定情况下,数组可以优化程序的性能。

This article will cover arrays and slices in detail, which will provide you with the necessary information to make the appropriate choice when choosing between these data types. Furthermore, you’ll review the most common ways to declare and work with both arrays and slices. The tutorial will first provide a description of arrays and how to manipulate them, followed by an explanation of slices and how they differ.

本文将详细介绍数组和切片,它们将为您提供必要的信息,以便在这些数据类型之间进行选择时做出适当的选择。 此外,您将回顾声明和使用数组和切片的最常用方法。 本教程将首先描述数组以及如何操作它们,然后解释切片以及它们之间的区别。

数组 (Arrays)

Arrays are collection data structures with a set number of elements. Because the size of an array is static, the data structure only needs to allocate memory once, as opposed to a variable length data structure that must dynamically allocate memory so that it can become larger or smaller in the future. Although the fixed length of arrays can make them somewhat rigid to work with, the one-time memory allocation can increase the speed and performance of your program. Because of this, developers typically use arrays when optimizing programs in instances where the data structure will never need a variable amount of elements.

数组是具有一定数量元素的集合数据结构。 由于数组的大小是静态的,因此数据结构只需要分配一次内存,而可变长度数据结构则必须动态分配内存,以便将来可以变大或变小。 尽管固定长度的数组使它们在使用时有些僵化,但是一次性内存分配可以提高程序的速度和性能。 因此,开发人员通常在数据结构永远不需要可变数量的元素的实例中优化程序时使用数组。

定义数组 (Defining an Array)

Arrays are defined by declaring the size of the array in brackets [ ], followed by the data type of the elements. An array in Go must have all its elements be the same data type. After the data type, you can declare the individual values of the array elements in curly brackets { }.

通过在方括号[ ]声明数组的大小,然后声明元素的数据类型来定义数组。 Go中的数组必须使其所有元素都具有相同的数据类型 。 在数据类型之后,可以在大括号{ }声明数组元素的各个值。

The following is the general schema for declaring an array:



Note: It is important to remember that every declaration of a new array creates a distinct type. So, although [2]int and [3]int both have integer elements, their differing lengths make their data types incompatible.

注意:请务必记住,新数组的每个声明都会创建一个不同的类型。 因此,尽管[2]int[3]int都具有整数元素,但是它们不同的长度使它们的数据类型不兼容。

If you do not declare the values of the array’s elements, the default is zero-valued, which means that the elements of the array will be empty. For integers, this is represented by 0, and for strings this is represented by an empty string.

如果不声明数组元素的值,则默认值为零值,这意味着数组元素将为空。 对于整数,它由0表示,对于字符串,则由空字符串表示。

For example, the following array numbers has three integer elements that do not yet have a value:


var numbers [3]int

If you printed numbers, you would receive the following output:

如果您打印numbers ,您将收到以下输出:

[0 0 0]

If you would like to assign the values of the elements when you create the array, place the values in curly brackets. An array of strings with set values looks like this:

如果要在创建数组时分配元素的值,请将其值放在大括号中。 具有设置值的字符串数组如下所示:

[4]string{"blue coral", "staghorn coral", "pillar coral", "elkhorn coral"}

You can store an array in a variable and print it out:


coral := [4]string{"blue coral", "staghorn coral", "pillar coral", "elkhorn coral"}

Running a program with the preceding lines would give you the following output:


[blue coral staghorn coral pillar coral elkhorn coral]

Notice that there is no delineation between the elements in the array when it is printed, making it difficult to tell where one element ends and another begins. Because of this, it is sometimes helpful to use the fmt.Printf function instead, which can format strings before printing them to the screen. Provide the %q verb with this command to instruct the function to put quotation marks around the values:

请注意,打印时数组中的元素之间没有描述,因此很难判断一个元素在哪里结束而另一个在哪里开始。 因此,有时改用fmt.Printf函数会有所帮助,该函数可以在将字符串打印到屏幕之前对其进行格式化。 为此命令提供%q动词,以指示函数在值周围加上引号:

fmt.Printf("%q\n", coral)

This will result in the following:


["blue coral" "staghorn coral" "pillar coral" "elkhorn coral"]

Now each item is quoted. The \n verb instructs to the formatter to add a line return at the end.

现在,每个项目都被引用。 动词\n指示格式化程序在末尾添加换行符。

With a general idea of how to declare arrays and what they consist of, you can now move on to learning how to specify elements in an array with an index number.


索引数组(和切片) (Indexing Arrays (and Slices))

Each element in an array (and also a slice) can be called individually through indexing. Each element corresponds to an index number, which is an int value starting from the index number 0 and counting up.

  • 0
  • 1
    觉得还不错? 一键收藏
  • 0


  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助




当前余额3.43前往充值 >
领取后你会自动成为博主和红包主的粉丝 规则
钱包余额 0