我使用链接到CSS文件的模板,并关注此主板上的其他一些帖子,我认为我有一切正确,但CSS仍未应用。此外,当我在Chrome中使用开发人员工具时,我会看到我在模板中输入的CSS文件的链接,如果我点击它,它会将我带到CSS文件,我假设它显示我可以找到它文件。
转到代码:
package main
import (
"fmt"
"strings"
"os"
"text/template"
"net/http"
)
var tpl *template.Template
func init() {
tpl = template.Must(template.ParseGlob("templates/*"))
}
func handler(w http.ResponseWriter, r *http.Request) {
host, _ := os.Hostname()
err := tpl.Execute(w, host)
if err != nil {
fmt.Println("Template Execute failed: ", err)
}
}
func process(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
fmt.Println(r.Form)
fmt.Println("path", r.URL.Path)
fmt.Println("scheme", r.URL.Scheme)
fmt.Println(r.Form["url_long"])
for k, v := range r.Form {
fmt.Println("key:", k)
fmt.Println("val:", strings.Join(v, ""))
}
fmt.Println(r.Form["wall"])
}
func main() {
host, _ := os.Hostname()
fmt.Println("host =", host)
//err = tpl.Execute(f, host)
swall := http.NewServeMux()
//fs := http.FileServer(http.Dir("www"))
//swall.Handle("/", fs)
swall.Handle("/resources/", http.StripPrefix("/resources/", http.FileServer(http.Dir("resources"))))
swall.HandleFunc("/", handler)
swall.HandleFunc("/process", process)
http.ListenAndServe(":8080", swall)
}
HTML:
<!doctype html>
<html>
<head>
<title>Great Wall</title>
<link href="/resources/css/gwall.css" type="text/css" />
</head>
<body>
<main>
<div id="page_title">
<h1>Great Wall</h1>
</div>
</main>
</body>
</html>
CSS:
h1 {
color: red;
font-size: 36pt;
text-align: center;
}
我尝试了几天我能想到并搜索了几天,但我对模板没有多少经验,而且我现在完全失败了。我知道当有人告诉我我做错了什么时,我最终会在额头上砸自己,但此时我会冒着瘀伤的风险。谢谢你的帮助。