Created
January 23, 2019 08:45
-
-
Save djthorpe/e9344604479dde8ec884e243f4cb40ad to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
type XConfig struct { | |
Value int | |
} | |
type YConfig struct { | |
Value int | |
} | |
type LogConfig struct { | |
} | |
type Log struct { | |
} | |
type X struct { | |
Log | |
value int | |
} | |
type Y struct { | |
X | |
value int | |
} | |
func (this *Log) Init(config LogConfig) (*Log, error) { | |
return this, nil | |
} | |
func (this *Log) Print(format string) { | |
fmt.Println("Log:", format) | |
} | |
func (this *X) Init(config XConfig) (*X, error) { | |
if _, err := this.Log.Init(LogConfig{}); err != nil { | |
return nil, err | |
} | |
this.Print("X Init") | |
this.value = config.Value | |
return this, nil | |
} | |
func (this *Y) Init(config YConfig) (*Y, error) { | |
if _, err := this.X.Init(XConfig{config.Value + 1}); err != nil { | |
return nil, err | |
} | |
this.Print("Y Init") | |
this.value = config.Value | |
return this, nil | |
} | |
func (this *Log) String() string { | |
return fmt.Sprintf("Log{ }") | |
} | |
func (this *X) String() string { | |
return fmt.Sprintf("X{ %v %v }", this.value, this.Log.String()) | |
} | |
func (this *Y) String() string { | |
return fmt.Sprintf("Y{ %v %v }", this.value, this.X.String()) | |
} | |
func main() { | |
if this, err := new(Y).Init(YConfig{100}); err != nil { | |
fmt.Println(err) | |
} else { | |
this.Print(fmt.Sprint(this)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment