What a syntax error is
Every programming language has grammar rules, just as English does. A syntax error is a mistake in that grammar: a missing quote, a missing parenthesis, a misplaced brace. Go reads your whole program before running it, and if the grammar is broken, it can't make sense of the code. The program does not compile, and nothing runs, not even the lines above the mistake.
That strictness helps you. The compiler stops at the problem and tells you where it is, instead of guessing what you meant.
Syntax and examples
A string with a missing quote
A string starts with a double quote and must end with one on the same line. Here the closing quote is missing:
greeting := "Hey there!
Go reads from the opening quote to the end of the line, finds no closing quote, and reports newline in string.
A call with a missing parenthesis
Every opening parenthesis needs a closing partner. Here the call to fmt.Println is never closed:
fmt.Println("My name is Andy"
The compiler reaches the end of the line while still waiting for more arguments or the closing ), and reports syntax error: unexpected newline in argument list; possibly missing comma or ).
Reading a compiler message
Go's messages start with the line and column where the compiler noticed the problem. For this program:
package main
import "fmt"
func main() {
fmt.Println("Hello!"
fmt.Println("I love coding!")
}
the compiler prints:
syntax error: unexpected newline in argument list; possibly missing comma or )
The 6:22 means line 6, column 22, which is right after "Hello!". The reported spot is where the compiler gave up, which is usually the mistake itself or the line just after it. When you see an error, read the message, go to that line and check the quotes and parentheses on it and on the line above.
More than one syntax error can be reported at once. If both mistakes from this lesson are in the same program, the compiler lists one message for each line.
Common mistakes and gotchas
- Missing closing quote.
fmt.Println("Welcome!)looks closed, but the)is now inside the string, and the quote that ends it is missing. - Missing closing parenthesis.
fmt.Println("Let's code!"has a complete string but an unclosed call. - Single quotes around text.
fmt.Println('I love coding!')is also an error: single quotes are for one character, so Go reportsmore than one character in rune literal. - Two strings in a row.
fmt.Println("a" "b")is missing a comma or a+between them. - Mismatched pairs. Quotes, parentheses and braces come in pairs. When you add one half, add the other half right away.
- Fixing the wrong thing. A syntax error is not a logic error. The code can look sensible and still be rejected. Fix the grammar first; then check that the output is what you wanted.
The exercise
The starting code has two print statements. Both are meant to print a message, but each has one small syntax error, so the program is rejected:
fmt.Println("Hello!"
fmt.Println("I love coding!)
- The first line has the complete string
"Hello!", but the call is never closed. Add the closing parenthesis after the string. - The second line has the opening quote but no closing quote, so the string swallows the
). Move the quote: put it right aftercoding!and before the parenthesis.
fmt.Println("Hello!")
fmt.Println("I love coding!")
The program then prints two lines, Hello! and I love coding!. Fix both lines before running it. With one error left, the program still won't compile.
Key takeaways
- A syntax error breaks Go's grammar, so the program does not compile and nothing runs.
- Strings need a closing double quote on the same line; calls need a closing parenthesis.
- The compiler's message gives the line and column where it noticed the problem; check that line and the one above it.
- Close every quote, parenthesis and brace as soon as you open it.