Go: 无法渲染外部样式表

huangapple go评论84阅读模式
英文:

Go: unable to render external stylesheets

问题

我正在尝试渲染外部样式表,但不确定为什么不起作用:

GO代码:

func main() {
    http.HandleFunc("/", homeHandler)
    http.HandleFunc("/image/", imageHandler)
    http.Handle("/layout/", http.StripPrefix("/layout/", http.FileServer(http.Dir("layout"))))

    http.ListenAndServe(":8000", nil)
}

目录结构:

gocode
    layout
        stylesheets
            home.css
    home.html
main.go

HTML代码:

<link rel="stylesheet" type="text/css" href="/stylesheets/home.css" />

有什么问题吗?我按照这里的示例进行操作:https://stackoverflow.com/questions/13302020/rendering-css-in-a-go-web-application

英文:

I'm trying to render external stylesheets. I'm not sure why this is not working:

GO:

func main() {
    http.HandleFunc(&quot;/&quot;, homeHandler)
    http.HandleFunc(&quot;/image/&quot;, imageHandler)
    http.Handle(&quot;/layout/&quot;, http.StripPrefix(&quot;/layout/&quot;, http.FileServer(http.Dir(&quot;layout&quot;))))

    http.ListenAndServe(&quot;:8000&quot;, nil)
}

Dir structure:

gocode
    layout
        stylesheets
            home.css
    home.html
main.go

HTML:

&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;/stylesheets/home.css&quot; /&gt;

Whats wrong? I followed the example here: https://stackoverflow.com/questions/13302020/rendering-css-in-a-go-web-application

答案1

得分: 3

您的文件服务器位于/layout/,因此应该是

<link rel="stylesheet" type="text/css" href="/layout/stylesheets/home.css" />
英文:

Your file server is at /layout/, so it should be

&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;/layout/stylesheets/home.css&quot; /&gt;

huangapple
  • 本文由 发表于 2014年8月28日 22:18:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/25551111.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定