FAQ
Hi there,

I'm new to Go and I can't figure out an error I'm having.

I have this code (http://play.golang.org/p/WWQvFsc1or):

package main

func MyFunction(ids []uint32, fields map[string]string) interface{} {

data := struct {
Ids []uint32 `json:"ids"`
_ map[string]string
}{
ids,
fields,
}

return data
}

But when I compile I get the following error:

prog.go:5: cannot refer to blank field or method

What am I doing wrong? What does the message mean exactly? I understand
where the error comes from (I'm using the blank identifier), but I don't
understand why it doesn't work.



*Also, here's a bit of context as to why I'm trying to write such code:*
The actual code I'm trying to compile is more complicated, I stripped out
all the noise to make it easy to read. The actual code doesn't return an
interface{}. I did this in my simplified example to get the code to compile.

In the actual code, I'm trying to JSON encode some data and get the
following JSON string:

{"ids":[12345,12345],"somefieldsinthemap":"thevalueofthefield"}.

*Another digression:*
In my original code, I have written testing files. When I run go test, the
error I get is the following:

# github.com/xxx/xxx/blabla
/var/folders/hz/867wldps5hzfmtk5ygyv8p000000gn/T/go-build872258806/
github.com/xxx/xxx/blabla/_test/_obj_test/*blabla.go:176: cannot refer to
blank field or method*
FAIL github.com/xxx/xxx/blabla [build failed]

However, running go build returns:
# github.com/xxx/xxx/blabla
./*blabla.go:160: cannot refer to blank field or method*

Why are the lines reported (160 and 176) not the same?

Thank you for your help!
-Hugues

--
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

  • Hugues Alary at Oct 6, 2014 at 3:32 pm

    Addendum to my last question:

    In my original code, I have written testing files. When I run go test,
    the error I get is the following:
    I actually run go test *-cover*. Running go test, without the -cover flag,
    reports the error from the same line number.
    On Sun, Oct 5, 2014 at 8:32 PM, Hugues Alary wrote:

    Hi there,

    I'm new to Go and I can't figure out an error I'm having.

    I have this code (http://play.golang.org/p/WWQvFsc1or):

    package main

    func MyFunction(ids []uint32, fields map[string]string) interface{} {

    data := struct {
    Ids []uint32 `json:"ids"`
    _ map[string]string
    }{
    ids,
    fields,
    }

    return data
    }

    But when I compile I get the following error:

    prog.go:5: cannot refer to blank field or method

    What am I doing wrong? What does the message mean exactly? I understand
    where the error comes from (I'm using the blank identifier), but I don't
    understand why it doesn't work.



    *Also, here's a bit of context as to why I'm trying to write such code:*
    The actual code I'm trying to compile is more complicated, I stripped out
    all the noise to make it easy to read. The actual code doesn't return an
    interface{}. I did this in my simplified example to get the code to compile.

    In the actual code, I'm trying to JSON encode some data and get the
    following JSON string:

    {"ids":[12345,12345],"somefieldsinthemap":"thevalueofthefield"}.

    *Another digression:*
    In my original code, I have written testing files. When I run go test,
    the error I get is the following:

    # github.com/xxx/xxx/blabla
    /var/folders/hz/867wldps5hzfmtk5ygyv8p000000gn/T/go-build872258806/
    github.com/xxx/xxx/blabla/_test/_obj_test/*blabla.go:176: cannot refer to
    blank field or method*
    FAIL github.com/xxx/xxx/blabla [build failed]

    However, running go build returns:
    # github.com/xxx/xxx/blabla
    ./*blabla.go:160: cannot refer to blank field or method*

    Why are the lines reported (160 and 176) not the same?

    Thank you for your help!
    -Hugues


    --
    Hugues ALARY

    --
    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.
  • Ian Lance Taylor at Oct 6, 2014 at 4:04 pm

    On Sun, Oct 5, 2014 at 8:32 PM, Hugues Alary wrote:
    I have this code (http://play.golang.org/p/WWQvFsc1or):

    package main

    func MyFunction(ids []uint32, fields map[string]string) interface{} {

    data := struct {
    Ids []uint32 `json:"ids"`
    _ map[string]string
    }{
    ids,
    fields,
    }

    return data
    }

    But when I compile I get the following error:

    prog.go:5: cannot refer to blank field or method

    What am I doing wrong? What does the message mean exactly? I understand
    where the error comes from (I'm using the blank identifier), but I don't
    understand why it doesn't work.
    You are trying to set the value of a _ field in a struct. You can't
    do that. You can't refer to a blank field. You can't do it in a
    composite literal either. The only use for a blank field in a struct
    is for padding; you can't set the value, or refer to it.

    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.
  • Hugues Alary at Oct 6, 2014 at 5:37 pm
    Hi Ian.

    Thanks for your answer. I somehow thought I could do that. Problem solved
    then!

    sent from my phone; not a reason to pardon my typos.
    On Oct 6, 2014 9:04 AM, "Ian Lance Taylor" wrote:
    On Sun, Oct 5, 2014 at 8:32 PM, Hugues Alary wrote:

    I have this code (http://play.golang.org/p/WWQvFsc1or):

    package main

    func MyFunction(ids []uint32, fields map[string]string) interface{} {

    data := struct {
    Ids []uint32 `json:"ids"`
    _ map[string]string
    }{
    ids,
    fields,
    }

    return data
    }

    But when I compile I get the following error:

    prog.go:5: cannot refer to blank field or method

    What am I doing wrong? What does the message mean exactly? I understand
    where the error comes from (I'm using the blank identifier), but I don't
    understand why it doesn't work.
    You are trying to set the value of a _ field in a struct. You can't
    do that. You can't refer to a blank field. You can't do it in a
    composite literal either. The only use for a blank field in a struct
    is for padding; you can't set the value, or refer to it.

    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.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedOct 6, '14 at 3:32p
activeOct 6, '14 at 5:37p
posts4
users2
websitegolang.org

2 users in discussion

Hugues Alary: 3 posts Ian Lance Taylor: 1 post

People

Translate

site design / logo © 2021 Grokbase