golang结构体方法接受多个参数类型

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

golang struct method to take in multiple argument types

问题

我的方法看起来像这样:

func (t *Worker) updateInfo(n structType1, node structType2)

然而,现在我需要使用这个API来处理structType1和structType3。这意味着n可以是structType3。

我该如何修改这个方法来实现这一点,而不是编写另一个方法如下,并重复相同的代码?

func (t *Worker) updateInfo(n structType3, node structType2)

编辑:这些结构体都是我自定义的结构体。

英文:

My method looks like:

func (t *Worker) updateInfo(n structType1, node structType2)

However, now i need to use this API to process both structType1 and structType3. Which means n could be structType3.

How do i modify the method to achieve this instead of writing another method like below and duplicate the same code?

func (t *Worker) updateInfo(n structType3, node structType2)

EDIT: These structs are all my own custom defined structs

答案1

得分: 1

在这种情况下,你可以使用泛型(generics)。

例如,假设structType1structType2都有一个名为Print的方法。

type structType1 struct{}
func (st1 structType1) Print() {
    fmt.Println("Calling Print function of structType1")
}

type structType3 struct{}
func (st3 structType3) Print() {
    fmt.Println("Calling Print function of structType2")
}

我们可以定义一个接口类型声明,如下所示。

type Struct13 interface {
    Print()
    structType1 | structType3 // 类型联合
}

然后,你需要修改Worker结构体和updateInfo函数的类型参数。(注意:Struct13中的Print函数仅用于演示目的。)

type Worker[T Struct13] struct{}

func (t *Worker[T]) updateInfo(n T, node structType2) {
    n.Print()
}

我们可以使用上述实现,如下所示。

st1 := structType1{}
st2 := structType2{}
st3 := structType3{}

w1 := Worker[structType1]{}
w1.updateInfo(st1, st2)

w2 := Worker[structType3]{}
w2.updateInfo(st3, st2)
英文:

You can use generics in this case.

For instance, let's assume that structType1 and structType2 have a method called Print.

type  structType1 struct {}
func(st1 structType1) Print() {
 fmt.Println("Calling Print function of structType1")
}

type  structType3 struct {}
func(st3 structType3) Print() {
 fmt.Println("Calling Print function of structType2")
}

We can define a interface type declarations as shown below.

type Struct13 interface {
 Print()
 structType1 | structType3 // type union 
}

Then you need to modify Worker struct and updateInfo function with type parameters. (Note: Print funtion in the Struct13 is for demonstration purpose.)

type Worker[T Struct13] struct{}

func (t *Worker[T]) updateInfo(n T, node structType2) {
 n.Print()
}

We can use above implementations as shown below.

    st1 := structType1{}
    st2 := structType2{}
    st3 := structType3{}


    w1 := Worker[structType1]{}
    w1.updateInfo(st1,st2)

    w2 := Worker[structType3]{}
    w2.updateInfo(st3,st2)

答案2

得分: -1

你可以使用空接口

func (t *Worker) updateInfo(n interface{}, node structType2)

在 Go 1.18 之后,你可以使用 any

func (t *Worker) updateInfo(n any, node structType2)

在函数体中

switch v := n.(type) {
    case structType1:
        ...
    case structType3:
        ...
    default:
        ...
}
英文:

you can use empty interface

func (t *Worker) updateInfo(n interface{}, node structType2)

after go 1.18
you can use any

func (t *Worker) updateInfo(n any, node structType2)

and in the function body

switch v := n.(type) {
    case structType1:
        ...
    case structType3:
        ...
    default:
        ...
}

huangapple
  • 本文由 发表于 2023年3月19日 13:26:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75780240.html
匿名

发表评论

匿名网友

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

确定