golang html/template template.ParseFiles(filenames) 相对路径问题

myhttp.go 
package main
import (
"fmt"
"html/template"
"log"
"net/http"
)
func login(w http.ResponseWriter,r *http.Request) {
fmt.Println("method:",r.Method)
if r.Method == "GET" {
t,_ :=template.ParseFiles("login.html")
t.Execute(w,nil)
} else {
fmt.Println("username:",r.Form["username"])
fmt.Println("password:",r.Form["password"])
}
}
func main() {
http.HandleFunc("/login",login)
err := http.ListenAndServe(":9090", nil)
if err != nil {
log.Fatal("ListenAndServe:",err)
}
}

如上代码 login.html要和生成的可执行文件myhttp.exe在同一级目录


原文地址:https://www.cnblogs.com/myDreamWillCometrue/p/13176862.html