FAQ
I have package A and B that is defined under A

package A
     package B

It's like package image
and package image/png

Then is there anyway that I can import package A from package B and use the
struct in A as a receiver for a method in B? I can simply use function, not
method. But just wonder if I can do something similar
to https://code.google.com/p/go/source/browse/src/pkg/image/draw/draw.go#20


In package A, I define the following:

// Space is a space for drawing.
type Space struct {
image.RGBA
}

Then I create a sub-package called B with the following import lines.

import (
  "github.com/user/project/A"
)

And want to use in that sub-package B, like the following.

func (s A.Space) Draw() {
  return ....
}

This does not work. So I tried the following:

type Space interface {
A.Space
}

func (s Space) Draw() {
  return ....
}

*And get the message: interface contains embedded non-interface*

Is there any other way that I can use parental-package as sub-package
receiver? Or is there anyway that I can just make the sub-package inherit
everything from the parental package?

Thanks!

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

  • Dave Cheney at Mar 13, 2014 at 4:17 am
    No, this won't work

    Firstly, Go has no concept of sub packages. Packages may be given path
    names that are given them useful logical groupings, ie, crypto/sha1, but
    the sha1 package is not a child of the crypto package; they have no special
    relationship.
    On Thursday, 13 March 2014 15:12:28 UTC+11, Gyu-Ho Lee wrote:

    I have package A and B that is defined under A

    package A
    package B

    It's like package image
    and package image/png

    Then is there anyway that I can import package A from package B and use
    the struct in A as a receiver for a method in B? I can simply use function,
    not method. But just wonder if I can do something similar to
    https://code.google.com/p/go/source/browse/src/pkg/image/draw/draw.go#20


    In package A, I define the following:

    // Space is a space for drawing.
    type Space struct {
    image.RGBA
    }

    Then I create a sub-package called B with the following import lines.

    import (
    "github.com/user/project/A"
    )

    And want to use in that sub-package B, like the following.

    func (s A.Space) Draw() {
    return ....
    }
    This does not work, you may not define a method on a type you did not
    declare in your own package, exported or not.

    This does not work. So I tried the following:

    type Space interface {
    A.Space
    }
    Interfaces may only contain other interfaces, they cannot contain structs,
    or other types because interfaces define behaviour, methods, not values.

    func (s Space) Draw() {
    return ....
    }

    *And get the message: interface contains embedded non-interface*

    Is there any other way that I can use parental-package as sub-package
    receiver? Or is there anyway that I can just make the sub-package inherit
    everything from the parental package?
    No, for reasons explained at the top of this post.

    Thanks!
    --
    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.
  • Gyu-Ho Lee at Mar 13, 2014 at 4:20 am
    I understand. Thanks a lot, Dave!

    --
    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
postedMar 13, '14 at 4:12a
activeMar 13, '14 at 4:20a
posts3
users2
websitegolang.org

2 users in discussion

Gyu-Ho Lee: 2 posts Dave Cheney: 1 post

People

Translate

site design / logo © 2021 Grokbase