3

Suppose I have a package within a package github.com/user/foo:

foo/
  main.go (package main)
  bar/
    bar.go (package bar)

Now, within bar.go I would like to use an exported type from main.go.

package main
type FooBar struct {}

If I try to import "github.com/user/foo" I get cycled imports not allowed error. Is there any way to do this, other than to create own repo for bar, which I don't want.

1 Answer 1

6

You can separate it like:

foo/
  main.go (package main)
  foo/
    foo.go (package foo)
  bar/
    bar.go (package bar)

then import "path/foo/foo" in bar.go

Sign up to request clarification or add additional context in comments.

6 Comments

This would require a rather large refactoring for my existing codebase...it would clean things up however.
@Jan a little bit refactoring doesn't hurt and IMO it will be easier to maintain in the long run.
Oh yea I totally agree. It would remove main namespace pollution and modularize the codebase. But, its a large legacy project, so Im going to pass on this refactoring.
A large legacy project in Go? I didn't realize we were old enough to have legacy projects... My how time flies.
Well, yeah, but it wasn't really a stable language until the 1.0 release, which was 2.5 years ago. I doubt there are any existing (maintained) codebases using a pre-1.0 language spec.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.