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

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

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

问题

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

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

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

  1. type TestArgs struct {
  2. name string
  3. ...
  4. customAssertForTypeA func(array []*pb.TypeA)
  5. customAssertForTypeB func(array []*pb.TypeB)
  6. }
  7. // 使用TestArgs定义测试用例。
  8. tests := []TestArgs{
  9. {
  10. name: "Test A",
  11. customAssertForTypeA: func(array []*pb.TypeA) {
  12. // 一些代码
  13. }
  14. },
  15. {
  16. name: "Test B",
  17. customAssertForTypeB: func(array []*pb.TypeB) {
  18. // 一些代码
  19. }
  20. },
  21. }

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

  1. 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

  1. type TestArgs struct {
  2. name string
  3. ...
  4. customAssertForTypeA func(array []*pb.TypeA)
  5. customAssertForTypeB func(array []*pb.TypeB)
  6. }
  7. // Define the test case with TestArgs.
  8. tests := []TestArgs{
  9. {
  10. name: "Test A",
  11. customAssertForTypeA: func(array []*pb.TypeA) {
  12. // Some code
  13. }
  14. },
  15. {
  16. name: "Test B",
  17. customAssertForTypeB: func(array []*pb.TypeB) {
  18. // Some code
  19. }
  20. },
  21. }

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

  1. customAssert func(array interface{})

and it doesn't work.

答案1

得分: 3

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

  1. type TestArgs struct {
  2. name string
  3. customAssert func(interface{})
  4. }
  5. // 使用TestArgs定义测试用例。
  6. tests := []TestArgs{
  7. {
  8. name: "Test A",
  9. customAssert: func(param interface{}) {
  10. // 一些代码
  11. array, ok := param.([]*pb.TypeA)
  12. if !ok {
  13. t.Fatal("断言错误 TypeA")
  14. }
  15. // 使用array
  16. },
  17. },
  18. {
  19. name: "Test B",
  20. customAssert: func(param interface{}) {
  21. // 一些代码
  22. array, ok := param.([]*pb.TypeB)
  23. if !ok {
  24. t.Fatal("断言错误 TypeB")
  25. }
  26. // 使用array
  27. },
  28. },
  29. }

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

英文:

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:

  1. type TestArgs struct {
  2. name string
  3. customAssert func(interface{})
  4. }
  5. // Define the test case with TestArgs.
  6. tests := []TestArgs{
  7. {
  8. name: "Test A",
  9. customAssert: func(param interface{}) {
  10. // Some code
  11. array, ok := param.([]*pb.TypeA)
  12. if !ok {
  13. t.Fatal("assertion error TypeA")
  14. }
  15. // use array
  16. },
  17. },
  18. {
  19. name: "Test B",
  20. customAssert: func(param interface{}) {
  21. // Some code
  22. array, ok := param.([]*pb.TypeB)
  23. if !ok {
  24. t.Fatal("assertion error TypeB")
  25. }
  26. // use array
  27. },
  28. },
  29. }

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:

确定