Friday, October 10, 2025
HomeGolangUtilizing web/http I am unable to serve css - Getting Assist

Utilizing web/http I am unable to serve css – Getting Assist


Hello there,

First off, I’m a beginner with golang and with webdev basically.
I’m making an attempt to program a easy web site and I’m not in a position to get the css config proper.

.
├── static
│   └── css
│       └── check.css
├── check.go
└── tmpl
    └── check.html

check.go:

package deal primary

import (
	"log"
	"web/http"
	"textual content/template"
)

func primary() {  

  web site := func(w http.ResponseWriter, r *http.Request) {
    templ := template.Should(template.ParseFiles("tmpl/check.html"))
    templ.Execute(w, nil)
  }
  
  http.Deal with("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static")))) 
  http.HandleFunc("/check/", web site)

  log.Println("Listening on :8081 ...")
  err := http.ListenAndServe(":8081", nil)
  if err != nil {
    log.Deadly(err)
  }
}

check.html:

<!doctype html>
<html lang="en-US">
  <head>
    <meta charset="UTF-8" />
    <meta title="viewport" content material="width=device-width, initial-scale=1.0" />
    <meta http-equiv="“X-UA-Suitable”" content material="“ie" ="edge”" />
    <title>Llynx Demo @ IGMR</title>
    <hyperlink
      href="https://cdn.jsdelivr.web/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css"
      rel="stylesheet"
      integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN"
      crossorigin="nameless"
    />

    <!-- <model> -->
    <!--   [class*="col"] { -->
    <!--     padding: 1rem; -->
    <!--     background-color: #33b5e5; -->
    <!--     border: 2px strong #fff; -->
    <!--     shade: #fff; -->
    <!--     text-align: heart; -->
    <!--   } -->
    <!---->
    <!--   [class*="con"] { -->
    <!--     padding: 1rem; -->
    <!--     background-color: #9e33e5; -->
    <!--     border: 2px strong #fff; -->
    <!--     shade: #fff; -->
    <!--     text-align: heart; -->
    <!--   } -->
    <!-- </model> -->

    <hyperlink rel="stylesheet" sort="textual content/css" href="https://discussion board.golangbridge.org/t/using-net-http-im-unable-to-serve-css/static/css/check.css" />
  </head>
  <physique>
    <div class="container">
      <h1>HI</h1>
    </div>
  </physique>
</html>

And the check.css:

.col {
  padding: 1rem;
  background-color: #33b5e5;
  border: 2px strong #fff;
  shade: #fff;
  text-align: heart;
}
.con {
  padding: 1rem;
  background-color: #9e33e5;
  border: 2px strong #fff;
  shade: #fff;
  text-align: heart;
}

3 Thinks I’ve noticed:

  1. The Chrome Dev software doesn’t present an error that it couldn’t load some css information
  2. If i’m going on http://localhost:8081/static/css/check.css I see the content material of the .css file – thats good, proper?
  3. Within the check.html: If I write the styling there I can see the div in purple, so the css shouldn’t be the problem?

I’m trying to find 3 hours now, I hope any individual can provide me some hints:)

Thanks!

Change this line in your .html file to

href="https://discussion board.golangbridge.org/static/css/check.css"

Right here you might have an instance methods to “add” all stuff you place inside the public folder.

Thanks, however sadly this isn’t working both.

I learn by all, the file construction will not be talked about nor a completed repo.
Didn’t made it :roll_eyes:

I believe one thing is mistaken along with your html or css code. I copied all the things out of your instance. Simply added h1 shade: purple to css file, modified this slash and all the things labored.



1 Like

My file construction is the previous basic.

And the file construction is talked about in the long run of every serve folder “./public/css”

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



1 Like

Thanks guys,
I don’t know what i’ve modified, nevertheless it’s working immediately after I used one other laptop (did’t touched the challenge for two weeks).

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments