Created
November 2, 2012 10:31
-
-
Save julianshen/4000031 to your computer and use it in GitHub Desktop.
[Go] Type, Method, and 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 Handler interface { | |
| ServeHTTP(ResponseWriter, *Request) | |
| } | |
| type HandlerFunc func(ResponseWriter, *Request) | |
| // ServeHTTP calls f(w, r). | |
| 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
| package main | |
| import ( | |
| "fmt" | |
| ) | |
| type MyHandler func(string) | |
| type MyInterface interface { | |
| Foo() | |
| } | |
| type Aa string | |
| func (a *Aa) Foo() { | |
| fmt.Println("Foo called : " + *a) | |
| } | |
| func (fs MyHandler) Foo() { | |
| fs("foo") | |
| } | |
| func CallFoo(i MyInterface) { | |
| i.Foo() | |
| } | |
| func main() { | |
| x := Aa("test") | |
| CallFoo(&x) | |
| var fs MyHandler | |
| fs = func(s string) { | |
| fmt.Println("aaa:" + s) | |
| } | |
| CallFoo(&fs) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment