【发布时间】:2017-05-01 18:12:50
【问题描述】:
所以我在 Go 中遇到了奇怪的文件路径问题。这是我的文件结构。
C:/ProjectName/
-------------->bin/
-------------->pkg/
-------------->src/web/
---------------------->main.go
---------------------->controllers/Constants.go
---------------------->content/css/index.css
---------------------->views/index.html
我的 go 环境变量
GOBIN=C:\ProjectName\bin
GOPATH=C:\ProjectName
在文件index.html 中,我像<link href="/css/index.css"... 那样访问css 文件,但这不起作用。找不到css 文件。
同样在Constants.go 文件中,我像const indexFile string = "../src/web/views/index.htm" 一样访问html 文件,但这也不起作用。
如果我像这样/content/css/index.css 访问css 文件,它可以工作,如果我像这样"../web/views/index.htm" 访问index.html文件,它也可以工作。
问题是我在一个团队中,而其他人的代码以在我的计算机上不起作用的方式工作。每次我进行更改以使其在我的计算机上运行时,它都会在其他所有人的计算机上中断。
您知道问题可能是什么以及如何解决它吗?
谢谢,
编辑 1:我的处理程序
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)
}
爱丽丝->https://github.com/justinas/alice
上下文-> https://github.com/gorilla/context
函数indexFunc 只为index.html 文件提供服务。
【问题讨论】:
-
html 资源的位置不应与 Go 源代码有任何关系。