How to open a html document with template engine in golang

Hello guys,
I try to open an HTML document for the login page. when trying with this section I got this error :

func Index(response http.ResponseWriter, request *http.Request) {

tmp, err := template.ParseFiles("github.com/mehrdaddolatkhah/rastebazaar/pkg/web/templates/admin/index.html")
if err != nil {
	panic(err)
}

tmp.Execute(response, nil)

}

I got os.PathError

and the HTML document is under this path:
– root path project
/pkg/web/templates/admin/login.html

I think this problem is for the path that I gave to template.ParseFiles. but I don’t know how I must give the correct path to ParseFiles. may please guide me. thank you

I update path for ParseFiles to : tmp, err := template.ParseFiles("pkg/web/templates/admin/index.html")

and now HTML document loaded, but CSS and javascript won’t load. shall I do something else?

You have to point to CSS and JS folders as well. And all other folders that you use.

http.Handle("/css/", http.StripPrefix("/css/", http.FileServer(http.Dir("./public/css"))))

https://play.golang.org/p/-piEnp9xrPJ

1 Like