- /**
- * Created by tsuibin on 13-12-19.
- */
- package main
- import(
- "io"
- "log"
- "net/http"
- "os"
- )
- func main() {
- mux := http.NewServeMux()
- mux.Handle("/", &myHandler{})
- mux.HandleFunc("/hello", sayHello)
- wd, err := os.Getwd()
- if err != nil{
- log.Fatal(err)
- }
- mux.Handle("/static/",
- http.StripPrefix("/static",
- http.FileServer(http.Dir(wd))))
- err = http.ListenAndServe(":8080", mux)
- if err != nil {
- log.Fatal(err)
- }
- }
- type myHandler struct{}
- func (*myHandler) ServeHTTP(w http.ResponseWriter, r *http.Request){
- io.WriteString(w, "URL:" + r.URL.Path)
- }
- func sayHello(w http.ResponseWriter, r *http.Request){
- io.WriteString(w, "Hello world, this is version 1")
- }
- //该片段来自于http://www.codesnippet.cn/detail/301220138332.html
来源: http://www.codesnippet.cn/detail/301220138332.html