10. Reading error messages — Homework solutions
The .go solution files are in
exercises/10/homework/solutions/. These problems practise
the loop: run, read the first error, fix one thing, run
again.
Problem 1 — Name the error
Problem. Identify the error, then fix it.
How to think about it. Run the file, read the message, and match it to one of the four errors: newline in string, undefined, not used, or the brace error.
Worked solution. The starter is:
package main
import "fmt"
func main() {
fmt.Println("Hello,", Keiko)
}Running it prints:
# command-line-arguments
exercises\10\homework\01-name-the-error.go:10:24: undefined: Keiko
Without quotes, Keiko is a name, and nothing by that
name exists. It was meant to be text. A fix:
package main
import "fmt"
func main() {
fmt.Println("Hello,", "Keiko")
}
// error: undefined: KeikoProblem 2 — Fix the quote
Problem. Repair an unfinished string. Go reports:
# command-line-arguments
exercises\10\homework\02-fix-the-quote.go:8:39: newline in string
exercises\10\homework\02-fix-the-quote.go:8:39: syntax error: unexpected newline in argument list; possibly missing comma or )
Worked solution.
package main
import "fmt"
func main() {
fmt.Println("A wizard is never late.")
}The only change is the missing closing " before the
).
Common mistakes.
- Adding the quote after the
). The closing quote goes at the end of the text, before the closing parenthesis. Otherwise the)becomes part of the string, and Go reportssyntax error: unexpected newline in argument list; possibly missing comma or ).
Problem 3 — Fix the typo
Problem. A misspelled function name causes an
undefined: error.
Worked solution. The starter has
fmt.Prinln("Fixed!"). The message names the culprit:
# command-line-arguments
exercises\10\homework\03-fix-the-typo.go:8:6: undefined: fmt.Prinln
Fix the spelling:
package main
import "fmt"
func main() {
fmt.Println("Fixed!")
}Common mistakes.
- "Fixing" it by inventing a different name.
fmtonly has the names it has. When the message saysfmt.Prinln, the fix is almost always the real function spelled correctly —fmt.Println.
Challenge — Three in a row
Problem. Three mistakes, one of each kind, fixed one at a time.
How to think about it. Run, fix the first error, and run again. The starter is:
package main
import "fmt"
func main()
{
fmt.Println("Starting up)
fmt.Prinln("Working")
fmt.Println("done")
}The first run shows two of the three mistakes:
# command-line-arguments
exercises\10\homework\04-three-in-a-row.go:10:1: syntax error: unexpected semicolon or newline before {
exercises\10\homework\04-three-in-a-row.go:11:27: newline in string
Move the { up and run again. The file is one line
shorter now, so the line numbers change:
# command-line-arguments
exercises\10\homework\04-three-in-a-row.go:10:27: newline in string
exercises\10\homework\04-three-in-a-row.go:10:27: syntax error: unexpected newline in argument list; possibly missing comma or )
Add the closing " and run again. Only now can Go check
the names:
# command-line-arguments
exercises\10\homework\04-three-in-a-row.go:11:6: undefined: fmt.Prinln
Fix the spelling for the last pass. The loop, working as intended.
Worked solution.
package main
import "fmt"
func main() {
fmt.Println("Starting up")
fmt.Println("Working")
fmt.Println("done")
}It prints:
Starting up
Working
done
Common mistakes.
- Fixing all three at once and losing track of which change did what. One error, one fix, run again — even when you think you see them all.
Done?
That is the end of Part 2. You can install Go and run a program,
print exactly what you want, comment your code, and read Go's errors.
The Part 2 mini-project — the ASCII Name Banner — puts your
fmt.Println skills to work building a picture out of text.
Then Part 3 digs into variables, strings, numbers, and input.