【发布时间】:2019-03-06 16:26:15
【问题描述】:
我刚开始学习围棋,我真正想做的一件事就是在围棋中制作网站。我看了一些关于它的教程并使网站正常工作,但我不知道如何添加样式。
我在 Internet 和 stackoverflow 上搜索了一些示例,但找不到真正适合我的示例(并且保持简单)。
下面是我最终得到的代码。 但我认为我现在遇到了一个新问题,因为控制台中显示:
为此,我尝试了很多在互联网上找到的解决方案,但都没有奏效,所以我很确定这是因为我在 go 中错误地导入了 css。
Go (functions.go):
package main
import (
"html/template"
"net/http"
)
type IndexPage struct {
Title string
SubTitle string
}
func indexHandler(w http.ResponseWriter, r *http.Request){
p := IndexPage{Title: "Pizza site", SubTitle: "everyone loves pizzas"}
t, _ := template.ParseFiles("index.html")
t.Execute(w,p)
}
func main() {
http.HandleFunc("/", indexHandler)
http.Handle("/css/", http.FileServer(http.Dir("css")))
http.ListenAndServe(":8080", nil)
}
HTML (index.html):
<html lang="nl">
<head>
<meta charset="utf-8">
<title>Pizzaaaaaaa</title>
<link rel="stylesheet" href="css/style.css" type="text/css">
</head>
<body>
<article>
<h1>
{{ .Title }}
<span class="subtitle">{{ .SubTitle }}</span>
</h1>
<p>Some text</p>
</article>
</body>
</html>
CSS (/css/style.css)
*{
color: rgb(250, 157, 157);
}
文件树
【问题讨论】:
-
stackoverflow.com/a/43601392/9312230 这可能会对您有所帮助。 TL;博士;此提供文件的代码将解决您的问题
http.Handle("/css/", http.StripPrefix("/css/", http.FileServer(http.Dir("css")))) -
哦,我看到这篇文章并复制了那个代码。我只是认为 stripPrefix 和所有这些东西都是不必要的代码,所以删除了那部分。 O.o