我正在尝试学习有关 golang 的在线视频课程,但在提供静态 html 文件时出现错误。
package main
import (
"io/ioutil"
"net/http"
)
func main() {
http.Handle("/", new(MyHandler))
http.ListenAndServe(":8000", nil)
}
type MyHandler struct {
http.Handler
}
func (this *MyHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
path := "public/" + req.URL.Path
data, err := ioutil.ReadFile(string(path))
if err == nil {
w.Write(data)
} else {
w.WriteHeader(404)
w.Write([]byte("404 - " + http.StatusText(404)))
}
}
我的文件夹结构 是这样的。
智慧大石
皈依舞
相关分类