Skip to content
Justice Tech School

Packages, init and Imports

Every file belongs to a package. init functions run before main. Capitalised names are exported. A module is created with go mod init, and packages are imported by path.

Example
// go.mod
// module example.com/app
//
// mathutil/mathutil.go
package mathutil

func Double(n int) int { return n * 2 } // exported
func secret() {}                          // not exported

// main.go
// import "example.com/app/mathutil"
// fmt.Println(mathutil.Double(4))

Exercise

Start a module.

go mod  example.com/app

Quiz Test

Which names can other packages use?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.