Go 在func中定义本地类型和方法?

Go 在func中定义本地类型和方法?,go,testing,types,interface,local,Go,Testing,Types,Interface,Local,我的代码使用带有一个函数的接口: func InsertRow(rec []string) error 此接口的不同实现有不同的类型。现在我想用“go test”来测试这个。在这种情况下,InsertRow的实现不应执行任何操作: func (t TestInserter) InsertRow(rec []string) error { return nil } 我可以在测试函数中定义内部类型。但现在我还想为这种类型定义一个虚拟方法: func TestInserter01(t *tes

我的代码使用带有一个函数的接口:

func InsertRow(rec []string) error
此接口的不同实现有不同的类型。现在我想用“go test”来测试这个。在这种情况下,InsertRow的实现不应执行任何操作:

func (t TestInserter) InsertRow(rec []string) error {
  return nil
}
我可以在测试函数中定义内部类型。但现在我还想为这种类型定义一个虚拟方法:

func TestInserter01(t *testing.T) {
  type TestMyInserter struct {} <-- Test type 

  func (t TestMyInserter) InsertRow(rec []string) error { <-- Dummy function on this type.
    return nil
  }

  ... using InsertRow as a parameter in another function ...
}
如果我在测试函数之外定义类型和方法,那么相同的代码也可以工作。
是否可以在测试函数中隐藏测试实现,而不在函数外部定义它?我需要很多,因此我更希望在测试函数中本地定义它们。

不,这是不可能的。方法声明只能在顶层(在任何函数之外)

见相关文件:

但是请注意,可以提供带有帮助器类型的“动态”实现。这意味着您将在函数中提供方法实现,并且在实现接口的助手类型的帮助下,您可以获得“动态”实现

例如:

type Inserter interface {
    InsertRow(rec []string) error
}

type helper func(rec []string) error

func (h helper) InsertRow(rec []string) error {
    return h(rec)
}

func main() {
    testInsert := func(rec []string) error {
        return fmt.Errorf("rec: %v", rec)
    }

    var i Inserter = helper(testInsert)

    err := i.InsertRow([]string{"one", "two"})
    fmt.Println(err)
}
这将输出(在上尝试):

变量可以是包含方法的函数类型字段的结构。它可用于涵盖多种方法:

type helper struct {
    insertRow func(rec []string) error
}

func (h helper) InsertRow(rec []string) error {
    return h.insertRow(rec)
}

func main() {
    h := helper{
        insertRow: func(rec []string) error {
            return fmt.Errorf("rec: %v", rec)
        },
    }

    var i Inserter = h

    err := i.InsertRow([]string{"one", "two"})
    fmt.Println(err)
}

这输出相同的结果。在电脑上试试。

不,不可能。方法声明只能在顶层(在任何函数之外)

见相关文件:

但是请注意,可以提供带有帮助器类型的“动态”实现。这意味着您将在函数中提供方法实现,并且在实现接口的助手类型的帮助下,您可以获得“动态”实现

例如:

type Inserter interface {
    InsertRow(rec []string) error
}

type helper func(rec []string) error

func (h helper) InsertRow(rec []string) error {
    return h(rec)
}

func main() {
    testInsert := func(rec []string) error {
        return fmt.Errorf("rec: %v", rec)
    }

    var i Inserter = helper(testInsert)

    err := i.InsertRow([]string{"one", "two"})
    fmt.Println(err)
}
这将输出(在上尝试):

变量可以是包含方法的函数类型字段的结构。它可用于涵盖多种方法:

type helper struct {
    insertRow func(rec []string) error
}

func (h helper) InsertRow(rec []string) error {
    return h.insertRow(rec)
}

func main() {
    h := helper{
        insertRow: func(rec []string) error {
            return fmt.Errorf("rec: %v", rec)
        },
    }

    var i Inserter = h

    err := i.InsertRow([]string{"one", "two"})
    fmt.Println(err)
}
这输出相同的结果。试穿一下

type helper struct {
    insertRow func(rec []string) error
}

func (h helper) InsertRow(rec []string) error {
    return h.insertRow(rec)
}

func main() {
    h := helper{
        insertRow: func(rec []string) error {
            return fmt.Errorf("rec: %v", rec)
        },
    }

    var i Inserter = h

    err := i.InsertRow([]string{"one", "two"})
    fmt.Println(err)
}