http.StripPrefix:从自定义文件夹提供文件

I'm running main.cgi from this directory:

htmlroot/sub/main.cgi

A request to the resource might look like this:

http://www.mysite/sub/main.cgi

My HTML pages include static CSS files, referred to something like this:

<link href="/styles/main.css" rel="stylesheet">

However, the static files are actually located here:

htmlroot/sub/styles/main.css

I'm trying to tell Go to grab the file from the htmlroot/sub/styles instead of htmlroot/styles, but I can't seem to make it work. The code looks like this:

styleDir := http.FileServer(http.Dir("styles")) //I think this should point to root/sub/styles -- but maybe not?

http.Handle("/styles/", http.StripPrefix("/styles/", styleDir)

In this case, main.css is not found. I believe it's actually looking for it in htmlroot/styles, but I'm not sure why.

I'd appreciate any tips.

Try this:

styleDir := http.FileServer(http.Dir("sub"+filepath.Separator+"styles"))
http.Handle("/styles/", http.StripPrefix("/styles/", styleDir)