x:= [... string {"Sat","Sun"}与x:= [] string {"Sat","Sun"} [英] x := [...]string{"Sat", "Sun"} vs x:= []string{"Sat", "Sun"}

查看:53
本文介绍了x:= [... string {"Sat","Sun"}与x:= [] string {"Sat","Sun"}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在go lang spec 中,他们在其中一个示例中使用了三个点:

In the go lang spec they used three dots in one of the examples:

days := [...]string{"Sat", "Sun"}  // len(days) == 2

如果遗漏了三个点,有什么区别吗?

Does it make any difference if the three dots are left out?

推荐答案

它产生了很大的不同 :区别在于 array slice之间.

It makes a pretty big difference: The difference is between an array and a slice.

[] string 创建一个指向字符串数组的切片.另一方面, [...] 创建一个 actual 字符串数组.

[]string creates a slice that points to an array of strings. On the other hand, [...] creates an actual array of strings.

关于golang博客上两者之间的区别,有一个很棒的博客文章.我会尽力在这里进行总结.

There is a great blog post about the difference between the two on the golang blog. I'll try to summarize here as best I can.

golang中的数组就像值类型,它们是对特定类型的引用,并且始终具有特定的长度.创建数组有两种方法:1)具有显式长度和2)隐式长度:

Arrays in golang are like value types, they are references to a specific type and are always of a specific length. There are two ways to create an array: 1) with explicit length and 2) implicit length:

// Explicit length. 
var days := [7]string { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }

// Implicit length. (Let the compiler figure it out how long it is)
var days := [...]string { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" } 

这两个都是等效的数组定义.请注意,数组的长度是其类型定义的 part .因此,您不能互换具有不同长度的相似类型的数组:

These are both equivalent array definitions. Note that the length of an array is part of it's type definition. So, you can't interchange arrays of similar type with different lengths:

// These two are not interchangeable!
var someArray [5]string;
var otherArray [10]string;

func work(data [5]string) { /* ... */ }

work(someArray)  // good
work(otherArray) // not so good

还请注意,数组(如structs)以值–的形式传递.数组的副本将提供给函数,而不是对其的引用.

Also note that arrays, like structs, are passed as value – a copy of the array will be given to a function, not a reference to it..

切片,另一方面,类似于引用类型.它们有阵列支持,但更具延展性.它们包括一个指向数组内位置,长度和容量的指针.

Slices, on the other hand, are like reference types. They are backed by an array, but they are more malleable. They include a pointer to a position within array, a length, and a capacity.

// Create a slice
var days := []string { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }

切片与数组不同,没有明确地绑定到其长度,并且不同长度的切片可以相互传递.它们也更像指针,这意味着它们是通过引用而不是值传递的.

Slices, unlike arrays, are not explicitly bound to their length and slices of different lengths can be passed for one another. They also act more like a pointer, which means they get passed by reference, instead of value.

关于转到数据结构,以及它们如何在内存中表示的文章也很棒.我强烈建议您检查一下.

There is also a great post about Go Data Structures, and how they are they are represented in memory. I highly recommend checking it out.

这篇关于x:= [... string {"Sat","Sun"}与x:= [] string {"Sat","Sun"}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆