英文:
What is the advantage of golang for defining the array int[x][y][z] for [z][y][x]int?
问题
在Go语言中,数组定义的语法与C++/C#/Java等其他语言有所不同。Go语言的数组定义语法如下:
var arr [Z][Y][X]int32
Go语言的数组定义语法更加简洁和直观。它将数组的维度信息放在类型的右侧,而不是左侧。这种语法的优势在于:
-
代码可读性更高:将维度信息放在类型的右侧,使得代码更加清晰和易读。开发者可以更快地理解数组的维度和类型。
-
一致性:Go语言的数组定义语法在多维数组和一维数组之间保持了一致性。无论是多维数组还是一维数组,都使用相同的语法进行定义,这样可以减少语法的复杂性和混淆。
-
更好的类型推断:Go语言的编译器可以根据数组的初始化值自动推断数组的维度,从而减少了开发者的工作量。开发者只需要提供初始化值,而不需要显式地指定数组的维度。
总的来说,Go语言的数组定义语法简洁、直观,并且具有良好的可读性和一致性,使得开发者能够更加轻松地理解和使用数组。
英文:
I just start to learn some basic of golang
, it is little strange for me when looking at the array define syntax.
C++/C#/Java all defines multi-dimensional arrays like:
int arr[X][Y][Z]; // C/C++
int[,,] arr = new int[X, Y, Z]; // C#
int[][][] multi = new int[X][Y][Z]; // Java
And in go:
var arr [Z][Y][X]int32 // go
what is the advantage about the syntax?
答案1
得分: 7
C#和Java的语法都受到了C语言的启发,并继承了C语言语法的所有怪癖。关于为什么Go语言有不同的语法,有一篇很好的文章可以参考:Go语言的声明语法:
Go语法
C语言家族之外的语言通常在声明中使用不同的类型语法。虽然这是一个独立的观点,但通常名称在前,后面跟着一个冒号。因此,我们上面的示例在一个虚构但说明性的语言中会变成类似下面这样:
x: int p: pointer to int a: array[3] of int
这些声明很清晰,但冗长——你只需要从左到右阅读它们。Go语言从这里汲取灵感,但为了简洁起见,它省略了冒号并去掉了一些关键字:
x int p *int a [3]int
[3]int
的外观与如何在表达式中使用a
没有直接对应关系。(我们将在下一节讨论指针。)你以牺牲一种独立的语法为代价获得了清晰度。
英文:
The Syntaxes of C# and Java were both inspired by C and inherit all the quirks of the C syntax. There is a nice article on why Go has a different syntax:
> ### Go syntax
> Languages outside the C family usually use a distinct type syntax in
> declarations. Although it's a separate point, the name usually comes
> first, often followed by a colon. Thus our examples above become
> something like (in a fictional but illustrative language)
>
> x: int
> p: pointer to int
> a: array3 of int
>
> These declarations are clear, if verbose - you just read them left to
> right. Go takes its cue from here, but in the interests of brevity it
> drops the colon and removes some of the keywords:
>
> x int
> p *int
> a 3int
>
> There is no direct correspondence between the look of [3]int
and how
> to use a in an expression. (We'll come back to pointers in the next
> section.) You gain clarity at the cost of a separate syntax.
答案2
得分: 2
这种类型的变量声明(var
)的另一个方面是它被初始化为**零值**。
这种初始化是递归进行的,所以例如,如果没有指定值,结构体数组的每个元素的字段都将被清零。
在C语言中,变量的声明类似于表示其类型的表达式,这是一个不错的想法,但类型和表达式的语法结合得不太好,结果可能会令人困惑;考虑函数指针。
Go语言主要将表达式和类型语法分开,这简化了事情(使用前缀*表示指针是一个例外,它证明了这个规则)。
英文:
Another aspect of this kind of variable declaration (var
) is that it is initialized to a zero value
> This initialization is done recursively, so for instance each element of an array of structs will have its fields zeroed if no value is specified.
The declaration is "backward" (coming from C):
> In C, the notion is that a variable is declared like an expression denoting its type, which is a nice idea, but the type and expression grammars don't mix very well and the results can be confusing; consider function pointers.
>
> Go mostly separates expression and type syntax and that simplifies things (using prefix * for pointers is an exception that proves the rule)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论