ZetCode

Golang import keyword

last modified May 7, 2025

This tutorial explains how to use the import keyword in Go. We'll cover package management basics with practical examples of importing packages.

The import statement makes exported identifiers from other packages available in the current file. It's essential for code organization and reuse.

In Go, import can reference standard library packages, third-party modules, and local packages. Proper imports are crucial for building modular and maintainable applications.

Basic import statement

The simplest form imports a single package. This example demonstrates importing the fmt package for formatted I/O operations.

basic_import.go
package main

import "fmt"

func main() {
    fmt.Println("Hello, World!")
}

The import "fmt" statement gives access to all exported functions from the fmt package. We then use fmt.Println to output text to the console.

Multiple imports with separate statements

Multiple packages can be imported using separate import statements. This approach is clear but verbose for many imports.

multiple_imports.go
package main

import "fmt"
import "math"
import "time"

func main() {
    fmt.Println("Current time:", time.Now())
    fmt.Println("Square root of 16:", math.Sqrt(16))
}

Each import statement brings in a different package. The code uses functions from all three imported packages to demonstrate their availability.

Grouped import statement

Go supports grouped imports for better readability. This is the preferred style when importing multiple packages.

grouped_imports.go
package main

import (
    "fmt"
    "math"
    "time"
)

func main() {
    fmt.Println("Pi:", math.Pi)
    fmt.Println("Weekday:", time.Now().Weekday())
}

The grouped syntax uses parentheses to enclose all import paths. This format is cleaner and easier to maintain than separate import statements.

Import with alias

Packages can be imported with custom aliases to avoid naming conflicts or provide shorter references.

alias_import.go
package main

import (
    "fmt"
    m "math"
)

func main() {
    fmt.Println("Cosine of 0:", m.Cos(0))
    fmt.Println("Square root of 9:", m.Sqrt(9))
}

The m "math" syntax creates an alias for the math package. We then use m instead of math to access its functions, demonstrating alias usage.

Dot import

Dot imports allow accessing package members without qualifiers. This is generally discouraged except in specific cases like testing.

dot_import.go
package main

import (
    "fmt"
    . "math"
)

func main() {
    fmt.Println("Pi:", Pi)
    fmt.Println("Square root of 25:", Sqrt(25))
}

The . "math" import makes all math package identifiers available directly. Note that this can cause naming conflicts and should be used sparingly.

Blank import

Blank imports execute package initialization without making its exports available. This is used for side effects like database drivers.

blank_import.go
package main

import (
    "fmt"
    _ "image/png"
)

func main() {
    fmt.Println("PNG decoder registered")
}

The _ "image/png" import registers the PNG decoder without exposing its functions. The main function demonstrates that the program runs while the import's side effect takes place.

Importing local packages

Local packages are imported using module paths. This example shows importing a package from the same module.

local_import.go
package main

import (
    "fmt"
    "example.com/mymodule/mypackage"
)

func main() {
    fmt.Println("Using local package:")
    mypackage.MyFunction()
}

The import path "example.com/mymodule/mypackage" references a local package. The code assumes proper module setup in the go.mod file.

Source

Go language specification

This tutorial covered the import keyword in Go with practical examples of package management in various scenarios.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Golang tutorials.