Code:
package main
import (
"fmt"
"log"
"net/http"
)
type String string
func (h String) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, h)
}
type Struct struct {
Greeting string
Punct string
Who string
}
func (h *Struct) ServeHTTP(w http.ResponseWriter, r*http.Request){
fmt.Fprintf(w, "%v %v %v", h.Greeting, h.Punct, h.Who)
}
func main() {
// your http.Handle calls here
http.Handle("/string", String("I'm a good man."))
http.Handle("/struct", &Struct{"Hello", ":", "Gophers! 你好吗?"})
log.Fatal(http.ListenAndServe("localhost:4000", nil))
}
Call example:
http://localhost:4000/struct
http://localhost:4000/string
Reference:
http://tour.golang.org/methods/14
No comments:
Post a Comment