Any idea why this struct expression in for loop initializer makes syntax error in compile-time? Pointer to struct works fine in this case but ofc I need local variable like bellow. Thanks for advices!
type Request struct {
id int
line []byte
err error
}
go func() {
for r := Request{}; r.err == nil; r.id++ {
r.line, r.err = input.ReadSlice(0x0a)
channel <- r
}
}()
Simplifying you code:
Gives compile time error:
This construct is ambiguous to parse. The opening brace
'{'
is not obvious whether it is part of a composite literal or the opening brace of thefor
statement itself (thefor
block).You can make it obvious by using parentheses around the composite literal (as the error suggests):