Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. Go Dictionary
  3. Packages / import

Packages / import

Go code is organized into packages. Every file begins with a package declaration. To use another package, load it with import. Whether an identifier is exported (public) or unexported (private) is determined by its first letter — uppercase for exported, lowercase for unexported.

Syntax

// Package declaration. Write this at the top of every file.
package main

// Single import.
import "fmt"

// Multiple imports (grouping is recommended).
import (
    "fmt"
    "math"
    "strings"
)

// Import with an alias.
import f "fmt"  // Use as f.Println().

// Blank import: runs only the package's init function (avoids a compile error when the package is not otherwise used).
import _ "image/png"

// Dot import: use identifiers without the package name (not recommended).
import . "fmt"  // Write Println() directly.

// Identifiers starting with an uppercase letter are exported (accessible from other packages).
func PublicFunc() {}   // Callable from other packages.
func privateFunc() {}  // Only accessible within the same package.

Package Rules

ItemDescription
package declarationRequired at the top of every file. All files in the same directory must use the same package name.
main packageThe entry point for executable programs. A main() function is required.
Exported identifiersFunctions, types, variables, and constants whose names start with an uppercase letter are accessible from other packages.
Unused importsCause a compile error. Use _ when you need a side-effect-only import.
Circular importsNot allowed. If package A imports B and B imports A, the build will fail.
Internal packagesPackages inside an internal/ directory can only be imported by code rooted at the parent directory.

Sample Code

// File: main.go
package main

import (
    "fmt"
    "strings"

    // Using an alias.
    m "math"

    // Blank import: imported only for its side effects.
    _ "net/http/pprof"
)

// Greeting is an exported function accessible from other packages.
func Greeting(name string) string {
    return "Hello, " + name + "!"
}

// greet is an unexported function accessible only within this package.
func greet(name string) {
    fmt.Println(Greeting(name))
}

func main() {
    // Using the standard library.
    fmt.Println("=== Packages and Imports Example ===")

    // Using the strings package.
    s := "  Hello, Go!  "
    fmt.Println("Trim:", strings.TrimSpace(s))
    fmt.Println("Upper:", strings.ToUpper(s))

    // Using the math package via its alias.
    fmt.Printf("Pi: %.4f\n", m.Pi)
    fmt.Printf("Sqrt(2): %.4f\n", m.Sqrt(2))

    fmt.Println()

    // Calling the exported function.
    fmt.Println(Greeting("World"))

    // The unexported function can be called from within the same package.
    greet("Go")
}

Notes

Go's package system is simple and strict. Unused imports are compile errors, which keeps your code consistently tidy. Import paths are written as full paths starting from the module name (e.g., "github.com/user/repo/pkg").

Package names conventionally match their directory name, though this is not required. By convention, names are short, lowercase, and contain no underscores.

The dot import (import . "pkg") lets you omit the package name, but it makes it hard to tell which package an identifier belongs to, so it is not recommended in normal code.

For module dependency management, see Go Modules. For commonly used Go commands, see go build / run / test.

If you find any errors or copyright issues, please .