FAQ
I'm very new to go. ~ 2 weeks.

I'm learning go by writing a small middleware system.

End points can be consumers or producers

I've just started to model this with interfaces

type Endpter interface {
GetName() string
GetEndPtType() string
}

type Consumer interface {
GetName() string
GetEndPtType() string
IsConsumer() bool
}
type Producer interface {
GetName() string
GetEndPtType() string
IsProducer() bool
}


So what I have is overlaping interfaces that have some methods in common.
  Is there some reason to avoid this or not?

Thanks

Eric

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Volker Dobler at May 22, 2013 at 1:03 pm

    Am Mittwoch, 22. Mai 2013 14:39:01 UTC+2 schrieb Eric Palmer:
    type Endpter interface {
    GetName() string
    GetEndPtType() string
    }

    type Consumer interface {
    GetName() string
    GetEndPtType() string
    IsConsumer() bool
    }
    type Producer interface {
    GetName() string
    GetEndPtType() string
    IsProducer() bool
    }

    So what I have is overlaping interfaces that have some methods in common.
    Is there some reason to avoid this or not?
    That depends: If Consumer and Producer are always Endpoints it
    seem more natural to me to embed an Enpoint:

    type Endpoint interface {
         Name() string
         Type() string
    }

    type Consumer interface {
          Endpoint
          // consumer-only methods here
    }

    I am not sure if I like the IsConsumer or IsProducer methods:
    Such can be queried by a type switch or cast. (What would
    happen if a Consumer answers with false to IsConsumer?)
    If this needs to be a method: Why not as part of Enpoint?

    V.




    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Henry Heikkinen at May 22, 2013 at 1:04 pm
    Hi Eric,

    You could embed the Endpter interface in the two others. See:
    http://play.golang.org/p/BUu1xO57OW

    IsProducer and IsConsumer methods seem a little strange but I don't know
    anything else about the code. Otherwise it's looking good.

    Regards,
    Henry Heikkinen


    2013/5/22 Eric Palmer <eric@ericfpalmer.com>
    I'm very new to go. ~ 2 weeks.

    I'm learning go by writing a small middleware system.

    End points can be consumers or producers

    I've just started to model this with interfaces

    type Endpter interface {
    GetName() string
    GetEndPtType() string
    }

    type Consumer interface {
    GetName() string
    GetEndPtType() string
    IsConsumer() bool
    }
    type Producer interface {
    GetName() string
    GetEndPtType() string
    IsProducer() bool
    }


    So what I have is overlaping interfaces that have some methods in common.
    Is there some reason to avoid this or not?

    Thanks

    Eric

    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Eric Palmer at May 22, 2013 at 1:15 pm
    Thanks to both of you. Consumers and Producers are always and Endpoint.

    IsConsumer() and IsProducer() were dirty methods because I had not figured
    out how to do that with reflection yet.

    Embedding these into Endpter will be useful.

    Eric

    On Wed, May 22, 2013 at 9:03 AM, Henry Heikkinen wrote:

    Hi Eric,

    You could embed the Endpter interface in the two others. See:
    http://play.golang.org/p/BUu1xO57OW

    IsProducer and IsConsumer methods seem a little strange but I don't know
    anything else about the code. Otherwise it's looking good.

    Regards,
    Henry Heikkinen


    2013/5/22 Eric Palmer <eric@ericfpalmer.com>
    I'm very new to go. ~ 2 weeks.

    I'm learning go by writing a small middleware system.

    End points can be consumers or producers

    I've just started to model this with interfaces

    type Endpter interface {
    GetName() string
    GetEndPtType() string
    }

    type Consumer interface {
    GetName() string
    GetEndPtType() string
    IsConsumer() bool
    }
    type Producer interface {
    GetName() string
    GetEndPtType() string
    IsProducer() bool
    }


    So what I have is overlaping interfaces that have some methods in common.
    Is there some reason to avoid this or not?

    Thanks

    Eric

    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.



    --
    KK4MWM (Ham Radio)
    About Me <http://about.me/ericpalmer>
    http://www.thingiverse.com/DaddyOh
    Join the 3DPrinter Revolution
    http://makerbot.com

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Henry Heikkinen at May 22, 2013 at 1:17 pm
    You don't need reflection--at least as in reflect package--to do that. See
    type assertions: http://golang.org/ref/spec#Type_assertions

    Regards,
    Henry Heikkinen


    2013/5/22 Eric Palmer <eric@ericfpalmer.com>
    Thanks to both of you. Consumers and Producers are always and Endpoint.

    IsConsumer() and IsProducer() were dirty methods because I had not figured
    out how to do that with reflection yet.

    Embedding these into Endpter will be useful.

    Eric

    On Wed, May 22, 2013 at 9:03 AM, Henry Heikkinen wrote:

    Hi Eric,

    You could embed the Endpter interface in the two others. See:
    http://play.golang.org/p/BUu1xO57OW

    IsProducer and IsConsumer methods seem a little strange but I don't know
    anything else about the code. Otherwise it's looking good.

    Regards,
    Henry Heikkinen


    2013/5/22 Eric Palmer <eric@ericfpalmer.com>
    I'm very new to go. ~ 2 weeks.

    I'm learning go by writing a small middleware system.

    End points can be consumers or producers

    I've just started to model this with interfaces

    type Endpter interface {
    GetName() string
    GetEndPtType() string
    }

    type Consumer interface {
    GetName() string
    GetEndPtType() string
    IsConsumer() bool
    }
    type Producer interface {
    GetName() string
    GetEndPtType() string
    IsProducer() bool
    }


    So what I have is overlaping interfaces that have some methods in
    common. Is there some reason to avoid this or not?

    Thanks

    Eric

    --
    You received this message because you are subscribed to the Google
    Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send
    an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.



    --
    KK4MWM (Ham Radio)
    About Me <http://about.me/ericpalmer>
    http://www.thingiverse.com/DaddyOh
    Join the 3DPrinter Revolution
    http://makerbot.com
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Eric Palmer at May 22, 2013 at 1:18 pm
    Even better.

    thanks

    On Wed, May 22, 2013 at 9:16 AM, Henry Heikkinen wrote:

    You don't need reflection--at least as in reflect package--to do that. See
    type assertions: http://golang.org/ref/spec#Type_assertions

    Regards,
    Henry Heikkinen


    2013/5/22 Eric Palmer <eric@ericfpalmer.com>
    Thanks to both of you. Consumers and Producers are always and Endpoint.

    IsConsumer() and IsProducer() were dirty methods because I had not
    figured out how to do that with reflection yet.

    Embedding these into Endpter will be useful.

    Eric

    On Wed, May 22, 2013 at 9:03 AM, Henry Heikkinen wrote:

    Hi Eric,

    You could embed the Endpter interface in the two others. See:
    http://play.golang.org/p/BUu1xO57OW

    IsProducer and IsConsumer methods seem a little strange but I don't know
    anything else about the code. Otherwise it's looking good.

    Regards,
    Henry Heikkinen


    2013/5/22 Eric Palmer <eric@ericfpalmer.com>
    I'm very new to go. ~ 2 weeks.

    I'm learning go by writing a small middleware system.

    End points can be consumers or producers

    I've just started to model this with interfaces

    type Endpter interface {
    GetName() string
    GetEndPtType() string
    }

    type Consumer interface {
    GetName() string
    GetEndPtType() string
    IsConsumer() bool
    }
    type Producer interface {
    GetName() string
    GetEndPtType() string
    IsProducer() bool
    }


    So what I have is overlaping interfaces that have some methods in
    common. Is there some reason to avoid this or not?

    Thanks

    Eric

    --
    You received this message because you are subscribed to the Google
    Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send
    an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

    --
    You received this message because you are subscribed to the Google
    Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send
    an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.



    --
    KK4MWM (Ham Radio)
    About Me <http://about.me/ericpalmer>
    http://www.thingiverse.com/DaddyOh
    Join the 3DPrinter Revolution
    http://makerbot.com

    --
    KK4MWM (Ham Radio)
    About Me <http://about.me/ericpalmer>
    http://www.thingiverse.com/DaddyOh
    Join the 3DPrinter Revolution
    http://makerbot.com

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Bienlein at May 22, 2013 at 1:22 pm
    Go does not support method overloading, se this famous example from here<http://www.infoq.com/articles/google-go-primer>
    :

    package main

    import "fmt"

    type Magician interface {
         Magic()
    }

    type Base struct{
         magician Magician
    }

    func (Base) Magic() {
         fmt.Print("base magic")
    }

    func (self Base) MoreMagic() {
         self.magician.Magic()
         self.Magic()
    }

    type Foo struct {
         Base
    }

    func (Foo) Magic() {
         fmt.Print("foo magic")
    }

    func main() {

         f := new(Foo)
         f.magician = f
         f.MoreMagic() // ==> foo magicbase magic
    }

    When run this will print "foo magicbase magic". So in your Consumer and
    Producer you will have to implement both IsConsumer() and IsProducer()
    retruning each true or false as required.

    -- Bienlein

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Nico at May 22, 2013 at 3:20 pm
    would it be possible to model this using structs?
    See: http://golang.org/doc/effective_go.html#embedding

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Bienlein at May 22, 2013 at 5:22 pm
    I'm not really sure whether I understand what you are asking for. But I
    just realized that what in my reply was this:

    func main() {
    f := new(Foo)
    f.magician = f
    f.MoreMagic()
    }

    was the trick that would do what you would like to do, see this<https://groups.google.com/forum/?hl=de&fromgroups=#!topic/golang-nuts/yM2JguyucM0>thread. To correct my mistake the example from the go
    primer <http://www.infoq.com/articles/google-go-primer> has this sample
    code:

    func main() {
    f := new(Foo)
    f.Magic() //=> foo magic
    f.MoreMagic() //=> base magic base magic
    }
    And this is the code that prints "base magic base magic"

    would it be possible to model this using structs?
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Nico at May 22, 2013 at 5:46 pm

    On 22/05/13 18:22, Bienlein wrote:
    I'm not really sure whether I understand what you are asking for.

    http://play.golang.org/p/wRL6YxXYY5



    package main

    import "fmt"

    type EndPt interface {
      Name() string
      Type() string
    }

    type myEndPt struct {
      _name string
      _type string
    }

    func (e *myEndPt) Name() string { return e._name }
    func (e *myEndPt) Type() string { return e._type }
    func (e *myEndPt) String() string { return "name = " + e._name + "\ntype
    = " + e._type }

    type Consumer struct {
      *myEndPt
    }

    type Producer struct {
      *myEndPt
    }

    type MindTheGap interface {
      EndPt
    }

    func main() {
      var c EndPt = Consumer{&myEndPt{"consumer name", "consumer type"}}
      var p EndPt = Producer{&myEndPt{"producer name", "producer type"}}

      _, isConsumer := c.(Consumer)
      _, isProducer := p.(Producer)
      _, isMindTheGap := p.(MindTheGap)

      fmt.Println("isConsumer =", isConsumer)
      fmt.Println(c)

      fmt.Println()

      fmt.Println("isProducer =", isProducer)
      fmt.Println(p)

      fmt.Println()

      fmt.Println("isMindTheGap =", isMindTheGap)
      fmt.Println(p)
    }


    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Bienlein at May 22, 2013 at 6:40 pm
    What about this one:

    package main

    import "fmt"

    type EndPt interface {
         Name() string
         Type() string
         IsConsumer() bool
         IsProducer() bool
    }

    type myEndPt struct {
         _name string
         _type string
    }

    type Consumer struct {
         *myEndPt
    }


    func (self Consumer) IsConsumer() bool {
         return true
    }

    func (self Consumer) IsProducer() bool {
         return false
    }

    type Producer struct {
         *myEndPt
    }

    func (self Producer) IsConsumer() bool {
         return false
    }

    func (self Producer) IsProducer() bool {
         return true
    }


    func main() {
         consumer := new(Consumer)
         producer := new(Producer)

         fmt.Println("isConsumer =", consumer.IsConsumer())
         fmt.Println("isConsumer =", consumer.IsProducer())

         fmt.Println()

         fmt.Println("isProducer =", producer.IsProducer())
         fmt.Println("isConsumer =", producer.IsConsumer())
    }

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Nico at May 22, 2013 at 7:10 pm
    What would be the need to define isConsumer and isProducer?
    Go already provides the means to test the type.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Eric Palmer at May 22, 2013 at 7:12 pm
    Just because I hadn't gotten to that point in my learning. They have been
    ripped out of the code already.

    thanks

    Eric

    On Wed, May 22, 2013 at 3:10 PM, Nico wrote:

    What would be the need to define isConsumer and isProducer?
    Go already provides the means to test the type.

    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.



    --
    KK4MWM (Ham Radio)
    About Me <http://about.me/ericpalmer>
    http://www.thingiverse.com/DaddyOh
    Join the 3DPrinter Revolution
    http://makerbot.com

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Nico at May 22, 2013 at 7:42 pm
    Sorry, I hope my quick reply didn't come across as rude.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Eric Palmer at May 22, 2013 at 7:49 pm
    Not at all.

    On Wed, May 22, 2013 at 3:42 PM, Nico wrote:

    Sorry, I hope my quick reply didn't come across as rude.


    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.



    --
    KK4MWM (Ham Radio)
    About Me <http://about.me/ericpalmer>
    http://www.thingiverse.com/DaddyOh
    Join the 3DPrinter Revolution
    http://makerbot.com

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Bienlein at May 22, 2013 at 7:26 pm

    What would be the need to define isConsumer and isProducer?
    Go already provides the means to test the type.
    Make use of OO message sends. The reason you have to do instanceof in Java
    is because Java does not have extension methods. So you could not add a
    method isProducer() to some common super class in case you don't own it.
    This way you have to revert to instanceof as a last resort. This made
    people believe that instanceof is a proper way of checking the type, but it
    is only a practical workaround in case OO message sends don't work.

    My previous code can be made much smarter:

    package main

    import "fmt"

    type EndPt struct {

    }

    func (self EndPt) IsConsumer() bool {
         return false
    }

    func (self EndPt) IsProducer() bool {
         return false
    }

    type Consumer struct {
         EndPt
    }

    func (self Consumer) IsConsumer() bool {
         return true
    }

    type Producer struct {
         EndPt
    }

    func (self Producer) IsProducer() bool {
         return true
    }


    func main() {
         consumer := new(Consumer)
         producer := new(Producer)

         fmt.Println("isConsumer =", consumer.IsConsumer())
         fmt.Println("isProducer =", consumer.IsProducer())

         fmt.Println()

         fmt.Println("isProducer =", producer.IsProducer())
         fmt.Println("isConsumer =", producer.IsConsumer())
    }

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedMay 22, '13 at 12:39p
activeMay 22, '13 at 7:49p
posts16
users5
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase