从任意接口列表中满足哪些接口?

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

Which interfaces satisfied from list of arbitrary interfaces?

问题

有一种方法可以从任意接口列表中确定一个具体类型实现了哪些接口吗?我知道可以使用类型切换,但我想知道所有满足的接口。

例如,给定以下代码:

  1. type Mover interface { Move() }
  2. type Talker interface { Talk() }
  3. type Flyer interface { Fly() }
  4. type Person struct{}
  5. func (a *Person) Move() {fmt.Println("Moving...") }
  6. func (a *Person) Talk() {fmt.Println("Talking...") }

我可以手动测试每个接口,如下所示:

  1. func testInterfaces(entity interface{}) {
  2. _, ok := entity.(Mover)
  3. if ok {
  4. fmt.Println("mover")
  5. }
  6. _, ok := entity.(Talker)
  7. if ok {
  8. fmt.Println("talker")
  9. }
  10. _, ok := entity.(Flyer)
  11. if ok {
  12. fmt.Println("flyer")
  13. }
  14. }

对于一个Person值,将打印出"mover"和"talker"。然而,我更希望有一个像下面这样的函数(非工作中的函数):

  1. func testInterfaces2(entity interface{}, interfaceList type??) {
  2. for _, inter := range interfaceList {
  3. val, ok := entity.(inter)
  4. if ok {
  5. // 对val进行一些操作
  6. }
  7. }
  8. }

有没有办法实现类似这个伪函数的功能,也许通过reflect包或其他方式?

英文:

Is there a way I can determine which interfaces a concrete type implements from an
arbitrary list of interfaces? I'm aware of a type switch, but I'd like to know all satisfied
interfaces.

For example, given:

  1. type Mover interface { Move() }
  2. type Talker interface { Talk() }
  3. type Flyer interface { Fly() }
  4. type Person struct{}
  5. func (a *Person) Move() {fmt.Println("Moving...") }
  6. func (a *Person) Talk() {fmt.Println("Talking...") }

I can manually test each interface as follows:

  1. func testInterfaces(entity interface{}) {
  2. _, ok := entity.(Mover)
  3. if ok {
  4. fmt.Println("mover")
  5. }
  6. _, ok := entity.(Talker)
  7. if ok {
  8. fmt.Println("talker")
  9. }
  10. _, ok := entity.(Flyer)
  11. if ok {
  12. fmt.Println("flyer")
  13. }
  14. }

For a Person value, "mover", and "talker" will print. However, I'd rather have a function like this (non-working) one:

  1. func testInterfaces2(entity interface{}, interfaceList type??) {
  2. for _, inter := range interfaceList {
  3. val, ok := entity.(inter)
  4. if ok {
  5. // do something with val
  6. }
  7. }
  8. }

Is there a way I can achieve something like this pseudo function, maybe through the reflect package or some other means?

答案1

得分: 3

你可以使用这个链接来获取接口类型的切片。

然后,你可以像这里一样检查你的值的Type是否实现了该接口:

  1. interfaces := []reflect.Type{reflect.TypeOf((*Mover)(nil)).Elem(),
  2. reflect.TypeOf((*Talker)(nil)).Elem(),
  3. reflect.TypeOf((*Flyer)(nil)).Elem()}
  4. p := &Person{}
  5. t := reflect.TypeOf(p)
  6. name := t.Elem().Name()
  7. for _, interf := range interfaces {
  8. if t.Implements(interf) {
  9. fmt.Printf("%s 是一个 %s\n", name, interf.Name())
  10. } else {
  11. fmt.Printf("%s 不是一个 %s\n", name, interf.Name())
  12. }
  13. }

但是,如果可能的话,我认为使用类型切换(Type switch)更好。

英文:

You could use this to get a slice of interface types.

Then, you can just check whether the Type of your value implements the interface as in here:

  1. interfaces := []reflect.Type{reflect.TypeOf((*Mover)(nil)).Elem(),
  2. reflect.TypeOf((*Talker)(nil)).Elem(),
  3. reflect.TypeOf((*Flyer)(nil)).Elem()}
  4. p := &Person{}
  5. t := reflect.TypeOf(p)
  6. name := t.Elem().Name()
  7. for _, interf := range interfaces {
  8. if t.Implements(interf) {
  9. fmt.Printf("%s is a %s\n", name, interf.Name())
  10. } else {
  11. fmt.Printf("%s is NOT a %s\n", name, interf.Name())
  12. }
  13. }

But I think the use of a Type switch is preferable if at all possible

答案2

得分: 2

go不太擅长处理泛型。处理这个问题的首选方法是使用类型切换:

  1. func testInterfaces2(entity interface{}) {
  2. switch entity.(type) {
  3. case Mover:
  4. fmt.Println("mover")
  5. case Talker:
  6. fmt.Println("talker")
  7. case Flyer:
  8. fmt.Println("flyer")
  9. default:
  10. fmt.Println("something else")
  11. }
  12. }

但正如val所说,获取类型实现的所有接口的方法是(val代码的清理版本):

  1. package main
  2. import "fmt"
  3. import "reflect"
  4. type Mover interface { Move() }
  5. type Talker interface { Talk() }
  6. type Flyer interface { Fly() }
  7. type Person struct{}
  8. func (a *Person) Move() {fmt.Println("Moving...") }
  9. func (a *Person) Talk() {fmt.Println("Talking...") }
  10. func testInterfaces(entity interface{}, interfaces []reflect.Type) {
  11. t := reflect.TypeOf(entity)
  12. name := t.Elem().Name()
  13. for _, interf := range interfaces {
  14. if t.Implements(interf) {
  15. fmt.Printf("%s is a %s\n", name, interf.Name())
  16. } else {
  17. fmt.Printf("%s is NOT a %s\n", name, interf.Name())
  18. }
  19. }
  20. }
  21. func main() {
  22. interfaces := []reflect.Type{
  23. reflect.TypeOf((*Mover)(nil)).Elem(),
  24. reflect.TypeOf((*Talker)(nil)).Elem(),
  25. reflect.TypeOf((*Flyer)(nil)).Elem(),
  26. }
  27. p := &Person{}
  28. testInterfaces(p, interfaces)
  29. }

在Go Play中可以查看:http://play.golang.org/p/6aqMx5CQvY

英文:

One thing that go doesn't handle exceptionally well is generics. The Prefered way to do this is a type switch:

  1. func testInterfaces2(entity interface{}) {
  2. switch entity.(type) {
  3. case Mover:
  4. fmt.Println("mover")
  5. case Talker:
  6. fmt.Println("talker")
  7. case Flyer:
  8. fmt.Println("flyer")
  9. default:
  10. fmt.Println("something else")
  11. }
  12. }

But like val said, the way to get all of the interfaces that are implemented by a type is (a cleaned up version of val's code):

  1. package main
  2. import "fmt"
  3. import "reflect"
  4. type Mover interface { Move() }
  5. type Talker interface { Talk() }
  6. type Flyer interface { Fly() }
  7. type Person struct{}
  8. func (a *Person) Move() {fmt.Println("Moving...") }
  9. func (a *Person) Talk() {fmt.Println("Talking...") }
  10. func testInterfaces(entity interface{}, interfaces []reflect.Type) {
  11. t := reflect.TypeOf(entity)
  12. name := t.Elem().Name()
  13. for _, interf := range interfaces {
  14. if t.Implements(interf) {
  15. fmt.Printf("%s is a %s\n", name, interf.Name())
  16. } else {
  17. fmt.Printf("%s is NOT a %s\n", name, interf.Name())
  18. }
  19. }
  20. }
  21. func main() {
  22. interfaces := []reflect.Type{
  23. reflect.TypeOf((*Mover)(nil)).Elem(),
  24. reflect.TypeOf((*Talker)(nil)).Elem(),
  25. reflect.TypeOf((*Flyer)(nil)).Elem(),
  26. }
  27. p := &Person{}
  28. testInterfaces(p, interfaces)
  29. }

Here it is in go play: http://play.golang.org/p/6aqMx5CQvY

huangapple
  • 本文由 发表于 2013年12月13日 23:32:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/20570193.html
匿名

发表评论

匿名网友

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

确定