今天学到Goweb时测试模板引擎的使用发现一个问题,代码如下:
结构:
package main
import (
"fmt"
"html/template"
"net/http"
)
func testTemplate(w http.ResponseWriter, r *http.Request) {
//解析模板
t, err := template.ParseFiles("hello.html")
//执行
fmt.Println("error=", err)
t.Execute(w, "hello template")
}
func main() {
http.HandleFunc("/testTemplate", testTemplate)
http.ListenAndServe(":8080", nil)
}
在VScode运行时访问http://localhost:8080/testTemplate
控制台出错
解决方法:
1、采用go build 运行方式,而不是go run
达到效果