Question about 'import' statement

67 views
Skip to first unread message

joe mcguckin

unread,
Feb 3, 2020, 2:40:13 PM2/3/20
to golang-nuts
Do all the files listed in the 'import' statement get pulled in and compiled, or merely scanned to resolve function references etc.

As someone who grew up on 'C', it seems a bit weird to not have an 'include' mechanism and its headers.

Does the it matter what order the files appear in the import clause?

Thanks,

joe

Marcin Romaszewicz

unread,
Feb 3, 2020, 2:52:31 PM2/3/20
to joe mcguckin, golang-nuts
All the files get downloaded to your $GOPATH/src or $GOPATH/pkg, depending on whether modules are enabled.

Everything gets compiled together into the same static executable, so yes, the files get compiled.

The order of imports should not matter in well written code, but you can always create scenarios where importing something tweaks global state, give this a read:

--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/6c63ed3c-a8d8-4b40-879b-725c1a440635%40googlegroups.com.

Brian Candler

unread,
Feb 3, 2020, 3:05:45 PM2/3/20
to golang-nuts
Also, I believe "go fmt" sorts the imports.

Ian Lance Taylor

unread,
Feb 3, 2020, 3:07:00 PM2/3/20
to joe mcguckin, golang-nuts
On Mon, Feb 3, 2020 at 11:40 AM joe mcguckin
<joseph.p...@gmail.com> wrote:
>
Think of a Go package as a .c file automatically defines a .h file
containing declarations of all the names exported by the package.
When you import the package, the compiler reads the implicitly
generated .h file, the Go tool ensures that the equivalent of the .c
file is compiled, and the linker arranges to link in the equivalent of
the .o file.

Ian
Reply all
Reply to author
Forward
0 new messages