Published in


Import declarations in Go

Programs in Go are made up of packages. Usually package depends on other packages either those built-in into the standard library or 3rd parties. Package needs to be imported first to use its exported identifiers. It’s done with construct called import declaration:

Above we’ve one import declaration with two ImportSpecs. Each ImportSpec defines single package import.

Package called main is used to create executable binary. Program execution starts in package main by calling its function which also called main.

But… there are other, less known options which are useful in various scenarios:

Each of these four import specifications behave differently and in this post we’ll analyse those differences.

Importing package can reference only exported identifiers from imported package. Exported identifiers are the ones started with Unicode upper case letter — https://golang.org/ref/spec#Exported_identifiers.

The basics

Anatomy of import declaration

  • Identifier is any valid identifier which will be used in qualified identifiers
  • ImportPath is string literal (raw or interpreted)

Let’s see some examples:

Factored import declaration

Importing two or more packages can be written in two ways. Either we can write multiple import declarations:

or we can use factored import declaration (using multiple ImportSpec within a single import declaration):

2nd option is especially useful if package has many imports and then repeating import keyword many times decreases readability. It also saves some keystrokes if you don’t use tools like https://github.com/bradfitz/goimports which fix imports automatically.

(Short) import path

String literal used in import specification (each import declaration contains one or more import specification) tells what package to import. This string is called import path. According to language spec it depends on the implementation how import path (string) is interpreted but in real life it’s path relative package’s vendor directory or `go env GOPATH`/src (more about GOPATH).

Built-in packages are imported with short import paths like "math" or "fmt".

Anatomy of .go file

Structure of every .go file is the same. First is package clause optionally preceded with comments usually describing the purpose of the package. Then zero or more import declarations. 3rd section contains zero or more top-level declarations (source code):

Enforced organisation doesn’t allow to introduce unnecessary mess which simplifies parsing and basically navigation through the codebase (import declarations cannot be placed before package clause nor be interleaved with top-level declarations so it’s always easy to find).

Scope of import

The scope of import is the file block. It means that it’s reachable from the whole file but not within the entire package:

Such program cannot be compiled:

More on scopes in one of previous posts:

Types of imports

Custom package name

By convention last component of import path is also name of imported package. Of course nothing stops us from not following that convention:

Output is simply b. Although possible, following the convention is usually better — various tools depend on it.

If custom package name is not specified in import specification then name from package clause is used to reference exported identifiers from imported package:

It’s possible to pass custom package name for import:

And the result is the same as before. This form of import can be useful if we’ve package which has the same interface (exported identifiers) as other package. One such example is https://github.com/sirupsen/logrus which has an API compatible with log:

If we’ll use only API found in built-in log package then replacing such import with import "log" doesn’t require any changes in the source code. It’s also a bit shorter (but still meaningful) so might save some keystrokes.

All exported identifiers into importing block

With imports like:

it’s possible to either reference exported identifier with package name passed in import specification (m.Exp) or with the name from the package clause of imported package (fmt.Println). There is another option which allows to access exported identifier without qualified identifier:

When might it be useful? In tests. Let’s suppose that that we’ve package a which is imported by package b. Now we want to add tests to package a. If tests will be also in package a and tests will also import package b (because then need something implemented there) then we’ll end up with circular dependency which is forbidden. One way to bypass that is to put tests into separate package like a_tests. Then we need to import package a and reference every exported identifier with qualified identifier. To make our life easier we can import package a with dot :

and then reference exported identifiers from package a without package name (just like when tests were in the same package but non-exported identifiers aren’t accessible).

It’s impossible to import two packages using dot as package name if they’ve at least one exported identifier in common:

Import with blank identifier

Golang’s compiler will yell if package is imported and not used (source code):

Import with dot where all exported identifiers are added directly into importing file block fails as well while building — source code. The only variant which works is the one with blank identifier. It’s required to know what init functions are in order to understand why do we need to have import with blank identifier. Throughout introduction to init functions has been covered in one of previous stories:

I encourage to read it from top to bottom but in essence import like:

doesn’t require to use package math in importing file but init function(s) from imported package will be executed anyway (package and it dependencies will be initialized). It’s useful if we’re interested only in bootstrapping work done by imported package but we don’t reference any exported identifiers from it.

Compiling will fail if package is imported without blank identifier and not used at all.

Circular import

Go specification explicitly forbids circular imports — when package imports itself indirectly. The most obvious case is when package a imports package b and package b in turn imports package a:

An attempt to build any of these two packages ends up with an error:

Of course it can be more complex scenario like a → b → c → d → a where xy means that package x imports package y.

Package cannot import itself neither:

Compiling such package also gives an error: can’t load package: import cycle not allowed.

Clap 👏👏👏 below to help others discover this story. Please follow me here or on Twitter if you want to get updates about new posts or boost work on future stories.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Michał Łowicki

Michał Łowicki


Software engineer at Datadog, previously at Facebook and Opera, never satisfied.