0

index.go

package main
import (
    "html/template"
    "net/http"
)
func viewHandler(w http.ResponseWriter, r *http.Request) {
    t, _ := template.ParseFiles("index.html")
    t.Execute(w, nil)
}
func main() {
    http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
    http.HandleFunc("/index", viewHandler)
    http.ListenAndServe(":8080", nil)
}

In my index.html, I used the following path

<link rel="stylesheet" type="text/css" href="/static/css/bootstrap.css">

And the path for the .css is as follows,

web(folder)

|---index.go

|---static/css/xxx.css

But, the css is not included in the html. How can I change the code to fix this problem

Wyatt
  • 1,357
  • 5
  • 17
  • 26
  • What do you mean "not included in html"? According to the code, the server should serve the CSS file under `serveraddress.com/static/css/bootstrap.css`, not include it in anything. I'm pretty sure the file names you've written here are wrong. "index.go"? – Staven Jun 28 '15 at 11:06
  • @Staven Sorry, that is main.go. Actually, when I click http://localhost:8080/static/css/bootstrap.css, it tells 404 error. How can I fix it? – Wyatt Jun 28 '15 at 11:23
  • @Wyatt I've just tested your code and it serves the CSS file correctly on my machine. Are you sure you don't have any conflicts with port 8080 on your machine? – Intermernet Jun 28 '15 at 11:28
  • @Intermernet Thank you very much. I changed it to 8090 and this problem is fixed – Wyatt Jun 28 '15 at 11:37

1 Answers1

-1

Because of the conflicts of the port, the CSS file is not correctly included. Thanks to @Intermernet. :)

Wyatt
  • 1,357
  • 5
  • 17
  • 26