Chapter 8. Packages
Go was designed to be a language that encourages good software engineering practices. An important part of high-quality software is code reuse—embodied in the principle “Don’t Repeat Yourself.”
As we saw in Chapter 6, functions are the first layer we utilize to allow code reuse. Go also provides another mechanism for code reuse: packages. Nearly every program we’ve seen so far included this line:
import
"fmt"
fmt
is the name of a package that includes a variety of functions related to formatting and output to the screen. Bundling code in this way serves three purposes:
-
It reduces the chance of having overlapping names, and in turn keeps our function names short and succinct.
-
It organizes code so that it’s easier to find code you want to reuse.
-
It speeds up the compiler by only requiring recompilation of smaller chunks of a program. Although we use the package
fmt
, we don’t have to recompile it every time we change our program.
The Core Packages
Instead of writing everything from scratch, most real-world programming depends on our ability to interface with existing libraries. This chapter will take a look at some of the most commonly used packages included with Go.
First, a word of warning: although some of these libraries are fairly obvious (or have been explained in previous chapters), many of the libraries included with Go require specialized, domain-specific knowledge (e.g., cryptography). It is beyond the scope of this book to explain these ...
Get Introducing Go now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.