Skip to content

Instantly share code, notes, and snippets.

@julianshen
Created November 2, 2012 10:31
Show Gist options
  • Save julianshen/4000031 to your computer and use it in GitHub Desktop.
Save julianshen/4000031 to your computer and use it in GitHub Desktop.
[Go] Type, Method, and Interface
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)
}
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