用go寫一個文件服務器很簡單:
http.handle(“/”, http.FileServer(http.Dir(“doc”))
http.ListenAndServe(":8888”, nil)
打來localhost:8888,就能看到doc目錄下的所有文件。
但如果,你想用localhost:8888/doc來顯示進入文件目錄,則需要
http.Handle(“/doc", http.StripPrefix(“/doc", http.FileServer(http.Dir(“doc"))))
http.StripPrefix用於過濾request,參數裏的handler的request過濾掉特定的前序,只有這樣,才能正確顯示文件目錄。