Chapter 4. bundles of code: Packages
It’s time to get organized. So far, we’ve been throwing all our code together in a single file. As our programs grow bigger and more complex, that’s going to quickly become a mess.
In this chapter, we’ll show you how to create your own packages to help keep related code together in one place. But packages are good for more than just organization. Packages are an easy way to share code between your programs. And they’re an easy way to share code with other developers.
Different programs, same function
We’ve written two programs, each with an identical copy of a function, and it’s becoming a maintenance headache...
On this page, we’ve got a new version of our pass_fail.go program from Chapter 2. The code that reads a grade from the keyboard has been moved to a new getFloat function. getFloat returns the floating-point number the user typed, unless there’s an error, in which case it returns 0 and an error value. If an error is returned, the program reports it and exits; otherwise, it reports whether the grade is passing or failing, as before.
On this page, we’ve got a new tocelsius.go program that lets the user type a temperature in the Fahrenheit measurement system and converts it to the Celsius system.
Notice that the getFloat function ...
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