Packages

View saved

One package per folder

All .go files in a directory share the same package name. Start simple: one folder, package main.

Import what you use

Unused imports are a compile error. Import paths look like quoted strings.

package main

import (
	"fmt"
	"strings"
)

func main() {
	fmt.Println(strings.ToUpper("go"))
}

Alias an import

Use an alias when two packages would collide or a name is awkward.

import str "strings"

func demo() {
	_ = str.TrimSpace("  hi  ")
}

Package habits

  • Keep package names short and lowercase
  • Export names by capitalizing them (Println is exported)
  • Unexported names start with a lowercase letter
  • Prefer the standard library before adding dependencies

Comments

One comment per signed-in account. Comments are saved with this page’s URL.