go HTMl text美化,golang将资源解释为样式表,但使用MIME类型传输text/plain

我在使用golang开发我的网页时遇到问题。 服务器文件(main.go):golang将资源解释为样式表,但使用MIME类型传输text/plain

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 这是我看到see screenshot 为什么我的页面没有加载正确的?我的css在哪里?为什么是错误“资源解释为样式表,但传输与MIME类型文本/纯:”出现,而我有我的内容在main.go类型处理??

+0

请点击上面的链接查看我打开我的网页时看到的内容^ –

+1

另请参阅https://github.com/golang/go/wiki/HttpStaticFiles –

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值