Can not load css and .js file with template engine

Hey guys,
I want to load an HTML document with a template engine in Golang. I have these files:
-myGoProgram as a binary file
-/static/
-/templates/

and in my goBinary file, try to load css like this:
http.Handle("…/static/", http.StripPrefix("…/static/", http.FileServer(http.Dir(“static”))))
log.Fatal(http.ListenAndServe(":8080", router))

and inside HTML document I try to load CSS and app.js like this:

when I try to load an HTML document without Go binary file everything would load correctly. but when try to load like this:
127.0.0.1:8080/login/admin
CSS and js file won’t load

please help me to fix this issue. thank you

thanks to dear Peter, a member of gophers slack, to help solve this problem.
I should use that router I defined to handle all routes, in this case, I used go-chi.

https://play.golang.org/p/yasJqCn9D-z

with this change now everything works correctly:

fs := http.FileServer(http.Dir("static"))
router.Handle("/static/*", http.StripPrefix("/static/", fs))

this StackOverFlow topic also helped me:

https://stackoverflow.com/questions/57811896/stylesheet-does-not-apply-in-go-html-template-using-chi-router

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.