Created
February 14, 2019 07:06
-
-
Save oyakata/36fe9ec31d91af1e9109be0e808ed09b to your computer and use it in GitHub Desktop.
関数アダプタ
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
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