golang标准库对于HttpServer有很好的支持。
import (
"fmt"
"log"
"net/http"
)
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "request from %s\n", r.URL.Path)
}
func main() {
http.HandleFunc("/", handler)
log.Fatalln(http.ListenAndServe("localhost:8000", nil))
}
启动以后,使用curl
命令来测试
$ curl -i http://localhost:8000/test/foo/bar
后台输出:
request from /test/foo/bar