3

I am trying to write a go project with several sub projects. For an simple example the project looks like this

Main
 |- package one
    |- package one.one
    |- package one.two
 |- package two

From my main package i can use any function or structure of any sub package By importing them. But My question is how can i access an struct or function of main from any sub package.

sadlil
  • 3,077
  • 5
  • 23
  • 36

2 Answers2

5

By importing the "subpackages" in main. But do not produce an import cycle (Restructure your code in this case).

Note that Go has (almost*) no notion of _sub_package: These are all plain packages and the directory layout has no influence on imports and usability/accessability of exported functions, types, methods, fields, variables and constants.

*) Internal packages and vendored packages depend on directory layout.

Volker
  • 40,468
  • 7
  • 81
  • 87
3

Thanks. I solved this problem by using an third package. Pretty easy that way.

sadlil
  • 3,077
  • 5
  • 23
  • 36
  • 7
    This is not a sufficient answer to the question, if anything @Volker 's answer is the answer if so it should be marked as such. – Adrian Forsius Apr 12 '17 at 15:14