当我尝试将值传递给.html代码时,我正在使用该html/template包.
但是,我似乎无法设置.css我的.html文件中引用的内容类型.它以纯文本形式提供给浏览器,因此忽略了格式化.
在一个静态.html文件中,我可以使用内置函数http.Fileserver来处理内容类型,但然后模板化不起作用.我无法传递变量.它只显示为{{.}}
有没有办法将content-type内置文件服务器的便利性http.Fileserver与模板能力结合起来http.HandleFunc?
这是我的代码,因为它没有使用http.Fileserver.注意,我的Go文件位于起始目录中,index.html而且.css文件位于子目录中/hello/:
package main
import (
"html/template"
"io/ioutil"
"log"
"net/http"
)
var Message string = "test page"
func servePipe(w http.ResponseWriter, req *http.Request) {
dat, err := ioutil.ReadFile("hello/index.html")
if err != nil {
log.Fatal("couldn't read file:", err)
}
templ, err := template.New("Test").Parse(string(dat))
if err != nil {
log.Fatal("couldn't parse …Run Code Online (Sandbox Code Playgroud)