Packages and Modules in Go
Packages and Modules is where your code grows beyond one file.
In this chapter, you will:
- split code into packages, and choose what other packages can see
- import packages in four different ways
- manage a project and its dependencies with Go modules
- meet the most useful packages in Go's standard library
What each lesson covers:
- Packages and Modules: How Go organises code into packages, exported and unexported names, the four kinds of import, Go modules with go.mod, go.sum and go mod tidy, managing dependencies, and the key standard packages.
This chapter has 1 lesson. Plan on about 25 minutes, with 3 exercises to check what you learned. Every example is real Go code: change it, run it again and compare the output, which is the fastest way to learn how each feature behaves.
Simple lesson: finish this chapter and you can organise a real Go project.
Lessons in this section
- Packages and Modules
How Go organises code into packages, exported and unexported names, the four kinds of import, Go modules with go.mod, go.sum and go mod tidy, managing dependencies, and the key standard packages.
- Lay out and name packages, and export the right names
- Import packages normally, with an alias, blank or dot
- Create a module and explain go.mod and go.sum
- Install, update and remove modules with go get and go mod tidy