Directory structure for Go web app

非 Y 不嫁゛ 提交于 2019-12-07 04:08:23

问题


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?


回答1:


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:


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



回答3:


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

A Suggest templates layout could be:

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

demo1

demo2

any



来源:https://stackoverflow.com/questions/27009798/directory-structure-for-go-web-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!