首选的声明/初始化方法

huangapple go评论93阅读模式
英文:

Preferred declare / initialize method

问题

我目前正在使用Google Go进行编程。有很多方法可以声明和/或初始化变量。有人可以解释一下每种方法的优缺点吗(根据我所知的样本,如下):

    var strArr0 *[10]string = new([10]string)

	var strArr1 = new([10]string)

	var strArr2 = make([]string,10)

	var strArr3 [10]string

	strArr4 := make([]string,10)

你更喜欢哪种语法,为什么呢?谢谢,SO的朋友们!

英文:

I'm currently playing with Google Go.<br/>
There's a lot of ways to declare and/or initialize variables.<br/>
Can someone explains pros/cons of each way (samples, as far as i know, below) :

    var strArr0 *[10]string = new([10]string)

	var strArr1 = new([10]string)

	var strArr2 = make([]string,10)

	var strArr3 [10]string

	strArr4 := make([]string,10)

What is your preferred syntax, and why ?<br/>
Thanks, SO folks !

答案1

得分: 7

我已经给你的示例编号为1-5,并在这里逐个解释。希望这能帮到你!

var strArr0 *[10]string = new([10]string)  // (1)

这将分配一个新的字符串数组,长度为10,并返回数组的指针。

var strArr1 = new([10]string)  // (2)

这与1完全相同。这只是一种简写方式,由于Go的类型推断而起作用。它还可以进一步简化为:

strArr1 := new([10]string)  // (2a)

其中1、2、2a都会产生完全相同的结果。

var strArr2 = make([]string,10)  // (3)

这将创建一个长度为10的字符串切片。切片是指底层数组的某个子集。来自golang规范

> make()调用会分配一个新的、隐藏的数组,返回的切片值引用该数组。
>
> > make([]T, length, capacity)
>
> 会产生与分配数组并对其进行切片相同的切片,因此这两个示例会产生相同的切片:
>
> > make([]int, 50, 100)
> > new([100]int)[0:50]

因此,3等同于以下任何一种:

var strArr2 = new([10]string)[0:10]  // 切片一个显式创建的数组
var strArr2 []string = new([10]string)[0:10]  // 显式声明strArr2的类型,而不是推断
strArr2 := new([10]string)[0:10]  // 使用:=简写而不是var

new或make的使用取决于您要创建的类型。make仅用于切片、映射和通道。它们使用不同的关键字来传达make正在初始化某些数据结构的概念,而不仅仅是将内存清零。

下一个示例回到了数组而不是切片:

var strArr3 [10]string  // (4)

这与1、2和2a相同。

strArr4 := make([]string,10)  // (5)

与3相同。:=只是在初始化变量时的简写形式,类型可以被推断出来。


那么应该选择哪个呢?这在一定程度上取决于个人的风格,但通常有一个选择显然会最大化代码的清晰度,例如在类型明显时使用类型推断:

foo := bar.ToStringArray()

或者在类型不太明显且类型对于查看很有用时声明类型:

var foo []string = bar.DoSomethingOpaque()

在切片与数组之间,通常会根据你调用的某个函数所需的类型来创建。

英文:

I've numbered your examples 1-5, and I'll walk through them here. Hope this helps!

var strArr0 *[10]string = new([10]string)  // (1)

This allocates a new array of strings, of length 10, and returns a pointer to the array.

var strArr1 = new([10]string)  // (2)

This is exactly the same as 1. It's just shorthand, that works due to Go's type inference. It could be shortened further to:

strArr1 := new([10]string)  // (2a)

Where 1, 2, 2a all produce exactly the same result.

var strArr2 = make([]string,10)  // (3)

This makes a slice of strings, with length 10. A slice refers to some subset of an underlying array. From the golang spec:

> The make() call allocates a new,
> hidden array to which the returned
> slice value refers.
>
> > make([]T, length, capacity)
>
> produces the same slice as allocating an array and slicing it,
> so these two examples result in the same slice:
>
> > make([]int, 50, 100)
> > new([100]int)[0:50]

So 3 is equivalent to any of the following:

var strArr2 = new([10]string)[0:10]  // Slicing an explicitly created array
var strArr2 []string = new([10]string)[0:10]  // Explicitly declaring the type of strArr2, rather than inferring
strArr2 := new([10]string)[0:10]  // Using the := shorthand instead of var

new or make are used depending on what type you're creating. make is used solely for slices, maps and channels. They used a different keyword to convey the idea that make is initializing some data structure under the hood, rather than just zeroing memory.

The next one is back to arrays rather than slices:

var strArr3 [10]string  // (4)

This is the same as 1, 2, and 2a.

strArr4 := make([]string,10)  // (5)

Same as 3. := is just shorthand when initializing a variable and the type can be inferred.


So which to choose? This is up to your personal style a bit, but in general one choice is obviously going to maximise the clarity of your code e.g. using type inference when the type is obvious:

foo := bar.ToStringArray()

or declaring the types when it's less so and the type will be useful to see:

var foo []string = bar.DoSomethingOpaque()

On slices versus arrays, you'll usually create whatever type is required for some function you're calling.

答案2

得分: 2

首先,你应该理解数组和切片之间的区别。数组被视为值,所以如果你将它们传递给函数,它们将被复制。而切片则是对底层数组的引用,所以如果你将切片传递给函数,函数变量仍将引用相同的底层数组。

在你的示例中,前两个是相同的,只是第一个在变量声明中指定了类型。Go允许你这样做,这在某些情况下可能有帮助 - 比如当你尝试声明一个变量为接口而不是值时(例如 var i io.Reader = os.Open(...))。

第三个和第五个(var strArr2 = make([]string,10)strArr4 := make([]string,10))是等价的,但后者使用了变量声明的简写形式,这样通常可以节省很多打字。

第四个只是声明了一个数组值。

除非我在处理网络数据,否则我很少在我的代码中处理数组。所以我更喜欢的语法是:

s := make([]string, 10)
英文:

First of all you should understand the distinction between arrays and slices. Arrays are treated as values, so if you pass them to functions they will get copied. Slices, on the other hand, are references to the underlying array, so if you pass a slice to a function, the function variable will still refer to the same underlying array.

In your examples, the first two are identical, it's just the first specifies the type in the variable declaration. Go let's you do that, and it may help in some cases -- like when you're trying to declare a variable to be an interface instead of a value (i.e var i io.Reader = os.Open(...)).

The third and the fifth (var strArr2 = make([]string,10), strArr4 := make([]string,10) are equivalent, but the latter uses the short-hand form of variable declaration, which can often save a lot of typing.

The fourth just declares an array value.

I rarely deal with arrays in my code unless I'm working with network data. So my preferred syntax is:

s := make([]string, 10)

huangapple
  • 本文由 发表于 2010年10月18日 23:29:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/3960685.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定