This is a creation in Article, where the information may have evolved or changed.
(Go mailing list problem record 2)
Compiling error when using struct literal on for loop
Compile the problem, the code is as follows, the code inside the comment is the compile error
PackageMainimport"FMT"typet struct {Field int}func (T T) Next () t {returnT{t.field+1}}func Main () {/* GotErrorAtNext forStatement:prog.go: -: syntaxError: unexpected {atEndof statement Prog.go: -: syntaxError: Non-declaration statement outside function body for t : = t{1}; T.field < 10; t = t.next() {FMT. Println (t)} *//This is fine t2: = t{1} for; T2. Field <Ten; t2 = T2. Next () {fmt. Println (T2)}}
Although it may be written in theory and need support, but this compilation problem is not serious to go language has to be corrected immediately, and for parsing, there is really a lot of difficulty here.
Of course there are circumvention methods, the code is written so that you can.
func main() { for t := (T{1}); t.Field < 10; t = t.Next() { fmt.Println(t) } }