FAQ
Hi,

I experienced a weird compiling error in my go program. I have such code
snippet to reproduce it:

https://play.golang.org/p/0VSh02ng7p

package main

import "fmt"

type T struct {
Field int
}

func (t T) Next() T {
return T{t.Field+1}
}

func main() {
/* Got error at next for statement:
prog.go:14: syntax error: unexpected { at end of statement
prog.go:17: syntax error: 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 < 10; t2 = t2.Next() {
fmt.Println(t2)
}
}

I can understand the compiler may consider the first '{' be the beginning
of a block. Do we consider it a bug? If the behavior is intentional, can we
output less confusing error message?

Thanks.
Sheng

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Ian Lance Taylor at May 27, 2016 at 5:22 am

    On Thu, May 26, 2016 at 7:20 PM, Sheng Yu wrote:
    I experienced a weird compiling error in my go program. I have such code
    snippet to reproduce it:

    https://play.golang.org/p/0VSh02ng7p

    package main

    import "fmt"

    type T struct {
    Field int
    }

    func (t T) Next() T {
    return T{t.Field+1}
    }

    func main() {
    /* Got error at next for statement:
    prog.go:14: syntax error: unexpected { at end of statement
    prog.go:17: syntax error: 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 < 10; t2 = t2.Next() {
    fmt.Println(t2)
    }
    }

    I can understand the compiler may consider the first '{' be the beginning of
    a block. Do we consider it a bug? If the behavior is intentional, can we
    output less confusing error message?
    Search for "ambiguity" in https://golang.org/ref/spec#Composite_literals .

    You need to write
         for t := (T{1}); t.Field < 10; t = t.Next() {

    Yes, a better error message would be nice if possible. It's not easy,
    though, as the parse failure is pretty far from the code that needs to
    change.

    Ian

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Sheng Yu at Jun 1, 2016 at 1:21 am

    On Thu, May 26, 2016 at 10:22 PM, Ian Lance Taylor wrote:
    On Thu, May 26, 2016 at 7:20 PM, Sheng Yu wrote:

    I experienced a weird compiling error in my go program. I have such code
    snippet to reproduce it:

    https://play.golang.org/p/0VSh02ng7p

    package main

    import "fmt"

    type T struct {
    Field int
    }

    func (t T) Next() T {
    return T{t.Field+1}
    }

    func main() {
    /* Got error at next for statement:
    prog.go:14: syntax error: unexpected { at end of statement
    prog.go:17: syntax error: 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 < 10; t2 = t2.Next() {
    fmt.Println(t2)
    }
    }

    I can understand the compiler may consider the first '{' be the
    beginning of
    a block. Do we consider it a bug? If the behavior is intentional, can we
    output less confusing error message?
    Search for "ambiguity" in https://golang.org/ref/spec#Composite_literals .

    You need to write
    for t := (T{1}); t.Field < 10; t = t.Next() {

    Yes, a better error message would be nice if possible. It's not easy,
    though, as the parse failure is pretty far from the code that needs to
    change.

    Ian
    Thanks for the pointer. It's good to have it in the spec already.

    Sheng

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedMay 27, '16 at 5:10a
activeJun 1, '16 at 1:21a
posts3
users2
websitegolang.org

2 users in discussion

Sheng Yu: 2 posts Ian Lance Taylor: 1 post

People

Translate

site design / logo © 2021 Grokbase