I have problem with adding some CSS to my web app, which is created with go lang. main.go
and html file are included to root directory and style.css
is included to root/css
directory
my .*go code is
package main
import (
"html/template"
"net/http"
)
func main(){
http.HandleFunc("/home", mainViewHandler)
http.ListenAndServe(":8080", nil)
}
func mainViewHandler(responseWriter http.ResponseWriter,request *http.Request){
t, _ := template.ParseFiles("main.html")
t.Execute(responseWriter, 0)
}
and html code is
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="todo/style.css">
<meta charset="UTF-8">
<title>Main Page</title>
</head>
<body>
<h2>Take your choice</h2>
<a href="/edit">Edit</a>
<a href="/add">Add</a>
<a href="/list">List</a>
</body>
</html>
You need to tell go to serve files from root/css directory. And keep in mind that root/css should be in the working folder of your app when you're running it.
I don't use http package directly, but believe in your case it should be as follows (before ListenAndServe):
http.Handle("/todo/", http.StripPrefix("root/css/", http.FileServer(http.Dir("root/css"))))
But you might need to adjust it according to your actual folders configuration.
See documentation here