Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The import declaration in Go is a fundamental language declaration used to bind external packages to the current file’s namespace, enabling access to their exported identifiers (functions, types, variables, and constants). Import paths are resolved at compile time based on the active Go module (go.mod), the workspace, or the standard library.

Syntax

Imports can be declared individually or grouped into a factored block. The factored syntax is the idiomatic standard for multiple imports.
// Single import declaration
import "fmt"

// Factored (block) import declaration
import (
    "context"
    "fmt"
    "net/http"
)

Import Paths

The string literal provided to the import statement represents the import path, not necessarily the package name.
  • Standard Library: Paths are typically single or multi-level un-prefixed strings (e.g., "math", "crypto/sha256").
  • Module-based: Paths are string literals representing the module path and package subdirectory (e.g., "github.com/user/repo/pkg").

Import Modifiers

Go provides three modifiers that alter how an imported package is bound to the local file’s lexical scope.
import (
    "database/sql"                  // Standard
    json "github.com/goccy/go-json" // Local PackageName
    _ "github.com/lib/pq"           // Blank
    . "math"                        // Dot
)
  • Standard: The package’s exported identifiers are accessed using the declared package name as a prefix (e.g., sql.Open).
  • Local PackageName (name "path"): Overrides the default package name. This is required when resolving namespace collisions (e.g., importing two packages both named utils) or when a package name is non-idiomatic.
  • Blank (_ "path"): Instructs the compiler to load the package and execute its init() functions for side-effects, but does not bind any of its identifiers to the local namespace. This bypasses the strict unused-import compiler check.
  • Dot (. "path"): Injects all exported identifiers from the imported package directly into the current file’s lexical block. Identifiers can be accessed without a package prefix (e.g., calling Sin() instead of math.Sin()).

Compiler Rules and Constraints

  1. Strict Unused Imports: If a package is imported but none of its identifiers are referenced in the file, the Go compiler reports a compilation error (imported and not used).
  2. Exported Identifiers Only: An import only grants access to identifiers in the target package that begin with an uppercase letter. Lowercase identifiers remain unexported.
  3. No Cyclic Dependencies: Go strictly prohibits circular imports. If Package A imports Package B, Package B cannot import Package A (neither directly nor transitively). The compiler will fail the build with an import cycle not allowed error.
  4. File-Scoped: Import declarations are scoped strictly to the file in which they reside, not the package. If file1.go and file2.go belong to the same package and both require "fmt", both files must independently import it.
Master Go with Deep Grasping Methodology!Learn More