FAQ
Hello,

Here is the code I try to compile:
package main

type FOO struct {
bar bool
}

func main() {
if a := FOO{bar:true}; a.bar {
// ...
}
}

The error message is: "type FOO is not an expression"
And I have to add a pair of braces around the struct initializer to make it
compile:

package main

type FOO struct {
bar bool
}

func main() {
if a := *(*FOO{bar:true}*)*; a.bar {
// ...
}
}

Is it correct behavior?

*Thanks,*
Hailiang

Search Discussions

  • Minux at Sep 1, 2012 at 8:12 am

    On Sat, Sep 1, 2012 at 4:07 PM, Hailiang wrote:

    Hello,

    Here is the code I try to compile:
    package main

    type FOO struct {
    bar bool
    }

    func main() {
    if a := FOO{bar:true}; a.bar {
    // ...
    }
    }

    The error message is: "type FOO is not an expression"
    And I have to add a pair of braces around the struct initializer to make
    it compile:

    package main

    type FOO struct {
    bar bool
    }

    func main() {
    if a := *(*FOO{bar:true}*)*; a.bar {
    // ...
    }
    }

    Is it correct behavior?
    it is.

    quoting from http://golang.org/ref/spec#Composite_literals:

    A parsing ambiguity arises when a composite literal using the TypeName form
    of the LiteralType appears between the
    keyword<http://golang.org/ref/spec#Keywords> and
    the opening brace of the block of an "if", "for", or "switch" statement,
    because the braces surrounding the expressions in the literal are confused
    with those introducing the block of statements. To resolve the ambiguity in
    this rare case, the composite literal must appear within parentheses.


    if x == (T{a,b,c}[i]) { … }
    if (x == T{a,b,c}[i]) { … }
  • Hailiang Wang at Sep 1, 2012 at 8:24 am
    Fast response! Thanks very much!

    Hailiang
    On Sat, Sep 1, 2012 at 4:11 PM, minux wrote:


    On Sat, Sep 1, 2012 at 4:07 PM, Hailiang wrote:

    Hello,

    Here is the code I try to compile:
    package main

    type FOO struct {
    bar bool
    }

    func main() {
    if a := FOO{bar:true}; a.bar {
    // ...
    }
    }

    The error message is: "type FOO is not an expression"
    And I have to add a pair of braces around the struct initializer to make
    it compile:

    package main

    type FOO struct {
    bar bool
    }

    func main() {
    if a := *(*FOO{bar:true}*)*; a.bar {
    // ...
    }
    }

    Is it correct behavior?
    it is.

    quoting from http://golang.org/ref/spec#Composite_literals:

    A parsing ambiguity arises when a composite literal using the TypeName
    form of the LiteralType appears between the keyword<http://golang.org/ref/spec#Keywords> and
    the opening brace of the block of an "if", "for", or "switch" statement,
    because the braces surrounding the expressions in the literal are confused
    with those introducing the block of statements. To resolve the ambiguity in
    this rare case, the composite literal must appear within parentheses.

    if x == (T{a,b,c}[i]) { … }
    if (x == T{a,b,c}[i]) { … }

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedSep 1, '12 at 8:07a
activeSep 1, '12 at 8:24a
posts3
users2
websitegolang.org

2 users in discussion

Hailiang Wang: 2 posts Minux: 1 post

People

Translate

site design / logo © 2022 Grokbase