@Pollux
2017-03-09T13:13:20.000000Z
字数 920
阅读 3222
PathToYou
package mainimport ("log""net/http""os")func main() {os.Mkdir("file", 0777)http.Handle("/pollux/", http.StripPrefix("/pollux/", http.FileServer(http.Dir("file"))))err := http.ListenAndServe(":8080", nil)if err != nil {log.Fatal("ListenAndServe: ", err)}}

FileServer returns a handler that serves HTTP requests with the contents of the file system rooted at root.To use the operating system's file system implementation, use http.Dir:

StripPrefix returns a handler that serves HTTP requests by removing the given prefix from the request URL's Path and invoking the handler h. StripPrefix handles a request for a path that doesn't begin with prefix by replying with an HTTP 404 not found error.
StripPrefix() 识别 URL 请求中的给定的前缀prefix,如 http://localhost:8080/pollux/ 中的 /pollux/,然后响应函数中的第二个参数 handler h,假如没有识别到给定的前缀,则响应404。