type species struct {
name string
genus string
}
type quacker interface {
Quack() string
}
type duck struct {
mu *sync.Mutex
q quacker
s species
}
// called like v.mu.Lock(), v.q.Quack(), v.s.name, and v.s.genus
type duck struct {
*sync.Mutex
quacker
species
}
// called like v.Lock(), v.Quack(), v.species, and v.genus
// an interface groups behavior which makes them tempting for this problem
type Tool interface {
Load(string) error
Execute(string) error
}
// this approach requires adding excess types
type GoTool struct{}
type (a GoTool) Load(path string) error {…}
type (a GoTool) Execute(args string) error {…}
type CppTool struct{}
// these methods may do nothing with a
type (a CppTool) Load(path string) error {…}
type (a CppTool) Execute(args string) error {…}
type Tool struct {
Load func(path string) error
Execute func(args string) error
}
var (
GoTool = Tool{
Load: func(path string) error {…},
Execute: func(args string) error {…},
}
CppTool = Tool{
Load: func(path string) error {…},
Execute: func(args string) error {…},
}
)
Ian
--
You received this message because you are subscribed to the Google Groups "golang-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-dev+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
I don't grasp the use of quacker in
your example; I would typically expect duck to have a Quack method and
thus to implement the quacker interface, I wouldn't ordinary expect
duck to embed a Quacker field.
(contrary to your example, I don't think I would ever recommend embedding a *sync.Mutex)
This can sometimes be appropriate advice but I'm not sure it rises to the level of a style guideline.
Another consideration in embedding is exporting.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-dev+...@googlegroups.com.
(contrary to your example, I don't think I would ever recommend embedding a *sync.Mutex)Can you explain your view on this? I embed Mutex pointers to pass structs by value instead of by pointer while also having the embedding shortcut.
Hello,I’d like to add these two sections to https://github.com/golang/go/wiki/CodeReviewComments based on experience giving golang-nuts code reviews in the past two months. The page says discuss even minor changes, so any feedback is appreciated.Embed Struct FieldsStruct embedding can simplify code throughout a project.Instead of
type species struct {
name string
genus string
}
type quacker interface {
Quack() string
}
type duck struct {
mu *sync.Mutex
q quacker
s species
}
// called like v.mu.Lock(), v.q.Quack(), v.s.name, and v.s.genusembed the fields to simplify access.
type duck struct {
*sync.Mutex
quacker
species
}
// called like v.Lock(), v.Quack(), v.species, and v.genusThe lack of stuttering struct access improves readability of Go code.A caveat is that the methods and fields of embedded types are promoted to the struct type which may be unwanted in library design or for godoc clarity.
See https://golang.org/issue/21670 (and the ensuing discussion about generalized “interface literals”).
In this example, duck should embed species only if every behavior of a species (including methods added in the future!) should be observable in a duck.
That's not at all obvious to me here, so I would avoid embedding in this case (and would discourage it in a code review).