-1

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>
Munesawagi
  • 283
  • 1
  • 6
  • 14
bajky
  • 332
  • 6
  • 17
  • Possible duplicate of [Rendering CSS in a Go Web Application](http://stackoverflow.com/questions/13302020/rendering-css-in-a-go-web-application) – Aruna Herath Jun 12 '16 at 02:10

1 Answers1

1

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

Seva
  • 2,388
  • 10
  • 9