本人用windows操作系统,用的Git-Bash工具
尝试过以下的方式:
曾经把main函数改成: router.ServeFiles("/src/*filepath", http.Dir("/video_server/templates"))也不行
handlers.go文件:
t, e := template.ParseFiles("./templates/home.html")
main.go文件:
router.ServeFiles("/statics/*filepath", http.Dir("./templates"))
完整的handlers.go文件:
package main
import (
“github.com/julienschmidt/httprouter”
“html/template”
“log”
“net/http”
)
type HomePage struct {
Name string
}
func HomeController(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
p := &HomePage{Name: “ajun”}
t, e := template.ParseFiles("./templates/home.html") //parser to bin for web page
if e != nil {
log.Printf(“parsing template home.html error :%s”, e)
return
}
t.Execute(w, p)
return
}
完整的main.go文件:
package main
import (
“net/http”
“github.com/julienschmidt/httprouter”
)
func RegisterHandler() *httprouter.Router {
router:=httprouter.New()
router.GET("/", HomeController)
router.POST("/", HomeController)
router.ServeFiles("/statics/*filepath", http.Dir("/templates"))
return router
}
func main() {
r:=RegisterHandler()
http.ListenAndServe(":8080", r)
}