doushe7934 2014-11-19 05:42
浏览 289
已采纳

Go Web应用程序的目录结构

I've followed the Writing Web Applications tutorial on the Go website and I'm starting to write my own web app. I've also read the beginning of How to Write Go Code and am trying to organise my code with the same workspace structure.

I'm writing a simple web app named mygosite that handles all requests by rendering a single template. After running go install github.com/wesleym/mygosite, my directory structure now looks like this:

go
+-src
| +-github.com
|   +-wesleym
|     +-mygosite
|       +-mygosite.go
|       +-templates
|         +- index.html
|       +-.git
+-bin
  +-mygosite

In my code, I'm referring to the template with path templates/index.html. When I run bin/mygosite, the app can't find my template because it's in the source tree. Since the templates are important to my app, I don't want to move them outside of my mygosite git repository.

Is my directory layout reasonable given what I'm trying to do? Should I change the path to my template in my code to be src/github.com/wesleym/mygosite/templates/index.html? What about static assets like CSS and JavaScript - where should those go when the time comes to introduce them?

tl;dr: Where do I put templates and static files in a Go web application project?

  • 写回答

3条回答 默认 最新

  • dongyuan8312 2014-11-19 07:20
    关注

    Where do I put templates and static files in a Go web application project?

    is the "wrong" question. If you ask "How will my executable find its resources?" you are almost at the solution: Your executable should read its resources from a (or several) locations which are configurable (and it is always nice to provide sensible defaults). Command line flags, environment variables and config files in the current working directory are common for such tasks. (Of course, if you just have a handful of small resources: Pack them into your executable as VonC recommended; this scheme just breaks down once you start including large assets like images or video.)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)