Skip to content

Instantly share code, notes, and snippets.

@djthorpe
Created January 23, 2019 08:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djthorpe/e9344604479dde8ec884e243f4cb40ad to your computer and use it in GitHub Desktop.
Save djthorpe/e9344604479dde8ec884e243f4cb40ad to your computer and use it in GitHub Desktop.
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