如何在Golang中将泛型类型的数组作为函数参数传递

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

How to pass generic type of array as the parameter for the function in Golang

问题

我是你的中文翻译助手,以下是你要翻译的内容:

我对Golang还不熟悉,我的问题是如何使函数接受泛型类型的参数。以下是我最近遇到的具体问题:

我定义了一个包含函数的结构体:

type TestArgs struct {
	name string
    ...
	customAssertForTypeA func(array []*pb.TypeA)
    customAssertForTypeB func(array []*pb.TypeB)
}

// 使用TestArgs定义测试用例。
tests := []TestArgs{
		{
			name: "Test A",
			customAssertForTypeA: func(array []*pb.TypeA) {
				// 一些代码
			}
		},
		{
			name: "Test B",
			customAssertForTypeB: func(array []*pb.TypeB) {
				// 一些代码
			}
		},
}

我的问题是如何使customAssert函数接受泛型类型的参数?我看到一些类似的问题有解决方案是使用interface{},所以我尝试了以下代码:

customAssert func(array interface{})

但是它没有起作用。

英文:

I'm new to Golang, My question is how to make function accept generic type of parameter. Here is the specific problem I met recently:

I defined a struct which contains a function

type TestArgs struct {
	name string
    ...
	customAssertForTypeA func(array []*pb.TypeA)
    customAssertForTypeB func(array []*pb.TypeB)
}

// Define the test case with TestArgs.
tests := []TestArgs{
		{
			name: "Test A",
			customAssertForTypeA: func(array []*pb.TypeA) {
				// Some code
			}
		},
		{
			name: "Test B",
			customAssertForTypeB: func(array []*pb.TypeB) {
				// Some code
			}
		},
}

My question is how to make customerAssert function accept generic type of parameter?
I saw some similar question with solution interface{}, so I tried

customAssert func(array interface{})

and it doesn't work.

答案1

得分: 3

类型断言(https://tour.golang.org/methods/15)是在你拥有一个interface{}类型的变量并且想要访问具体值时使用的。

type TestArgs struct {
    name         string
    customAssert func(interface{})
}

// 使用TestArgs定义测试用例。
tests := []TestArgs{
    {
        name: "Test A",
        customAssert: func(param interface{}) {
            // 一些代码
            array, ok := param.([]*pb.TypeA)
            if !ok {
                t.Fatal("断言错误 TypeA")
            }
            // 使用array
        },
    },
    {
        name: "Test B",
        customAssert: func(param interface{}) {
            // 一些代码
            array, ok := param.([]*pb.TypeB)
            if !ok {
                t.Fatal("断言错误 TypeB")
            }
            // 使用array
        },
    },
}

以上是你需要的翻译内容。

英文:

Type assertions ( https://tour.golang.org/methods/15 ) is what you need when you have an interface{} and want to access to the concrete value:

type TestArgs struct {
	name         string
	customAssert func(interface{})
}

// Define the test case with TestArgs.
tests := []TestArgs{
	{
		name: "Test A",
		customAssert: func(param interface{}) {
			// Some code
			array, ok := param.([]*pb.TypeA)
			if !ok {
				t.Fatal("assertion error TypeA")
			}
			// use array
		},
	},
	{
		name: "Test B",
		customAssert: func(param interface{}) {
			// Some code
			array, ok := param.([]*pb.TypeB)
			if !ok {
				t.Fatal("assertion error TypeB")
			}
			// use array
		},
	},
}

huangapple
  • 本文由 发表于 2021年8月10日 06:39:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/68719282.html
匿名

发表评论

匿名网友

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

确定