How do I import Parent-package from Sub-package for method?

7,059 views
Skip to first unread message

Gyu-Ho Lee

unread,
Mar 13, 2014, 12:12:28 AM3/13/14
to golan...@googlegroups.com
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 (
)

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!

Dave Cheney

unread,
Mar 13, 2014, 12:17:34 AM3/13/14
to golan...@googlegroups.com
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 (
)

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!

Gyu-Ho Lee

unread,
Mar 13, 2014, 12:20:25 AM3/13/14
to golan...@googlegroups.com
I understand. Thanks a lot, Dave!
Reply all
Reply to author
Forward
0 new messages