英文:
Why do I need to pass the type for one param and not for another if they're the same type?
问题
这里是游乐场链接:https://play.golang.org/p/uiW2j6Zlj1d
在这个特定的部分中:
checkSums := func(t testing.TB, got, want []int) {
t.Helper()
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v want %v", got, want)
}
}
为什么我需要将类型[]int
传递给want
参数,但将相同类型传递给got
参数是可选的?
英文:
Here's the playground link: https://play.golang.org/p/uiW2j6Zlj1d
In this specific part here:
checkSums := func(t testing.TB, got, want []int) {
t.Helper()
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v want %v", got, want)
}
}
Why do I need to pass the type [] int
to the want
parameter, but passing this very same type to the got
parameter is optional?
答案1
得分: 2
这是语言规范中给出的内容:
在函数声明中,规范定义了:
FunctionDecl = "func" FunctionName Signature [ FunctionBody ] .
然后,签名包括参数声明:
ParameterDecl = [ IdentifierList ] [ "..." ] Type .
最后,标识符列表在常量声明下:
IdentifierList = identifier { "," identifier } .
因此,这等于一个逗号分隔的标识符列表,可选地包含省略号 ...
,最后是类型:
ParameterDecl = [ identifier { "," identifier } ] [ "..." ] Type .
英文:
This is given by the language specifications:.
In Function Declarations the specs define:
FunctionDecl = "func" FunctionName Signature [ FunctionBody ] .
The signature then includes the parameter declaration:
ParameterDecl = [ IdentifierList ] [ "..." ] Type .
And finally the identifier list is under Constant declarations:
IdentifierList = identifier { "," identifier } .
Therefore, this equals to a comma-separated list of identifiers, optionally the vararg token ...
and finally the type:
ParameterDecl = [ identifier { "," identifier } ] [ "..." ] Type .
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论