0

我只是不明白 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 模板和路径。我猜。

4

0 回答 0