Skip to content
Tutorials Center
My learning

Topics

Go Reference ยท Keywords and Statements

The import declaration

Reference for Go import: single and grouped imports, aliases, blank imports for side effects, dot imports, unused import errors and ordering conventions.

import makes the exported names of another package available in the current file. Imports are per file, not per package: each file lists the packages it uses. After the clause package, the import declarations come first, before any other declaration.

An import names a package by its import path, a string such as "fmt" or "math/rand/v2". In code you refer to the package by its package name, which is the name in the package clause of that package (usually the last path element).

Go
import (
	"fmt"
	str "strings"
)

Try it

Go Grouped imports and an alias

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Go Blank and dot imports

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Syntax

Go
import "path"
import alias "path"
import _ "path"
import . "path"
import (
	"path1"
	alias "path2"
)
Form Description
import "p" Use the package under its own name.
import a "p" Use it under the name a. Needed to resolve clashes (two packages called rand) or to shorten long names.
import _ "p" Import for side effects only: the package is initialized (its init functions run) but none of its names is accessible. Typical for database drivers and image decoders.
import . "p" Merge the exported names of p into the file scope. Discouraged outside tests; it hides where names come from.

Errors and edge cases:

  • An imported but unused package is a compile error: "os" imported and not used. Use the blank form if you really want only side effects.
  • Importing the same path twice with the same name in one file is an error; using it twice under different aliases is allowed but pointless.
  • An import cannot be conditional. Platform differences are handled with build constraints (//go:build linux) on whole files.
  • Import cycles between packages are rejected.

Usage notes

  • Grouping and order: put standard-library imports first, a blank line, then third-party and your own. gofmt sorts lines within each group; tools like goimports add and remove imports for you.
  • Versioned paths: a module at major version 2 or higher ends its path with /v2, /v3, and so on. The package name stays the same: import "math/rand/v2" is used as rand.
  • Local names can shadow a package. A variable called url hides the imported net/url inside its scope. Pick different variable names, or alias the import.
  • Standard library only on this site's runner: examples are a single file with no go.mod, so third-party paths cannot be resolved there.
  • The package, not the file, is the unit of dependency: importing a package pulls in everything it imports, and all of it is initialized before your main starts.
  • Internal packages (.../internal/...) can be imported only from the tree rooted at the directory containing internal.