Skip to content

Instantly share code, notes, and snippets.

@archever
Created September 21, 2018 09:47
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 archever/b13bf778d57807b7b9489db190822780 to your computer and use it in GitHub Desktop.
Save archever/b13bf778d57807b7b9489db190822780 to your computer and use it in GitHub Desktop.
golang interface as namespace
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