这篇文章只是联系go指南时的笔记吧。
package main import ( "fmt" "log" "net/http" ) type String string type Struct struct{ Greeting string Punct string Who string } func main(){ http.Handle("/string",String("aaaaaaaaaaaaaaaaaaaa2")) http.Handle("/struct", Struct{ Greeting: "Hello", Punct: "", Who: "gopher", }) log.Fatal(http.ListenAndServe("localhost:4000",nil)) } func (s String) ServeHTTP(w http.ResponseWriter, r *http.Request){ fmt.Fprint(w,s) } func (s Struct) ServeHTTP(w http.ResponseWriter, r *http.Request){ fmt.Fprint(w,s) }
上面的http.handle() 里面的url,如果重复的话,会报错