Does anyone konw which proposal changes the access for embeded field name of struct?

107 views
Skip to first unread message

Ally Dale

unread,
Jun 7, 2021, 5:19:43 AM6/7/21
to golang-nuts
Does anyone konw which proposal changes the access for embeded field name of struct?
Eg:
```
type Base struct{
    Name string
}
type Value struct{
    Base
}
```

We initial a Value as:
var v = Value{
    Base: Base{
        Name:"xxx",
    }
}
instead of 
var v = Value{
        Name:"xxx",
}

I want to know why golang change the access method.
Anyone who knows the isssue link, please reply me. Thanks a lot.

Axel Wagner

unread,
Jun 7, 2021, 5:28:27 AM6/7/21
to Ally Dale, golang-nuts
I don't understand the question. This has always been how the language worked, all the way back to Go 1.0 at least. As far as I'm aware.

Embedding only effects selector expressions and methods sets - i.e. it allows you to write `v.Name` instead of writing `v.Base.Name`. It doesn't affect composite literals.

--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/ff4f3443-c863-4d96-958c-76023979b0ben%40googlegroups.com.

Jan Mercl

unread,
Jun 7, 2021, 5:29:07 AM6/7/21
to Ally Dale, golang-nuts
On Mon, Jun 7, 2021 at 11:20 AM Ally Dale <vip...@gmail.com> wrote:

> I want to know why golang change the access method.

The name of the programming language is Go.

> Anyone who knows the isssue link, please reply me. Thanks a lot.

There's no proposal and there's no issue. It always worked like that.

You cannot initialize the field 'Name' of struct 'Value' because
struct 'Value' has no such field. The (essentially) syntactic sugar
that allows to access the inner field like in 'value.Name' is
specified to work in selectors, not in initializers:
https://golang.org/ref/spec#Selectors

Ally Dale

unread,
Jun 7, 2021, 5:35:34 AM6/7/21
to golang-nuts
As far as I remember(maybe I have missing something),  
var v = Value{
        Name:"xxx",
}
Is the old way to initial an embeded field.
Some issuse changes the way of this.
I can't find the original discussion.

Axel Wagner

unread,
Jun 7, 2021, 5:47:07 AM6/7/21
to Ally Dale, golang-nuts
What we are saying is that you are misremembering. That's not how Go ever worked.

Ross Light

unread,
Jun 7, 2021, 10:35:47 AM6/7/21
to golang-nuts
https://golang.org/issue/9859 is a proposal to support such syntax, but it hasn't really moved in a long time.

Ally Dale

unread,
Jun 7, 2021, 11:07:36 AM6/7/21
to golang-nuts
Thank a lot, this is the topic what I'm interested in.
Current syntax has troubled me in many cases.
Reply all
Reply to author
Forward
0 new messages