【问题标题】:Why do I need to pass the type for one param and not for another if they're the same type?如果它们是相同的类型,为什么我需要传递一个参数的类型而不是另一个参数?
【发布时间】:2021-09-23 03:02:17
【问题描述】:

这里是操场链接: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 参数是可选的?

【问题讨论】:

标签: unit-testing go


【解决方案1】:

这是由语言规范给出的:。

Function Declarations 规范中定义:

FunctionDecl = "func" FunctionName Signature [ FunctionBody ] .

然后签名包含参数声明:

ParameterDecl  = [ IdentifierList ] [ "..." ] Type .

最后标识符列表在Constant declarations下:

IdentifierList = identifier { "," identifier } .

因此,这等于以逗号分隔的标识符列表,可选可变参数标记 ...,最后是类型:

ParameterDecl  = [ identifier { "," identifier } ] [ "..." ] Type .

【讨论】:

    猜你喜欢
    • 2017-10-06
    • 2020-08-11
    • 1970-01-01
    • 2022-12-20
    • 1970-01-01
    • 2014-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多