Skip to content

Instantly share code, notes, and snippets.

@oyakata
Created February 14, 2019 07:06
Show Gist options
  • Save oyakata/36fe9ec31d91af1e9109be0e808ed09b to your computer and use it in GitHub Desktop.
Save oyakata/36fe9ec31d91af1e9109be0e808ed09b to your computer and use it in GitHub Desktop.
関数アダプタ
package main
import (
"fmt"
)
type helloFunc func(int, int, int) string
func callHello(fn helloFunc) {
s := fn(1, 2, 3)
fmt.Println(s)
}
func foo(x, y, z int) string {
return fmt.Sprintf("%v:%v:%v", x, y, z)
}
func bar(x, y int) string {
return fmt.Sprintf("%v:%v", x, y)
}
func helloAdaptor(fn func(int, int) string) helloFunc {
return func(x, y, z int) string {
return fn(x, y)
}
}
func main() {
callHello(foo)
callHello(helloAdaptor(bar))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment