Packages and imports

Contents
One directory, one package
Import paths
Alias and dot imports
init order

One directory, one package

A package is a directory of Go files that share one package clause. The module path in go.mod gives every package a full address, so other files import it by that path instead of by file name.

myshop/
go.mod
math/add.go
main.go

The library file below declares its package and one exported function. Save it as math/add.go so its address becomes the module path plus math.

package math func Add(a, b int) int { return a + b }

Only names starting with a capital letter cross the package border, which is why Add is visible and a helper named add would stay private.

Import paths

An import path names a package, not a file. Standard library paths are short words such as fmt and strings, while your own packages start with the module path, for example example.com/shop/math.

package main import ( "fmt" "strings" ) func main() { fmt.Println(strings.ToUpper("go")) fmt.Println(strings.HasPrefix("gopher", "go")) }

Group imports in one parenthesized block, with standard packages first and external ones after a blank line. The go tool complains about missing and unused imports, so the block always mirrors what the file truly uses.

Alias and dot imports

When two packages export the same name, import one under a short alias and keep the code unambiguous. A single underscore blanks an import used only for setup, and a dot nickname drops the qualifier entirely, so save the dot for small test files.

package main import ( "fmt" s "strings" ) func main() { shout := s.ToUpper("hello") fmt.Println(shout) }

Here strings answers to s, so s.ToUpper reads clearly next to fmt.Println. Prefer meaningful aliases over single letters in large files, where the reader cannot see the import block and the call at once.

init order

Package variables initialize first, then each init function runs, and only then does main start. Every imported package completes its own chain before its importer, so the whole program wakes up from the leaves inward.

package main import "fmt" func note(step string) string { fmt.Println(step) return step } var started = note("package variable ready") func init() { fmt.Println("init runs") } func main() { fmt.Println("main runs") }

package variable ready
init runs
main runs

Keep init small and predictable, because it runs before main and hides work from the reader. Heavy setup belongs in an explicit function that main calls on purpose.

Next: The go toolchain

Article author: Arthur Isaev

Related articles
Golang
Go course: zero to hero in 50 lessons
What is Go and where it runs
Install Go and check the version
Your first Go program
Modules with go mod
Variables
Constants
Types
Numbers
Strings
Conditions with if
Switch in Go
Loops with for
Arrays in Go
Slices in Go
Maps in Go
Functions in Go
Errors as values
Pointers in Go
Structs in Go
Methods in Go
Interfaces in Go
Embedding instead of inheritance
Generics in Go
Packages and imports
The go toolchain
Testing with go test
Advanced errors
defer in Go
Strings, bytes and runes
Time in Go
JSON
Files and IO
HTTP servers
HTTP clients
Context
Goroutines
Channels
select and sync
CLI: args, flags, env
Logging
Regular expressions
Sorting
Concurrency patterns
Databases with database/sql
Benchmarks and profiling
Toolchain and CI
Project layout
Capstone: wordfreq CLI
Capstone: JSON API server
Hero roadmap: the whole course on one page
Install Go on Windows 11
Install Go on Ubuntu
Install Go on Rocky Linux
Install Go on macOS
Install Go on FreeBSD
Go in Docker

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.ru
Write to info@urn.su if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: