Go "syntax error: unexpected ..." - Fix Parse Errors in CI
The Go parser hit a token it did not expect. The file is malformed - a missing or extra brace, an opening brace on the wrong line (Go inserts semicolons at line ends), or a stray character - so compilation fails before type checking.
What this error means
The build stops with syntax error: unexpected <token> or expected declaration, found <token>, pointing at a line. Nothing past the parse error compiles.
./main.go:11:1: syntax error: unexpected }, expecting expression
# or, from a brace on the next line:
./main.go:8:6: syntax error: unexpected newline, expecting { after if clauseCommon causes
Mismatched or misplaced braces
A missing }, an extra one, or an opening { placed on the next line (Go’s automatic semicolon insertion then breaks the statement) produces an unexpected token.
A stray or invalid character
A smart quote, a stray comma, or a non-ASCII character pasted into source confuses the parser.
How to fix it
Let gofmt locate and fix the structure
gofmt fails to format unparseable code and points at the offending line; once it formats cleanly the syntax is valid.
gofmt -l -e . # lists files with parse errors and why
gofmt -w .
go build ./...Keep opening braces on the same line
Go requires the { on the same line as if/for/func because of automatic semicolon insertion.
if ok { // correct
// ...
}
// not:
// if ok
// {How to prevent it
- Run
gofmt/go vet(or a formatter on save) so syntax errors surface immediately. - Keep braces on the conventional lines Go expects.
- Avoid pasting code from rich text that introduces smart quotes.