On 24 March 2015 at 10:20, Nate Finch <
nate....@gmail.com> wrote:
> unexported identifiers are unexported. This is not inheritance. X is still
> a field on a, not B. B.X is just an alias for b.a.X. The reasons for #1
> and #2 are the same. Unexported identifiers can't be accessed by external
> packages and thus is not documented in godoc.
I'm afraid you're wrong there. Even though the type a is not exported, its
members are still publicly available, so B.X is publicly available.
This is an outstanding bug in godoc:
https://github.com/golang/go/issues/6127
There are places it's a real issue. For example in
http://godoc.org/gopkg.in/check.v1#C
the C type has a benchmark-related field, N, which is necessary in order to use
the benchmark functionality correctly, but is undocumented because of
this issue.
Forud, you have two choices - either export the embedded type
or make sure that your documentation explicitly mentions the additionally
available fields.
I'd recommend that fields like this are not part of the required
initialization of
such a struct. As you suggested, the way to do it is with an additional
assignment statement.
Aside: it's perhaps interesting to note that when using reflect,
anonymous fields do not get a non-empty PkgPath (see
http://golang.org/pkg/reflect/#StructField), indicating that they are
not truly private.
cheers,
rog.
>
> On Tuesday, March 24, 2015 at 6:09:50 AM UTC-4, Forud A wrote:
>>
>> Hi Gophers,
>>
>> I have a public struct that embed another internal struct with public
>> fields like this :
>>
>> type a struct {
>> X int
>> }
>>
>> type B struct {
>> a
>> Y int
>> }
>>
>>
>> in other package I can use B.X and B.Y , the problem is :
>> 1- can not directly initialize X when I create new B like this : B{X: 1,
>> Y: 10}, is there anyway to do this without a secound line like v.X=10 ?
>> (this is not so important, another line of code is not a big deal)
>> 2- the other problem (important for us :)) ) is in document for B, there
>> is no sign of the X. I think this is some kind of bug in godoc, or something
>> that I miss?
>