英文:
Properly serve a CSS file Golang
问题
我有一个CSS文件,我正在尝试在我的Web服务器上提供它(使用http包中的新功能)。我尝试过使用http.ServeFile和http.Handle。我还尝试将其作为模板进行提供,但问题是在HTML页面的底部打印了CSS文件的内容。有没有正确的方法在不打印整个CSS文件的情况下提供CSS文件?
英文:
I have a CSS file and I am trying to serve it on my Web Server (New with the http package). I have tried
http. (ServeFile, and Handle)
I have also tried serving it as a template, but the issue is that on the bottom of the HTML page, it prints the CSS. What is the proper way of serving the CSS file without it printing the whole CSS file on the bottom of the page?
答案1
得分: 0
你可以尝试这个替代方案。
http.Handle("/", http.FileServer(http.Dir("css/")))
如果这对你解决问题有帮助,请告诉我。
英文:
You can try this instead.
http.Handle("/", http.FileServer(http.Dir("css/")))
Let me know if it helped you to your problem.
答案2
得分: 0
我总是这样做:
http.HandleFunc("/game.css", serveCss)
以及这样:
func serveCss(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "client/game.css")
}
希望这对你有帮助。
英文:
I always do this:
http.HandleFunc("/game.css", serveCss)
and this:
func serveCss(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "client/game.css")
}
Hope this help
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论