Packages and visibility
A folder is a package, and capitalisation is your access control.
- Write a module with two packages and call one from the other.
- Explain how capitalisation decides what a package exports.
- Fix an
undefinederror caused by a lowercase name.
Three things you need before writing anything real
A package is a folder of Go files compiled together. It is how you split a growing program into pieces, and it is the only privacy Go has: capitalised names are visible outside the package, lowercase names are not.
JSON is the text format most web APIs send and receive. Go turns a struct into JSON and back using the field tags you met with structs, so most JSON work in Go is describing your types accurately.
A test is code that runs your code and fails loudly when the answer is wrong. Testing is part of the Go toolchain, not a library you pick: a file ending _test.go, a function starting Test, and go test.
A folder is a package
Every .go file declares a package, and every file in one folder must declare the same one. Visibility is decided by capitalisation: a name starting with a capital letter is exported and visible outside the package; a lowercase name is private to it. There is no public or private keyword.
package calc// Add is visible outside the package because it starts with a capital.func Add(a, b int) int { return a + b }// helper is private to this package.func helper() {}package mainimport ( "fmt" "example.com/calc-demo/calc" // module path + folder)func main() { fmt.Println(calc.Add(2, 3))}- Every file in one folder declares the same package name.
- Capitalised names are exported; lowercase ones are private to the package.
- The import path is the module path plus the folder.
- There is no
publicorprivatekeyword, and none is needed. - Once a module has many packages,
internal/folders and import cycles come into play: see Packages in code in the tooling section.
- 1Create a
calcpackage and call it frommain. - 2Use a blank import (
_ "net/http/pprof") and work out what it is for. - 3Alias an import and use the alias.