Created
September 21, 2018 09:47
-
-
Save archever/b13bf778d57807b7b9489db190822780 to your computer and use it in GitHub Desktop.
golang interface as namespace
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" | |
"reflect" | |
) | |
type person struct { | |
Name string | |
} | |
type BabyI interface { | |
Crawling() | |
GrowUp() BoyI | |
} | |
type BoyI interface { | |
Playing() | |
Older() ManI | |
} | |
type ManI interface { | |
Working() | |
} | |
func (p *person) Crawling() { | |
fmt.Printf("%s is crawling\n", p.Name) | |
} | |
func (p *person) Playing() { | |
fmt.Printf("%s is playing\n", p.Name) | |
} | |
func (p *person) Working() { | |
fmt.Printf("%s is working\n", p.Name) | |
} | |
func (p *person) GrowUp() BoyI { | |
return reflect.ValueOf(p).Interface().(BoyI) | |
} | |
func (p *person) Older() ManI { | |
return reflect.ValueOf(p).Interface().(ManI) | |
} | |
func NewBaby(name string) BabyI { | |
return &person{Name: name} | |
} | |
func main() { | |
baby := NewBaby("dd") | |
baby.Crawling() | |
baby.GrowUp().Playing() | |
baby.GrowUp().Older().Working() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment