Created
October 14, 2017 13:51
-
-
Save pei0804/c5f81f77760d821d9e46456dce4c7077 to your computer and use it in GitHub Desktop.
gointerface
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" | |
type Person interface { | |
Greet() string | |
SetGreet(string) | |
SetName(string) | |
GetName() string | |
} | |
// Man | |
type Man struct { | |
name string | |
greet string | |
} | |
func (p *Man) Greet() string { | |
if p.greet == "" { | |
return "おっす" | |
} | |
return p.greet | |
} | |
func (p *Man) SetGreet(greet string) { | |
p.greet = greet | |
} | |
func (p *Man) SetName(name string) { | |
p.name = name | |
} | |
func (p *Man) GetName() string { | |
return p.name | |
} | |
// Woman | |
type Woman struct { | |
name string | |
greet string | |
} | |
func (p *Woman) Greet() string { | |
if p.greet == "" { | |
return "こんばんは" | |
} | |
return p.greet | |
} | |
func (p *Woman) SetGreet(greet string) { | |
p.greet = greet | |
} | |
func (p *Woman) SetName(name string) { | |
p.name = name | |
} | |
func (p *Woman) GetName() string { | |
return p.name | |
} | |
// NewType | |
type WeiType struct { | |
Man | |
} | |
func (p *WeiType) Greet() string { | |
return "うぇーいwwwwww" | |
} | |
func main() { | |
var person Person = &WeiType{ | |
Man{}, | |
} | |
fmt.Println(person.Greet()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment