关于Go web开发 文件路径的问题

Orlion 2017-03-25 03:06:35
现在的项目目录结构是这样的:

go install main之后运行会报错tpl/index/index.html该文件不存在。

当我把indexController第9行代码改为template.ParseFiles("index.html")并将index.html文件移动到golibrary目录(即与src bin pkg同级目录)时。运行就不会报错了。

OS: window
GOPATH: D:/xxx/golibrary/

So, Why?
...全文
1272 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
cooodeer 2019-08-13
  • 打赏
  • 举报
回复
引用 4 楼 weixin_43742919 的回复:
楼主这个问题怎么解决的 ?我现在也是这个问题,感觉是gopath的配置问题
请问你怎么解决的?
weixin_43742919 2018-12-01
  • 打赏
  • 举报
回复
func index(w http.ResponseWriter, r *http.Request) {
name := "my website"


t, err := template.ParseFiles(
"/Project/src/chaintops/templates/layout.html",
"/Project/src/chaintops/templates/nav.html",
"/Project/src/chaintops/templates/index.html",
"/Project/src/chaintops/templates/foot.html",)

if err != nil {
fmt.Println(err)
} else {
t.ExecuteTemplate(w, "layout", name)
}
}

加载模板文件一定要写绝对路径,写/templates/layout.html 就报错,找不到文件
weixin_43742919 2018-12-01
  • 打赏
  • 举报
回复
楼主这个问题怎么解决的 ?我现在也是这个问题,感觉是gopath的配置问题
Koh Rong 2017-03-30
  • 打赏
  • 举报
回复
遇到相同问题了, lz的怎么样了
不得闲 2017-03-26
  • 打赏
  • 举报
回复
最好是放到Bin目录下,如果你不放到bin目录下,运行之前先获取当前目录,然后换算出相对路径,再做处理
Orlion 2017-03-25
  • 打赏
  • 举报
回复
描述错误,应该是移动模板文件到bin目录下。 So, Go编译的时候是不会把模板文件编译到项目里?

2,190

社区成员

发帖
与我相关
我的任务
社区描述
go语言学习与交流版
社区管理员
  • go语言社区
  • Freeman Z
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧