Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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