Resources

Import directly

Individual packages

import "fmt"
import "math"

Grouped imports

Note lack of comma.

import (
	"fmt"
	"math"
)

Nested imports

Top level

import "math"

math.Sqrt(7)

Next level

Get the rand submodule inside the math package.

import "math/rand"

rand.Intn(10)

Note you cannot use a submodule without importing it directly. This will give an error.

import "math"

math.rand.Int(10)

Though in other languages like Python, that is allowed.

Aliases

import f "fmt"

f.Println("Hello, World"!) 

Import into current namespace

This has some drawbacks, so avoid using this.

import . "fmt"

Println("Hello, World!")

Blank import

Avoid error on unused import by using an underscore.

import _ "os"