douduiwei2831 2016-02-08 10:23
浏览 9
已采纳

范围内的[] interfaces {},并获得每种类型的通道字段

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 
", t, t.GetSize())
    }

    for _, t := range things {
        fmt.Printf("%+v 
", 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条回答 默认 最新

  • dpict99695329 2016-02-08 10:28
    关注

    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 
    ", 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line