2014-11-19 05:42
浏览 200

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, my directory structure now looks like this:

|   +-wesleym
|     +-mygosite
|       +-mygosite.go
|       +-templates
|         +- index.html
|       +-.git

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/ 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?

图片转代码服务由CSDN问答提供 功能建议

我遵循了编写Web应用程序教程,我开始编写自己的Web应用程序。 我还阅读了如何编写Go代码的开头,并尝试使用

我正在编写一个名为mygosite的简单Web应用程序,该应用程序通过呈现单个模板来处理所有请求。 运行 go install 后,我的目录结构现在看起来像这样:

 + -src 
 |  + \ N |  + -wesleym \ N |  + -mygosite \ N |  + -mygosite.go \ N |  + -templates \ N |  +-index.html 
 |  +-。git 
 + -bin 
 + -mygosite 

在我的代码中,我指的是路径为 templates / index的模板。 HTML 。 当我运行 bin / mygosite 时,该应用程序找不到源模板,因为它位于源代码树中。 由于模板对我的应用程序很重要,因此我不想将其移出mygosite git存储库。

考虑到我要执行的操作,目录布局是否合理? 我应该在代码中将模板的路径更改为 src / / wesleym / mygosite / templates / index.html 吗? 诸如CSS和JavaScript之类的静态资产呢?当需要引入静态资产时,这些资产应该放在哪里?

tl; dr:dr:我应该在哪里将模板和静态文件放在Go中 Web应用程序项目?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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.)

    打赏 评论
  • drm16022 2014-11-19 06:54

    You could consider using the project jteeuwen/go-bindata

    This package converts any file into managable Go source code. Useful for embedding binary data into a go program. The file data is optionally gzip compressed before being converted to a raw byte slice.

    You can see it used in "golang embed file for later parsing execution use"

    That same page also mention GeertJohan/go.rice as an alternative

    Another recent good tool comes from esc: Embedding Static Assets in Go

    a program that:

    • can take some directories and recursively embed all files in them in a way that was compatible with http.FileSystem
    • can optionally be disabled for use with the local file system for local development
    • will not change the output file on subsequent runs
    • has reasonable-sized diffs when files changed
    • is vendoring-friendly
    打赏 评论
  • dsxmwin86342 2018-04-13 13:12

    If you want template-nesting (somewhat like template-inheritance), try: go(golang) template manager, especially for web development .

    A Suggest templates layout could be:

    ├── context
    │   ├── layout
    │   │   └── layout.tpl.html
    │   └── partial
    │       └── ads.tpl.html
    └── main
        └── demo
            ├── demo1.tpl.html
            ├── demo2.tpl.html
            └── dir1
                └── dir2
                    └── any.tpl.html




    打赏 评论

相关推荐 更多相似问题