Published on

Golang Error: syntax error: unexpected else, expecting }

Authors

Today I was working with some Go code and got the following error:

syntax error: unexpected else, expecting }

My code looked like the below:

if someCondition {
//
} else if someOtherCondition {
//
}
else {
//
}

This error was caused by the else not being on the same line as the last brace of the else if. Go it turns out is very pedantic about where elses are placed:

if someCondition {
//
} else if someOtherCondition {
//
} else {
//
}