我在使用 golang 开发网页时遇到问题。服务器文件(main.go):
package main
import (
"net/http"
"io/ioutil"
"strings"
"log"
)
type MyHandler struct {
}
func (this *MyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path[1:]
log.Println(path)
data, err := ioutil.ReadFile(string(path))
if err == nil {
var contentType string
if strings.HasSuffix(path, ".css") {
contentType = "text/css"
} else if strings.HasSuffix(path, ".html") {
contentType = "text/html"
} else if strings.HasSuffix(path, ".js") {
contentType = "application/javascript"
} else if strings.HasSuffix(path, ".png") {
contentType = "image/png"
} else if strings.HasSuffix(path, ".svg") {
contentType = "image/svg+xml"
} else {
contentType = "text/plain"
}
w.Header().Add("Content Type", contentType)
w.Write(data)
} else {
w.WriteHeader(404)
w.Write([]byte("404 Mi amigo - " + http.StatusText(404)))
}
}
func main() {
http.Handle("/", new(MyHandler))
http.ListenAndServe(":8080", nil)
}
但是当我输入http://localhost:8080/templates/home.html 这是我看到的截图 为什么我的页面没有加载正确?我的css在哪里??为什么在我处理 main.go 中的内容类型时出现错误“资源解释为样式表但使用 MIME 类型文本/纯文本传输:”?