遍历一个`[]interfaces{}`并获取每个类型的`channel`字段。

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

range over an []interfaces{} and get the channel field of each type

问题

我会尽量清晰地解释一下,首先在我的脑海中。

我有一个接口和一些通过声明方法来继承它的类型。这是一种非常好的和聪明的继承方式。

然后我有一个"超级"类型Thing,所有其他类型都嵌入其中。

Thing结构体有一个Size int和一个Out chan属性。

我试图理解的是为什么我可以从子结构体中获取size的值.GetSize(),但是我在通道字段.GetChannel()上却没有同样的成功(我在其中使用通信来在goroutine和它们的调用者之间进行通信)。

...在这里我得到了t.GetChannel undefined (type Measurable has no field or method GetChannel)

这里可能需要一个逻辑的演示:

package main

import (
	"fmt"
)

type Measurable interface {
	GetSize() int
}

type Thing struct {
	Size int
	Out  chan int
}
type Something struct{ *Thing }
type Otherthing struct{ *Thing }

func newThing(size int) *Thing {
	return &Thing{
		Size: size,
		Out:  make(chan int),
	}
}
func NewSomething(size int) *Something   { return &Something{Thing: newThing(size)} }
func NewOtherthing(size int) *Otherthing { return &Otherthing{Thing: newThing(size)} }

func (s *Thing) GetSize() int         { return s.Size }
func (s *Thing) GetChannel() chan int { return s.Out }

func main() {

	things := []Measurable{}

	pen := NewSomething(7)
	paper := NewOtherthing(5)

	things = append(things, pen, paper)

	for _, t := range things {
		fmt.Printf("%T %d \n", t, t.GetSize())
	}

	for _, t := range things {
		fmt.Printf("%+v \n", t.GetChannel())
	}

	// for _, t := range things {
	// fmt.Printf("%+v", t.Thing.Size)
	// }
}

被注释的代码是我正在尝试学习的另一件事。我可以通过在超级类型上声明的方法来获取一个值,但是不能直接从子类型中访问。当然,我可以使用t.(*bothTheThingTypes).Size来解析类型,但我失去了动态性,我还没有完全理解这一点...

英文:

I'll try to make it as clear as possible, in my head first.

I have an interface and a couple of Types that inherit it by declaring a method. Pretty nice and clever way of inheritance.

I have then a "super" Type Thing, which all the other Types embed.

The Thing struct has a Size int and an Out chan properties

What I'm trying to understand is why I can get the value of size .GetSize() from both the child structs, but I don't have the same success with the channel field .GetChannel() (*ndr which I'm using to communicate among goroutines and their caller)

...here I get t.GetChannel undefined (type Measurable has no field or method GetChannel)

It might help a demo of the logic:

package main

import (
	"fmt"
)

type Measurable interface {
	GetSize() int
}

type Thing struct {
	Size int
	Out  chan int
}
type Something struct{ *Thing }
type Otherthing struct{ *Thing }

func newThing(size int) *Thing {
	return &Thing{
		Size: size,
		Out:  make(chan int),
	}
}
func NewSomething(size int) *Something   { return &Something{Thing: newThing(size)} }
func NewOtherthing(size int) *Otherthing { return &Otherthing{Thing: newThing(size)} }

func (s *Thing) GetSize() int         { return s.Size }
func (s *Thing) GetChannel() chan int { return s.Out }

func main() {

	things := []Measurable{}

	pen := NewSomething(7)
	paper := NewOtherthing(5)

	things = append(things, pen, paper)

	for _, t := range things {
		fmt.Printf("%T %d \n", t, t.GetSize())
	}

	for _, t := range things {
		fmt.Printf("%+v \n", t.GetChannel())
	}

	// for _, t := range things {
	// fmt.Printf("%+v", t.Thing.Size)
	// }
}

The commented code is another thing I'm trying to learn. I can get a value by using a method declared on the super Type, but not by accessing directly from the child one. Sure, I could resolve the type with t.(*bothTheThingTypes).Size but I lose the dinamicity, I'm not fully getting this...

答案1

得分: 2

我正在尝试理解的是为什么我可以从两个子结构体中获取 GetSize() 方法的值,但是对于 GetChannel() 方法,我没有同样的成功。

type Measurable interface {
    GetSize() int
}

...

things := []Measurable{}
for _, t := range things {
    fmt.Printf("%+v \n", t.GetChannel())
}

我可能误解了问题,但这似乎严格是由于你的 Measurable 接口没有一个 GetChannel 方法引起的。

英文:

> What I'm trying to understand is why I can get the value of size
> .GetSize() from both the child structs, but I don't have the same
> success with the channel field .GetChannel()

type Measurable interface {
    GetSize() int
}

...

things := []Measurable{}
for _, t := range things {
    fmt.Printf("%+v \n", t.GetChannel())
}

I may be missing the point but this seems to be caused strictly by the fact that your Measurable interface doesn't have a GetChannel method.

huangapple
  • 本文由 发表于 2016年2月8日 18:23:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/35267271.html
匿名

发表评论

匿名网友

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

确定