Chapter 13
Using and Creating Packages in Go
IN THIS CHAPTER
Knowing what a package is and how it works
Using packages created by third parties
Go uses the concept of packages to better organize code for reusability and readability. In earlier chapters, I show you how to use some of the built-in packages like fmt, strconv, math, and time in your Go application. In this chapter, I dig into the topic of packages in more detail. I show you how to create your own packages for your own use, as well as create packages for sharing. Finally, I explain how to install third-party packages on your system.
Working with Packages
If you've read the earlier chapters in this book, you know that Go applications always have this first statement:
package main
Go organizes code into units call packages. A package is made up of a collection of files. The main package is a special package that contains the main() function, which makes the main package an executable program. The main() function serves as the entry point to your application.
Let's take a look at an example. Suppose you have a directory ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access
All files in a package must be in the same directory, and all package names must all be in lowercase.