Not showing my style in html with embed golang feature

174 Views Asked by At

I want to use the embed features in Golang but I can't link up the CSS style to the HTML. I tried to find more information to see what I do wrong, but I could not find any help.

main.go

package main

import (
    "embed"
    "html/template"
    "net/http"
    "github.com/gorilla/mux"
)

//go:embed static
var fs embed.FS

//go:embed static/css
var css embed.FS

func testHandler(w http.ResponseWriter, r *http.Request) {
    t, _ := template.ParseFS(fs, "static/index.html")
    t.Execute(w, "")

}

func main() {

    r := mux.NewRouter()

    r.HandleFunc("/", testHandler)
    fileServer := http.FileServer(http.FS(css))

    //normally what I use
    //fileServer := http.FileServer(http.Dir("static"))

    http.Handle("/", r)
    http.Handle("/css/", fileServer)
    http.ListenAndServe(":8083", nil)
}

index.html

<!DOCTYPE html>
<html >
<head>
  <meta charset="UTF-8">
  <title>Responsive Table</title>
      <link rel="stylesheet" href="/css/style.css">
</head>

<h1>Hi</h1>

style.css

body {background-color: coral;}

Structure is as follows...

├── go.mod
├── go.sum
├── main.go
├── static
│   ├── css
│   │   └── style.css
│   └── index.html
0

There are 0 best solutions below