Skip to content
Latchkey

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.

go build output
./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 clause

Common 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.

Terminal
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.

Go
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.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →