FAQ
I had been building what I'll call *main* interfaces that had embedded *sub*
interfaces. I would then make structs that fulfilled the *sub* interface
and attach them to an implementation of the *main* interface. But then I
got the idea that I should make the *main* interface just be functions, and
then build structs that were made up of other structs that implemented all
of the needed function calls. This way a function could move from one
struct to another and the over all duck typing would be maintained.

Code here:

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

Is the sane? A bad practice? Do I not understand interface composition?

Thanks in advance.

--
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/d/optout.

Search Discussions

  • Jesse McNelis at Apr 15, 2015 at 4:07 am

    On Wed, Apr 15, 2015 at 2:44 AM, wrote:
    I had been building what I'll call main interfaces that had embedded sub
    interfaces. I would then make structs that fulfilled the sub interface and
    attach them to an implementation of the main interface. But then I got the
    idea that I should make the main interface just be functions, and then build
    structs that were made up of other structs that implemented all of the
    needed function calls. This way a function could move from one struct to
    another and the over all duck typing would be maintained.

    Code here:

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

    Is the sane? A bad practice? Do I not understand interface composition?
    Yep, that's a pretty common pattern, it's composing structs.

    You can compose interfaces from smaller interfaces

    eg.
    type MainThinger interface {
       AddSub
       Fullname(fname, lname string) string
    }

    type AddSub interface {
       Add(n1, n2 int) int
       Sub(n1, n2 int) int
    }

    --
    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/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedApr 14, '15 at 6:55p
activeApr 15, '15 at 4:07a
posts2
users2
websitegolang.org

2 users in discussion

Jesse McNelis: 1 post Jon: 1 post

People

Translate

site design / logo © 2022 Grokbase