Why do I need to pass the type for one param and not for another if they're the same type?

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

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 .

huangapple
  • 本文由 发表于 2021年7月14日 14:11:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/68372888.html
匿名

发表评论

匿名网友

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

确定