Code
Refactor main.go — printing becomes functions
Keep your three variables exactly as they are — we're not touching them. The change is where the printing lives: pull it out of main into small, named functions.
Add these three functions below main:
// printHeader prints the app title line.
func printHeader() {
fmt.Println("App:", appName)
}
// formatItem BUILDS the one-line summary and returns it (prints nothing).
func formatItem(title string, pages int, read bool) string {
return fmt.Sprintf("Book: %s | pages: %d | read: %v", title, pages, read)
}
// printItem calls formatItem and prints the result.
func printItem(title string, pages int, read bool) {
fmt.Println(formatItem(title, pages, read))
}
Then replace the two print lines in main with two calls:
printHeader()
printItem(title, pages, read)
Why split formatItem from printItem? Because formatItem returns the line instead of printing it — so later you can reuse it (e.g. writing the line to a file). printItem just prints what formatItem built. All the logic moved out of main.
Verify. The output must be exactly the same as in lesson 2:
App: Book list
Book: The Go Programming Language | pages: 380 | read: false
Tip. This is the heart of refactoring: a good refactor does not change the program's behavior — only the shape of the code. If the output changed, you broke something.