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:

以下是声明数组的一般模式:

[capacity]data_type{element_values}

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:

例如,以下数组numbers具有三个没有值的整数元素:

var numbers [3]int

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

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


   
   
   
Output
[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"}
fmt.Println(coral)

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

使用前几行运行程序将为您提供以下输出:


   
   
   
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:

这将导致以下结果:


   
   
   
Output
["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
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值