Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 在Golang中加载图像和css_Html_Css_Image_Go_Template Engine - Fatal编程技术网

Html 在Golang中加载图像和css

Html 在Golang中加载图像和css,html,css,image,go,template-engine,Html,Css,Image,Go,Template Engine,我在项目根目录的包main中的server.js中设置了一个路由 http.HandleFunc(“/”,route.IndexHandler) IndexHandler在包route中实现,如下所示: func IndexHandler(w http.ResponseWriter, r *http.Request) { data:=struct{ Name string }{ "My name", } util.RenderTem

我在项目根目录的包
main
中的
server.js
中设置了一个路由

http.HandleFunc(“/”,route.IndexHandler)

IndexHandler
在包
route
中实现,如下所示:

func IndexHandler(w http.ResponseWriter, r *http.Request) {
    data:=struct{
        Name string
    }{
        "My name",
    }
    util.RenderTemplate(w, "index", data)
}
func RenderTemplate(w http.ResponseWriter, tmpl string, data interface{}) {
    cwd, _ := os.Getwd()
    t, err := template.ParseFiles(filepath.Join(cwd, "./view/" + tmpl + ".html"))
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }
    err = t.Execute(w, data)
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
    }
}
/
/public/css
/public/images
/public/js
/route
/view
RenderTemplate
函数在包
util
中实现如下:

func IndexHandler(w http.ResponseWriter, r *http.Request) {
    data:=struct{
        Name string
    }{
        "My name",
    }
    util.RenderTemplate(w, "index", data)
}
func RenderTemplate(w http.ResponseWriter, tmpl string, data interface{}) {
    cwd, _ := os.Getwd()
    t, err := template.ParseFiles(filepath.Join(cwd, "./view/" + tmpl + ".html"))
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }
    err = t.Execute(w, data)
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
    }
}
/
/public/css
/public/images
/public/js
/route
/view
项目中的目录结构如下:

func IndexHandler(w http.ResponseWriter, r *http.Request) {
    data:=struct{
        Name string
    }{
        "My name",
    }
    util.RenderTemplate(w, "index", data)
}
func RenderTemplate(w http.ResponseWriter, tmpl string, data interface{}) {
    cwd, _ := os.Getwd()
    t, err := template.ParseFiles(filepath.Join(cwd, "./view/" + tmpl + ".html"))
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }
    err = t.Execute(w, data)
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
    }
}
/
/public/css
/public/images
/public/js
/route
/view
index.html
视图位于文件夹
view
,路由器位于文件夹
route

index.html
中,我包括以下资源:


当请求适当的路径时,
index.html
仍会呈现,但不会加载图像和样式表。如何将它们包含在Golang html模板引擎中?

您需要明确要求服务器提供静态文件

在您的情况下,注册另一个处理程序

http.Handle("/public/", http.StripPrefix("/public/", http.FileServer(http.Dir("public"))))

正如Aruna所说,注册一个静态文件服务器句柄

http.Handle("/public/", http.StripPrefix("/public/", http.FileServer(http.Dir("public"))))
要使用HTML中的文件,只需

<img src="/public/images/img_landing_page_mac.png">