Struct field, error and non-name xyz on left side of :=

3,508 views
Skip to first unread message

Lukáš Zapletal

unread,
Dec 30, 2012, 11:11:15 AM12/30/12
to golan...@googlegroups.com
Hello all,

I know this error has been discussed here many times, but I want to focus on one particular scenario:

type Cache struct {
  file os.File
  // ...
}

cache := new(Cache)
cache.file, err := os.Open(filename) // here the error from %subj%

Now, I would like to know, what is the best approach here - I want Go to determine Error type for me while I still want to initialize both on the same line. What is the best practice here?

Thanks and Happy New Year!

LZ

minux

unread,
Dec 30, 2012, 11:32:44 AM12/30/12
to Lukáš Zapletal, golan...@googlegroups.com
those two goals can't be met at the same time. 

either you give up the first one (the type of err is error, and it's well-known):
var err error
cache := new(Cache)
cache.file, err = os.Open(filename)

or you give up the the 2nd one:
cache := new(Cache)
f, err := os.Open(filename)
cache.file = f

btw, i suggest you write a constructor function for type Cache, for example
func NewFromFile(filename string) *Cache

jaspe...@kreuzwerker.de

unread,
Nov 28, 2013, 9:04:46 AM11/28/13
to golan...@googlegroups.com, Lukáš Zapletal
Hmmm, I've just run into this same problem myself of assigning using ':=' to a struct field. 

Has this really been discussed many times before? I can only find one other thread referring to this issue, which ended in someone saying it might be a good addition to add later: https://groups.google.com/forum/#!searchin/golang-nuts/struct$20field$20non-name/golang-nuts/ItYSNKqt_kA/NbDaqs_4YK0J

Is it possible to generate a little more discussion on why this would not be a good addition? It seems like a nice convenience to me...

Jan Mercl

unread,
Nov 28, 2013, 10:59:54 AM11/28/13
to jaspe...@kreuzwerker.de, golang-nuts, Lukáš Zapletal
On Thu, Nov 28, 2013 at 3:04 PM, <jaspe...@kreuzwerker.de> wrote:
> Hmmm, I've just run into this same problem myself of assigning using ':=' to
> a struct field.
>
> Has this really been discussed many times before? I can only find one other
> thread referring to this issue, which ended in someone saying it might be a
> good addition to add later:
> https://groups.google.com/forum/#!searchin/golang-nuts/struct$20field$20non-name/golang-nuts/ItYSNKqt_kA/NbDaqs_4YK0J
>
> Is it possible to generate a little more discussion on why this would not be
> a good addition? It seems like a nice convenience to me...

I think the problem is that := is actually not an assignment
operator[0] at all. Thus allowing foo.bar := baz would be IMO
surprising at least.

[0]: http://golang.org/ref/spec#Short_variable_declarations

-j

jaspe...@kreuzwerker.de

unread,
Nov 28, 2013, 11:10:10 AM11/28/13
to golan...@googlegroups.com, jaspe...@kreuzwerker.de, Lukáš Zapletal
Are we talking about the same thing Jan? I may not have been very clear in my first post, but I was referring to the example that Lukáš gave originally of:

cache.file, err := os.Open(filename)

where there is at least one new variable (err) and also the struct field which is known.

I agree that something like: foo.bar := baz should not work.

chris dollin

unread,
Nov 28, 2013, 11:20:18 AM11/28/13
to jaspe...@kreuzwerker.de, golang-nuts, Lukáš Zapletal
On 28 November 2013 16:10, <jaspe...@kreuzwerker.de> wrote:
Are we talking about the same thing Jan? I may not have been very clear in my first post, but I was referring to the example that Lukáš gave originally of:

cache.file, err := os.Open(filename)

where there is at least one new variable (err) and also the struct field which is known.

I agree that something like: foo.bar := baz should not work.

Then cache.file, err := whatever shouldn't work.

The lhs of a short declaration is a list of identifers to be (re)declared.
spinkle.sponkle isn't an identifier. (Whether or not there's a "new variable"
declared is a different matter.)

Chris

--
Chris "allusive" Dollin

Jasper Timm

unread,
Nov 28, 2013, 12:06:20 PM11/28/13
to chris dollin, golang-nuts, Lukáš Zapletal
In what definition of 'identifier' does spinkle.sponkle not exist? (http://golang.org/ref/spec#Identifiers) Am I missing something fundamental here?

I think there being a 'new variable' is the whole idea here. In my mind at least, ':=' for multiple entries on the LHS translates to
 ':=' for new terms 
and 
'=' for everything known. 

Is that wrong?

chris dollin

unread,
Nov 28, 2013, 12:25:48 PM11/28/13
to Jasper Timm, golang-nuts, Lukáš Zapletal

Identifiers don't contain dotsvin go.
Chrisatabusstop.

Jasper Timm

unread,
Nov 28, 2013, 12:52:46 PM11/28/13
to chris dollin, golang-nuts
Fair enough.

But given that, is 'it's not an identifier' enough of a reason? Why is it not ok to extend this to struct fields as well?

Gustavo Niemeyer

unread,
Nov 28, 2013, 12:53:10 PM11/28/13
to Jasper Timm, chris dollin, golang-nuts, Lukáš Zapletal
On Thu, Nov 28, 2013 at 3:06 PM, Jasper Timm <jaspe...@kreuzwerker.de> wrote:
> In what definition of 'identifier' does spinkle.sponkle not exist?
> (http://golang.org/ref/spec#Identifiers) Am I missing something fundamental
> here?

Yeah, it's a selector rather than an identifier:

http://golang.org/ref/spec#Selectors

With that said ...

> I think there being a 'new variable' is the whole idea here. In my mind at
> least, ':=' for multiple entries on the LHS translates to
> ':=' for new terms
> and
> '=' for everything known.
>
> Is that wrong?

It's not how it works today, but it doesn't feel like a totally
unacceptable perspective either. I actually wished that would work a
few times too, and IIRC none of the main developers have demonstrated
strong disagreement to that. It is probably a "let's wait a bit"
situation.

I suggest opening an issue, so it's properly tracked and responded to.


gustavo @ http://niemeyer.net

Gustavo Niemeyer

unread,
Nov 28, 2013, 1:43:20 PM11/28/13
to Jasper Timm, chris dollin, golang-nuts, Lukáš Zapletal
On Thu, Nov 28, 2013 at 3:53 PM, Gustavo Niemeyer <gus...@niemeyer.net> wrote:
> It's not how it works today, but it doesn't feel like a totally
> unacceptable perspective either. I actually wished that would work a
> few times too, and IIRC none of the main developers have demonstrated
> strong disagreement to that. It is probably a "let's wait a bit"
> situation.
>
> I suggest opening an issue, so it's properly tracked and responded to.

I did it: http://golang.org/issue/6842


gustavo @ http://niemeyer.net

Jasper Timm

unread,
Nov 28, 2013, 1:50:25 PM11/28/13
to Gustavo Niemeyer, chris dollin, golang-nuts, Lukáš Zapletal
Thanks Gustavo, beat me to it ;)
Reply all
Reply to author
Forward
0 new messages