I notice that the embedded example in Effective Go uses a function for
initializing an embedded field, so is not much help as I want to use a
struct literal.
type Person struct {
name string
}
type Employee struct {
Person
}
func main() {
dude := Employee{Person{"Me"}}
// or dude:= Employee{Person:Person{"Me"}}
}
-Daniel
This is as I also have a field "number" in Employee that I also want
to initialize. But with your first line changed to
dude:=Employee{Person{"Me"}, number:1}
I get the error message - "mixture of field:value and value
initializers".
However, I really don't understand the logic behind your second line,
and would like to know how you came up with it from the Go docs.
I'm assuming your Employee struct is now of the form
type Employee struct {
Person
number int
}
If so, as described in Effective Go, "Constructors and composite
literals", you can construct structs using { } syntax in two ways:
1. Providing the values for each (and every) field in order, eg.
dude := Employee{ Person{"Me"}, 1 }
2. Or, by using field:value initializers for any number of fields in
any order, eg.
dude := Employee{ Person:Person{"Me"}, number:1 } or
dude := Employee{ number:1, Person:Person{"Me"} } or
dude := Employee{ Person:Person{"Me"} } (number gets default value of 0)
But, you can't mix and match those styles, as you've attempted to do.
dude := Employee{ Person:Person{"Me"} } (number gets default value of 0)
Well, it's not required syntax, but I'm with you in spirit. On the
same point: as currently implemented, being able to directly reference
embedded fields' members feels more like syntactic sugar than a
first-class-feature. If I have
type Foo struct {
Alpha string
}
type Bar struct {
Beta int
}
type Foobar struct {
Foo
Bar
Delta string
}
it's a real shame I can't create a Foobar via
fb := Foobar{"abc", 64, "xyz"} // or
fb := Foobar{Alpha:"abc", Beta:64, Delta:"xyz"}
or do a json.Unmarshal on `{ "Alpha": "abc", "Beta": 64, "Delta":
"xyz" }`, given that (once created) the following is totally valid:
fmt.Printf("%s %d %d\n", fb.Alpha, fb.Beta, fb.Delta)
If this worked, it would really help cut down on the amount of
copy/paste and code duplication I'm currently having to do. I wonder
if there's a technical reason this isn't possible?
fmt.Printf("%s %d %s\n", fb.Alpha, fb.Beta, fb.Delta), of course :|
On Mar 14, 8:18 pm, Peter Bourgon <peterbour...@gmail.com> wrote:
p := Person{...}
For my use, struct Embedding is excellent at run time in that the
methods and fields of the embedded structs appear at run time as top
level methods and fields, giving simple composition. Unfortunately
this is not reflected in initialization with struct literals and I end
up with initializing as in:
dude := Employee{Person:Person{firstName:"John", secondName:"Doe"},
number:1}
when what I would like to be able to use is:
dude := Employee{firstName:"John", secondName:"Doe", number:1}
Perhaps the problem is that what I want is a simple macro like include
facility to provide a union of the structs, not the complex tree
structured embedding type facility. I realize that this would not
allow for multiple fields with the same name, but I am not sure that
is a problem in real programs. This macro approach may also remove the
complex compiler writing issues.
On Mar 14, 8:43 pm, Steven <steven...@gmail.com> wrote:
> On Sun, Mar 14, 2010 at 4:35 PM, Peter Bourgon <pe...@bourgon.org> wrote:
> > On Sun, Mar 14, 2010 at 10:23 PM, Steven <steven...@gmail.com> wrote:
> > > On Sun, Mar 14, 2010 at 4:18 PM, Peter Bourgon <peterbour...@gmail.com>
> http://groups.google.com/group/golang-nuts/browse_thread/thread/ad301...