On Tue, Mar 25, 2014 at 6:30 AM, Erik Engheim <
erik.e...@gmail.com> wrote:
>
> On 25 Mar 2014, at 12:49, Michael Jones <
m...@google.com> wrote:
>
> Go's import mechanism is clever. The magic info that is imported by that
> reference contains all the information about the entire tree of calls that
> package makes. So, if you have 1000 files in 50 packages of 20 files, the
> compiler ends up reading 50 import definitions rather than 1000. That helps.
>
>
> What you mean is that if my Go program imports these 50 packages, then when
> I compile my program it will only look at the functions and types exported
> by these 50 packages. It will not look at all those other functions exposed
> by the other 1000 files?
>
> E.g. if I have two packages circle and point, and I have a main package for
> my program which only includes circle.
>
> circle package:
>
> import . “point”
>
> type Circle struct {
> Center Point
> Radius float64
> }
>
> point package:
>
> type Point struct {
> X, Y float64
> }
>
> main package
>
> import “circle”
>
> Then will the compiled circle package only contain a definition for Circle
> and not Point?
What matters here is specifically the export information. The export
information will in fact include a definition for Point, although it
will not be accessible from the main package. The definition is
needed in order to describe the circle.Circle type. But the export
information has been precompiled, so reading it is faster than parsing
the original file.
> Wouldn’t this be similar to using a
> forward declaration of Point in C++. If the circle header file doesn’t
> include the point header file I don’t have to pay for that either in C++.
Yes, but in your example that is not possible, because Circle has a
field of type Point. If it had a field of type *Point then a forward
declaration of Point would suffice (in either C++ or Go).
> I am sorry if I am dense about this. But I am a C++ guy so I don’t really
> have a good mental model for how this sort of thing works without header
> files.
Compiling a package produces export information that describes all the
exported names in the package. Importing a package reads that export
information.
Ian