O'Reilly logo

Head First Go by Jay McGavren

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

4 packages

Bundles of Code

Images

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 exists, otherwise it reports whether the grade is passing or failing, as before.

Images

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 in tocelsius.go is ...

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