Last active
December 3, 2017 03:00
-
-
Save tenntenn/be5483d0cfbb529218f3 to your computer and use it in GitHub Desktop.
インタフェースの実装パターン #golang ref: https://qiita.com/tenntenn/items/eac962a49c56b2b15ee8
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
// 構造体型の宣言 | |
type Hoge struct { | |
// フィールドリスト | |
} | |
// インタフェース型の宣言 | |
type Fuga interface { | |
// メソッドリスト | |
} |
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
// 構造体型の宣言 | |
type Hoge struct { | |
// フィールドリスト | |
} | |
// インタフェース型の宣言 | |
type Fuga interface { | |
// メソッドリスト | |
} |
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
TypeDecl = "type" ( TypeSpec | "(" { TypeSpec ";" } ")" ) . | |
TypeSpec = identifier Type . | |
Type = TypeName | TypeLit | "(" Type ")" . | |
TypeName = identifier | QualifiedIdent . | |
TypeLit = ArrayType | StructType | PointerType | FunctionType | InterfaceType | | |
SliceType | MapType | ChannelType . |
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
type TypeName interface { | |
// メソッドリスト | |
Method1() | |
Method2() | |
} |
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
type TypeName interface { | |
// メソッドリスト | |
Method1() | |
Method2() | |
} |
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
type Stringer interface { | |
String() string | |
} |
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
type Stringer interface { | |
String() string | |
} |
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
var stringer fmt.Stringer | |
// int型の100をHex型にキャストし,fmt.Stringer型の変数に代入している | |
stringer = Hex(100) |
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
var stringer fmt.Stringer | |
// int型の100をHex型にキャストし,fmt.Stringer型の変数に代入している | |
stringer = Hex(100) |
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
type HandlerFunc func(w ResponseWriter, r *Request) | |
func (f HandlerFunc) ServeHTTP(w ResponseWriter, r *Request) { | |
f(w, r) | |
} |
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
type HandlerFunc func(w ResponseWriter, r *Request) | |
func (f HandlerFunc) ServeHTTP(w ResponseWriter, r *Request) { | |
f(w, r) | |
} |
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
type Handler interface { | |
ServeHTTP(w ResponseWriter, r *Request) | |
} |
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
type Handler interface { | |
ServeHTTP(w ResponseWriter, r *Request) | |
} |
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
f := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
fmt.Fprintf(w, "hello, world") | |
}) | |
// func Handle(pattern string, handler Handler) | |
http.Handle("/", f) |
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
f := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
fmt.Fprintf(w, "hello, world") | |
}) | |
// func Handle(pattern string, handler Handler) | |
http.Handle("/", f) |
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
func HandleFunc(pattern string, handler func(ResponseWriter, *Request)) |
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
func HandleFunc(pattern string, handler func(ResponseWriter, *Request)) |
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
type Name struct { | |
FirstName string | |
LastName string | |
} | |
func (n *Name) String() string { | |
return fmt.Sprintf("%s %s", n.FirstName, n.LastName) | |
} | |
type Person struct { | |
// *Name型の値を埋め込む | |
*Name | |
Age int | |
} | |
func main() { | |
n := &Name{ | |
FirstName: "Taro", | |
LastName: "Yamada", | |
} | |
p := &Person{ | |
// *Name型のnを埋め込む | |
Name: n, | |
Age: 20, | |
} | |
fmt.Println(p.String()) | |
} |
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
type Name struct { | |
FirstName string | |
LastName string | |
} | |
func (n *Name) String() string { | |
return fmt.Sprintf("%s %s", n.FirstName, n.LastName) | |
} | |
type Person struct { | |
// *Name型の値を埋め込む | |
*Name | |
Age int | |
} | |
func main() { | |
n := &Name{ | |
FirstName: "Taro", | |
LastName: "Yamada", | |
} | |
p := &Person{ | |
// *Name型のnを埋め込む | |
Name: n, | |
Age: 20, | |
} | |
fmt.Println(p.String()) | |
} |
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
var stringer fmt.Stringer = p | |
fmt.Println(stringer.String()) |
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
var stringer fmt.Stringer = p | |
fmt.Println(stringer.String()) |
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
type Person interface { | |
// 敬称 | |
Title() string | |
// 名前 | |
Name() string | |
} |
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
type Person interface { | |
// 敬称 | |
Title() string | |
// 名前 | |
Name() string | |
} |
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
type 識別子 型 |
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
type person struct { | |
firstName string | |
lastName string | |
} | |
func (p *person) Name() string { | |
return fmt.Sprintf("%s %s", p.firstName, p.lastName) | |
} |
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
type person struct { | |
firstName string | |
lastName string | |
} | |
func (p *person) Name() string { | |
return fmt.Sprintf("%s %s", p.firstName, p.lastName) | |
} |
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
type Gender int | |
const ( | |
Female = iota | |
Male | |
) | |
type female struct { | |
*person | |
} | |
func (f *female) Title() string { | |
return "Ms." | |
} | |
type male struct { | |
*person | |
} | |
func (m *male) Title() string { | |
return "Mr." | |
} |
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
type Gender int | |
const ( | |
Female = iota | |
Male | |
) | |
type female struct { | |
*person | |
} | |
func (f *female) Title() string { | |
return "Ms." | |
} | |
type male struct { | |
*person | |
} | |
func (m *male) Title() string { | |
return "Mr." | |
} |
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
func NewPerson(gender Gender, firstName, lastName string) Person { | |
p := &person{firstName, lastName} | |
if gender == Female { | |
return &female{p} | |
} | |
return &male{p} | |
} |
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
func NewPerson(gender Gender, firstName, lastName string) Person { | |
p := &person{firstName, lastName} | |
if gender == Female { | |
return &female{p} | |
} | |
return &male{p} | |
} |
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
type Hoge struct { | |
chan int // ダメ | |
[]int // ダメ | |
fmt.Stringer // OK | |
} |
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
type Hoge struct { | |
chan int // ダメ | |
[]int // ダメ | |
fmt.Stringer // OK | |
} |
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
type Hoge struct { | |
*Fuga // *Fuga型の値しか埋め込むことができない | |
fmt.Stringer // fmt.Stringerインタフェースを実装していれば埋め込むことができる | |
} |
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
type Hoge struct { | |
*Fuga // *Fuga型の値しか埋め込むことができない | |
fmt.Stringer // fmt.Stringerインタフェースを実装していれば埋め込むことができる | |
} |
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
type Person struct { | |
fmt.Stringer | |
FirstName string | |
LastName string | |
Age int | |
} | |
func main() { | |
p := Person{ | |
Stringer: nil, | |
FirstName: "Taro", | |
LastName: "Yamada", | |
Age: 20, | |
} | |
fmt.Println(p.Stringer) // nil | |
p.Stringer = ??? // fmt.Stringerインタフェースを実装していれば代入できる | |
} |
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
type Person struct { | |
fmt.Stringer | |
FirstName string | |
LastName string | |
Age int | |
} | |
func main() { | |
p := Person{ | |
Stringer: nil, | |
FirstName: "Taro", | |
LastName: "Yamada", | |
Age: 20, | |
} | |
fmt.Println(p.Stringer) // nil | |
p.Stringer = ??? // fmt.Stringerインタフェースを実装していれば代入できる | |
} |
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
type StringerFunc func() string | |
func (sf StringerFunc) String() string { | |
return sf() | |
} |
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
type StringerFunc func() string | |
func (sf StringerFunc) String() string { | |
return sf() | |
} |
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
func BindStringer(p *Person, f func(p *Person) string) fmt.Stringer { | |
return StringerFunc(func() string { | |
return f(p) | |
}) | |
} |
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
func BindStringer(p *Person, f func(p *Person) string) fmt.Stringer { | |
return StringerFunc(func() string { | |
return f(p) | |
}) | |
} |
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
func NewPerson(firstName, lastName string, age int) (p *Person) { | |
p = &Person{ | |
nil, | |
firstName, | |
lastName, | |
age, | |
} | |
p.Stringer = StringerFunc(func() string { | |
return fmt.Sprintf("%s %s (%d)", p.FirstName, p.LastName, p.Age) | |
}) | |
return | |
} | |
func (p *Person) SetStringer(sf func(p *Person) string) { | |
p.Stringer = StringerFunc(func() string { | |
return sf(p) | |
}) | |
} |
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
func NewPerson(firstName, lastName string, age int) (p *Person) { | |
p = &Person{ | |
nil, | |
firstName, | |
lastName, | |
age, | |
} | |
p.Stringer = StringerFunc(func() string { | |
return fmt.Sprintf("%s %s (%d)", p.FirstName, p.LastName, p.Age) | |
}) | |
return | |
} | |
func (p *Person) SetStringer(sf func(p *Person) string) { | |
p.Stringer = StringerFunc(func() string { | |
return sf(p) | |
}) | |
} |
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
// パッケージ外に公開されない型として宣言する | |
type stringer fmt.Stringer | |
type Person struct { | |
stringer // パッケージ外から代入ができない匿名フィールド | |
FirstName string | |
LastName string | |
Age int | |
} |
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
// パッケージ外に公開されない型として宣言する | |
type stringer fmt.Stringer | |
type Person struct { | |
stringer // パッケージ外から代入ができない匿名フィールド | |
FirstName string | |
LastName string | |
Age int | |
} |
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
type Hex int |
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
type Hex int |
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
// 配列型 | |
[10]int | |
// 構造体型 | |
struct { | |
// フィールドリスト | |
} | |
// ポインタ型 | |
*int | |
// 関数型 | |
func(s string) int | |
// インタフェース型 | |
interface { | |
// メソッドリスト | |
} | |
// スライス型 | |
[]int | |
// マップ型 | |
map[string]int | |
// チャネル型 | |
chan bool |
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
// 配列型 | |
[10]int | |
// 構造体型 | |
struct { | |
// フィールドリスト | |
} | |
// ポインタ型 | |
*int | |
// 関数型 | |
func(s string) int | |
// インタフェース型 | |
interface { | |
// メソッドリスト | |
} | |
// スライス型 | |
[]int | |
// マップ型 | |
map[string]int | |
// チャネル型 | |
chan bool |
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
type HandlerFunc func(w http.ResponseWriter, r *http.Request) |
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
type HandlerFunc func(w http.ResponseWriter, r *http.Request) |
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
func (p *Person) String() string { | |
return fmt.Sprintf("%s %s (%d)", p.FirstName, p.LastName, p.Age) | |
} |
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
func (p *Person) String() string { | |
return fmt.Sprintf("%s %s (%d)", p.FirstName, p.LastName, p.Age) | |
} |
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
type Person struct { | |
FirstName string | |
LastName string | |
Age int | |
} |
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
type Person struct { | |
FirstName string | |
LastName string | |
Age int | |
} |
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
type Hex int | |
func (h Hex) String() string { | |
return fmt.Sprintf("0x%x", int(h)) | |
} |
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
type Hex int | |
func (h Hex) String() string { | |
return fmt.Sprintf("0x%x", int(h)) | |
} |
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
type HandlerFunc func(w ResponseWriter, r *Request) | |
func (f HandlerFunc) ServeHTTP(w ResponseWriter, r *Request) { | |
f(w, r) | |
} |
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
type HandlerFunc func(w ResponseWriter, r *Request) | |
func (f HandlerFunc) ServeHTTP(w ResponseWriter, r *Request) { | |
f(w, r) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment