Last active
January 6, 2018 11:24
-
-
Save spraints/7fa7f93366b6cdfffe30e4e5232adb02 to your computer and use it in GitHub Desktop.
This file contains 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" | |
) | |
func main() { | |
try(&Owner{}) | |
try(&Owner{org: &Organization{"group"}}) | |
try(&Owner{user: &User{"person"}}) | |
} | |
func try(o *Owner) { | |
defer func() { | |
if rvr := recover(); rvr != nil { | |
fmt.Printf(" => panic!! %#v\n", rvr) | |
} | |
}() | |
fmt.Printf("Trying %#v\n", o) | |
fmt.Printf(" => %q\n", o.DatabaseString()) | |
} | |
type Owner struct { | |
org *Organization | |
user *User | |
} | |
func (o Owner) DatabaseString() string { | |
var owner DatabaseStringer | |
fmt.Printf(" [0] %#v\n", owner) | |
if owner == nil { | |
owner = o.GetOrganization() | |
fmt.Printf(" [1] %#v\n", owner) | |
} | |
if owner == nil { | |
owner = o.GetUser() | |
fmt.Printf(" [2] %#v\n", owner) | |
} | |
if owner == nil { | |
return "NIL" | |
} | |
return owner.DatabaseString() | |
} | |
func (o Owner) GetOrganization() *Organization { | |
return o.org | |
} | |
func (o Owner) GetUser() *User { | |
return o.user | |
} | |
type DatabaseStringer interface { | |
DatabaseString() string | |
} | |
type Organization struct { | |
name string | |
} | |
func (o *Organization) DatabaseString() string { | |
return "organization:" + o.name | |
} | |
type User struct { | |
name string | |
} | |
func (u *User) DatabaseString() string { | |
return "user:" + u.name | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment