Skip to content

Instantly share code, notes, and snippets.

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