In the first code example, I get errors for the "if pr != nil" line:
for sup, _ := range supervisorToColor {
pr := emailToPerson[sup]
// The line below causes the compilation error:
// ./myprog.go:1046: missing condition in if statement
// ./myprog.go:1046: pr != nil evaluated but not used
if pr != nil
{
local := peopleOnFloor[pr.Email]
sp := &Super{pr, local}
names = append(names, sp)
}
}
If I comment out the nil check if statement, it compiles fine:
for sup, _ := range supervisorToColor {
pr := emailToPerson[sup]
// if pr != nil
// {
local := peopleOnFloor[pr.Email]
sp := &Super{pr, local}
names = append(names, sp)
// }
}
At first I was inclined to think it was some syntax error earlier in the code, but the fact that it works when I comment out the lines makes me think it's something else.
emailToPerson is of type map[string]*Person where Person is a struct
Thanks in advance. Apologies if this turns out to be something incredibly simple.
The open curly brace needs to be on the same line as the if
:
if pr != nil {
From the Go spec on semicolons:
The formal grammar uses semicolons ";" as terminators in a number of productions. Go programs may omit most of these semicolons using the following two rules:
When the input is broken into tokens, a semicolon is automatically inserted into the token stream immediately after a line's final token if that token is
- an identifier
- an integer, floating-point, imaginary, rune, or string literal
- one of the keywords
break
,continue
,fallthrough
, orreturn
- one of the operators and delimiters
++
,--
,)
,]
, or}
To allow complex statements to occupy a single line, a semicolon may be omitted before a closing "
)
" or "}
".
This means that your code was equivalent to:
if pr != nil;
{
// ...
}