html – How to use static file in Golang?

Using this file structure and code, I’m trying to make the CSS file work, but it doesn’t include the CSS style when I run the code. I do not know why? Although I searched online and used the answers already given, nothing worked.

app
 - controllers
 - models
 - views
     - static
          - main.css
     - templates
          - home.html
 main.go

home.html

<head>
    <link rel="stylesheet" href="../static/main.css">
</head>

main.go

fun main(){
    http.HandleFunc("/", controllers.Home)
    http.Handle("/views/", http.StripPrefix("/views/static/", http.FileServer(http.Dir("views/static"))))
    http.ListenAndServe(":4000", nil);
}

Leave a Reply

Your email address will not be published.