转到:打开../src/web/views/index.htm:系统找不到指定的路径

So i am having strange file path issues in Go. Here is what my file structure looks like.

C:/ProjectName/
-------------->bin/
-------------->pkg/
-------------->src/web/
---------------------->main.go
---------------------->controllers/Constants.go
---------------------->content/css/index.css
---------------------->views/index.html

My go enviroment variables

GOBIN=C:\ProjectName\bin
GOPATH=C:\ProjectName

In the file index.html, i access the css file like so <link href="/css/index.css"... but this doesn't work. The css file is not found.
Also in the Constants.go file i access the html file like so const indexFile string = "../src/web/views/index.htm" but this also doesn't work.

If i access the css file like this /content/css/index.css it works, and if i access the index.htmlfile like this "../web/views/index.htm" it also works.

The problem is that i am on a team, and everybody else's code works in the way that doesn't work on my computer. And every time i make the changes to make it run on my computer, it breaks on every other persons computer.

Any idea on what the problem could be and how to get about fixing it?

Thank you,

EDIT 1: My handlers

router := httprouter.New()
router.GET(basic("/", IndexFunc))

func basic(p string, h func(http.ResponseWriter, *http.Request)) (string, 
httprouter.Handle) {

    return p, wrapHandler(alice.New(context.ClearHandler).ThenFunc(h))
}

func IndexFunc(w http.ResponseWriter, r *http.Request) 
{    
    t, err := template.ParseFiles("../src/web/views/index.htm")
    checkError(err)
    t.Execute(w, nil)
}

alice -> https://github.com/justinas/alice context -> https://github.com/gorilla/context function indexFunc just serves the index.html file.

We need to see the code you are using to host the files to be sure, but usually what I will do to serve a directory is this:

http.Handle("/content/", http.StripPrefix("/content/", http.FileServer(http.Dir("content"))

This will:

  • Serve all files inside "content" (relative to where you are running app from) at /content/*
  • Strip ("/content") from the front of all urls before looking up the file in that dir.

I try to put all static files in a common directory to make this easier.

My guess is you are just doing http.Handle("/", http.Fileserver(http.Dir("")) or something, which just serves all files at their paths relative to the working dir.

Edit:

In go apps, you have to specify the path for each handler. You need to either move the files to match the urls you want, or define multiple url mappings. I would usually recommend handlers in this setup:

  1. API handlers under /api defined one at a time (http.HandleFunc("/api/whatever", myHandler))

  2. All Static content served from /static/* from a single directory. (First line in answer above). So /content/css/style.css serves ./content/css/style.css

  3. Anything else serves "index.html". http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request){ http.ServeFile(w,r,"path/to/index.html") }