指向实现相同接口的不同结构体的指针数组

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

Array of pointers to different struct implementing same interface

问题

我想做的事情是:我有几种结构类型,它们都实现了相同的接口,声明了一个方法,比如"Process()"

type Worker interface {
  Process()
}

type obj1 struct {
}

func (o *obj1) Process() {
}

// obj2、obj3等结构类型也是一样的

在代码中,我创建了几个这些结构体的实例,然后我想调用每个实例的Process()方法。对每个实例调用Process()方法是可以的。

o1 := obj1.New()
o2 := obj1.New()
o3 := obj2.New()
o4 := obj3.New()
// ...
o1.Process()
o2.Process()
// ...

现在,我想要一个"ProcessAll()"函数,它可以接收这些实例,并在每个实例上调用Process()方法,以及一些相关的元操作。

以下是我尝试编写的代码,但是这个特定的片段不起作用,因为我不确定如何做到这一点:

func ProcessAll(objs []*Worker) {
   for _, obj := range objs {
     obj.Process()
   }
}

ProcessAll([]*Worker{o1, o2, /* ... */})

在Go语言中是否可以实现这种功能,如果可以,我该如何实现?

英文:

What I am trying to do: I have several struct types, all implementing the same interface, which declare a method, say "Process()"

type Worker interface {
  Process()
}

type obj1 struct {
}

func (o *obj1) Process() {
}

// same with struct type obj2, obj3, ...

Throughout the code, I create several instances of these structs, and then I want to call the process on each of these. Calling Process() on each works fine.

o1 := obj1.New()
o2 := obj1.New()
o3 := obj2.New()
o4 := obj3.New()
// ...
o1.Process()
o2.Process()
// ...

Now, I would like to have a "ProcessAll()" function, that would receive those instances and do the job of calling the Process() method on each of those, as well as some meta work around it.

Here is the sort of code that I'm trying to produce, but this particular snippet does not work because I'm not sure how to do it:

func ProcessAll(objs []*Worker) {
   for _, obj := range objs {
     obj.Process()
   }
}

ProcessAll([]*Worker{o1, o2, /* ... */})

Is that sort of things possible to accomplish with go, and if yes how can I do to implement it ?

答案1

得分: 8

你不需要创建一个指向接口的对象数组。接口是引用值。

package main

import "fmt"

type Worker interface{
    Process()
}

type obj1 struct {
}

func (o *obj1) Process() {
    fmt.Println("obj1 Process()")
}

type obj2 struct {
}

func (o *obj2) Process() {
    fmt.Println("obj2 Process()")
}

func ProcessAll(objs []Worker) {
    for _, o := range objs {
        o.Process()
    }
}

func main() {
    ProcessAll([]Worker{ &obj1{}, &obj2{} })
}

或者在这里查看:http://play.golang.org/p/eWXiZzrN-W

英文:

You don't need to make objs array of pointers to interface. Interfaces are reference values.

package main

import "fmt"

type Worker interface{
	Process()
}

type obj1 struct {
}

func (o *obj1) Process() {
	fmt.Println("obj1 Process()")
}

type obj2 struct {
}

func (o *obj2) Process() {
	fmt.Println("obj2 Process()")
}

func ProcessAll(objs []Worker) {
	for _, o := range objs {
		o.Process()
	}
}

func main() {
	ProcessAll([]Worker{ &obj1{}, &obj2{} })
}

Or check it out here: http://play.golang.org/p/eWXiZzrN-W

huangapple
  • 本文由 发表于 2013年6月13日 07:30:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/17077074.html
匿名

发表评论

匿名网友

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

确定