type T struct {
Variable string
private int
}
Now &T{"a"} produces the error message "too few values in struct
initializer", and &T{"a", 3} produces no error message at all. The fact
that the former form isn't allowed is somewhat inconvenient; the fact
that the latter is allowed seems like a bug.
Is there a reason struct initializers are done this way?
I think you're wrong, it's not a bug. If the field is private it means
that it will not be visible outside of the package boundaries and you
can't construct the T using composite literal in other than the current
package. In fact you can't access that private field directly at all
from any other package.
Yes, it is. You can omit fields, but if you do
Go asks that you name the fields you're intending
to list, so that it's clear which ones you intended
to supply and that it isn't just a matter of forgetting one.
x := &Job{
Logger: log.New(os.Stderr, nil, "Job: ", log.Ldate),
}
Russ