O'Reilly logo

Introducing Go by Caleb Doxsey

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required