FAQ
Hi -- I've misunderstood an issue in Go to do with interfaces.
I'm trying to use them to decouple my code, but my approach is failing as
follows.

Given the following toy code:

package main

import (
"fmt"
)

type Child struct {
name string
}

type Parent struct {
child *Child
}

func (c *Child) Name() string {
return c.name
}

func (p *Parent) Child() *Child {
return p.child
}

type CI interface {
Name() string
}

type PI interface {
Child() CI
}

func main() {
c := &Child{name: "Horace"}
p := &Parent{child: c}
pi := PI(p)
fmt.Println(c, p, pi)
}


I get this error:
test/scratch.go:34: cannot convert p (type *Parent) to type PI:

*Parent does not implement PI (wrong type for Child method)

have Child() *Child

want Child() CI
The obvious way to fix this would be to have the interface PI return a
pointer to a concrete Child type, but then that would rather couple the
interface to the implementation, wouldn't it?
Could someone help me understand what I'm seeing/doing wrong, here?
Thanks very much for any & all advice,
    Doug.

--
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

  • Pierre Curto at Jan 29, 2016 at 4:26 pm
    Hello,

    I think you want the Parent method Child() to return CI:
    http://play.golang.org/p/CIh5nwLGzo

    HTH

    Le vendredi 29 janvier 2016 17:05:13 UTC+1, doug livesey a écrit :
    Hi -- I've misunderstood an issue in Go to do with interfaces.
    I'm trying to use them to decouple my code, but my approach is failing as
    follows.

    Given the following toy code:

    package main

    import (
    "fmt"
    )

    type Child struct {
    name string
    }

    type Parent struct {
    child *Child
    }

    func (c *Child) Name() string {
    return c.name
    }

    func (p *Parent) Child() *Child {
    return p.child
    }

    type CI interface {
    Name() string
    }

    type PI interface {
    Child() CI
    }

    func main() {
    c := &Child{name: "Horace"}
    p := &Parent{child: c}
    pi := PI(p)
    fmt.Println(c, p, pi)
    }


    I get this error:
    test/scratch.go:34: cannot convert p (type *Parent) to type PI:

    *Parent does not implement PI (wrong type for Child method)

    have Child() *Child

    want Child() CI
    The obvious way to fix this would be to have the interface PI return a
    pointer to a concrete Child type, but then that would rather couple the
    interface to the implementation, wouldn't it?
    Could someone help me understand what I'm seeing/doing wrong, here?
    Thanks very much for any & all advice,
    Doug.
    --
    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.
  • Doug livesey at Jan 29, 2016 at 4:57 pm
    Thanks for that, Pierre. Actually, for my use-case, what I think I need are
    generics. I just assumed that interfaces would work like generics.
    I've had some very depressing reading to do around Go and generics whilst
    trying to solve this problem!
    Thanks for your help,
        Doug.
    On Friday, 29 January 2016 16:26:43 UTC, pierre...@gmail.com wrote:

    Hello,

    I think you want the Parent method Child() to return CI:
    http://play.golang.org/p/CIh5nwLGzo

    HTH

    Le vendredi 29 janvier 2016 17:05:13 UTC+1, doug livesey a écrit :
    Hi -- I've misunderstood an issue in Go to do with interfaces.
    I'm trying to use them to decouple my code, but my approach is failing as
    follows.

    Given the following toy code:

    package main

    import (
    "fmt"
    )

    type Child struct {
    name string
    }

    type Parent struct {
    child *Child
    }

    func (c *Child) Name() string {
    return c.name
    }

    func (p *Parent) Child() *Child {
    return p.child
    }

    type CI interface {
    Name() string
    }

    type PI interface {
    Child() CI
    }

    func main() {
    c := &Child{name: "Horace"}
    p := &Parent{child: c}
    pi := PI(p)
    fmt.Println(c, p, pi)
    }


    I get this error:
    test/scratch.go:34: cannot convert p (type *Parent) to type PI:

    *Parent does not implement PI (wrong type for Child method)

    have Child() *Child

    want Child() CI
    The obvious way to fix this would be to have the interface PI return a
    pointer to a concrete Child type, but then that would rather couple the
    interface to the implementation, wouldn't it?
    Could someone help me understand what I'm seeing/doing wrong, here?
    Thanks very much for any & all advice,
    Doug.
    --
    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
postedJan 29, '16 at 4:05p
activeJan 29, '16 at 4:57p
posts3
users2
websitegolang.org

2 users in discussion

Doug livesey: 2 posts Pierre Curto: 1 post

People

Translate

site design / logo © 2021 Grokbase