开发者

Preferred declare / initialize method

开发者 https://www.devze.com 2023-01-20 19:50 出处:网络
I\'m currently playing with Google Go. There\'s开发者_StackOverflow中文版 a lot of ways to declare and/or initialize variables.

I'm currently playing with Google Go.

There's开发者_StackOverflow中文版 a lot of ways to declare and/or initialize variables.

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 ?

Thanks, SO folks !


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.


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)
0

精彩评论

暂无评论...
验证码 换一张
取 消