Chapter 13

Using and Creating Packages in Go

IN THIS CHAPTER

Bullet Knowing what a package is and how it works

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

Remember All files in a package must be in the same directory, and all package names must all be in lowercase.

Let's take a look at an example. Suppose you have a directory ...

Get Go Programming Language For Dummies 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.