Golang模板使用相对路径失败

时间:2018-06-21 12:49:15

标签: templates go go-templates errortemplate

我只是不了解Go模板和相对路径...

使用以下目录结构:

Application Directory
  program.go
  program executable
-- css
     program.css
-- pages
     index.gohtml
     wartree.gohtml

目前,我的模板是纯html格式,已经在不同环境中使用了几年。

我有以下Go代码:

package main

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

func main() {
    http.HandleFunc("/", index)
    http.HandleFunc("/wartree", wartree)

    log.Fatal(http.ListenAndServe(":8080", nil))
}


func index(w http.ResponseWriter, r *http.Request) {
    fmt.Println("Index has been called...")

    tmpl := template.Must(template.ParseGlob("pages/*"))
    tmpl.Execute(w, nil)
}

func wartree(w http.ResponseWriter, r *http.Request) {
    fmt.Println("wartree has been called...")

    tmpl := template.Must(template.ParseFiles("wartree.gohtml"))
    tmpl.Execute(w, nil)
}

两个模板的HTML模板头中都有以下行... <link href="css/nav.css" type="text/css" media="screen" />

执行程序时,未使用css,并且无法调用Wartree模板。它只是再次调用索引?

我什至尝试将css的路径更改为绝对路径,但这没什么区别,而且Go或浏览器也没有错误。

我只是不了解Go模板和路径。我想。

0 个答案:

没有答案
相关问题