Last active
January 16, 2018 11:11
-
-
Save tenntenn/6612309 to your computer and use it in GitHub Desktop.
Go言語における埋め込みによるインタフェースの部分実装パターン ref: https://qiita.com/tenntenn/items/e04441a40aeb9c31dbaf
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 struct { | |
FirstName string | |
LastName string | |
} | |
func (p *Person) Name() string { | |
return p.FirstName + " " + p.LastName | |
} | |
func main() { | |
person := &Person{"Taro", "Yamada"} | |
fmt.Println(person.Name()) | |
} | |
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 struct { | |
FirstName string | |
LastName string | |
} | |
func (p *Person) Name() string { | |
return p.FirstName + " " + p.LastName | |
} | |
func main() { | |
person := &Person{"Taro", "Yamada"} | |
fmt.Println(person.Name()) | |
} | |
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 struct { | |
FirstName string | |
LastName string | |
} | |
func (p *Person) Name() string { | |
return p.FirstName + " " + p.LastName | |
} | |
type Named interface { | |
Name() string | |
} | |
func printName(named Named) { | |
fmt.Println(named.Name()) | |
} | |
func main() { | |
person := &Person{"Tarou", "Yamada"} | |
printName(person) | |
} |
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 struct { | |
FirstName string | |
LastName string | |
} | |
func (p *Person) Name() string { | |
return p.FirstName + " " + p.LastName | |
} | |
type Named interface { | |
Name() string | |
} | |
func printName(named Named) { | |
fmt.Println(named.Name()) | |
} | |
func main() { | |
person := &Person{"Tarou", "Yamada"} | |
printName(person) | |
} |
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 Hoge struct { | |
N int | |
} | |
type Piyo struct { | |
Hoge | |
M int | |
} | |
func main() { | |
piyo := &Piyo{Hoge{1}, 2} | |
fmt.Println(piyo.N, piyo.M) | |
fmt.Println(piyo.Hoge.N, piyo.M) | |
} |
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 Hoge struct { | |
N int | |
} | |
type Piyo struct { | |
Hoge | |
M int | |
} | |
func main() { | |
piyo := &Piyo{Hoge{1}, 2} | |
fmt.Println(piyo.N, piyo.M) | |
fmt.Println(piyo.Hoge.N, piyo.M) | |
} |
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 Gender int | |
const ( | |
Female = iota | |
Male | |
) | |
type Person interface { | |
Name() string | |
Title() string | |
} | |
func New(gender Gender, firstName, lastName string) Person { | |
p := &person{firstName, lastName} | |
if gender == Male { | |
return &male{p} | |
} else { | |
return &female{p} | |
} | |
} | |
type person struct { | |
firstName string | |
lastName string | |
} | |
func (p *person) Name() string { | |
return p.firstName + " " + p.lastName | |
} | |
type female struct { | |
*person | |
} | |
func (f *female) Title() string { | |
return "Ms." | |
} | |
type male struct { | |
*person | |
} | |
func (m *male) Title() string { | |
return "Mr." | |
} | |
func printFullName(p Person) { | |
fmt.Println(p.Title(), p.Name()) | |
} | |
func main() { | |
taro := New(Male, "Taro", "Yamada") | |
printFullName(taro) | |
hanako := New(Female, "Hanako", "Yamada") | |
printFullName(hanako) | |
} | |
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 Gender int | |
const ( | |
Female = iota | |
Male | |
) | |
type Person interface { | |
Name() string | |
Title() string | |
} | |
func New(gender Gender, firstName, lastName string) Person { | |
p := &person{firstName, lastName} | |
if gender == Male { | |
return &male{p} | |
} else { | |
return &female{p} | |
} | |
} | |
type person struct { | |
firstName string | |
lastName string | |
} | |
func (p *person) Name() string { | |
return p.firstName + " " + p.lastName | |
} | |
type female struct { | |
*person | |
} | |
func (f *female) Title() string { | |
return "Ms." | |
} | |
type male struct { | |
*person | |
} | |
func (m *male) Title() string { | |
return "Mr." | |
} | |
func printFullName(p Person) { | |
fmt.Println(p.Title(), p.Name()) | |
} | |
func main() { | |
taro := New(Male, "Taro", "Yamada") | |
printFullName(taro) | |
hanako := New(Female, "Hanako", "Yamada") | |
printFullName(hanako) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment