2016-06-12 30 views
-2

我有问题与添加一些CSS到我的网络应用程序,这是创建与朗朗。 main.go和HTML文件都包含到根目录,并style.css被列入到root/css目录如何包含CSS去朗应用程序

我。* Go代码是

package main 

import (
    "html/template" 
    "net/http" 
) 

func main(){ 

    http.HandleFunc("/home", mainViewHandler) 
    http.ListenAndServe(":8080", nil) 
} 

func mainViewHandler(responseWriter http.ResponseWriter,request *http.Request){ 

    t, _ := template.ParseFiles("main.html") 
    t.Execute(responseWriter, 0) 
} 

和HTML代码是

<!DOCTYPE html> 
<html lang="en"> 

    <head> 
     <link rel="stylesheet" href="todo/style.css"> 
     <meta charset="UTF-8"> 
     <title>Main Page</title> 

    </head> 

    <body> 
     <h2>Take your choice</h2> 
     <a href="/edit">Edit</a> 
     <a href="/add">Add</a> 
     <a href="/list">List</a> 
    </body> 
</html> 
+0

[在围棋Web应用程序渲染CSS]的可能的复制(http://stackoverflow.com/questions/13302020/rendering-css-in-a-go-web-application) –

回答

1

你需要告诉去从root/css目录提供文件。请记住,在运行它时,root/css应该位于应用程序的工作文件夹中。

我不直接使用HTTP包,但相信在你的情况应该是如下(ListenAndServe前):

http.Handle("/todo/", http.StripPrefix("root/css/", http.FileServer(http.Dir("root/css")))) 

但是你可能需要根据你的实际文件夹的配置进行调整。

查看文档here

+0

我之前尝试使用此解决方案的时间更长,并且不起作用 – bajky

+0

我必须添加一个“。”。在我的路径之前-_- ...感谢您的提示。 – bajky

相关问题