Go web应用的目录结构

huangapple go评论69阅读模式
英文:

Directory structure for Go web app

问题

我已经按照你的要求翻译了以下内容:

我已经按照 Go 官方网站上的 编写 Web 应用 教程进行了操作,并且开始编写自己的 Web 应用程序。我还阅读了 如何编写 Go 代码 的开头部分,并尝试使用相同的工作区结构来组织我的代码。

我正在编写一个名为 mygosite 的简单 Web 应用程序,它通过渲染一个单一的模板来处理所有请求。在运行 go install github.com/wesleym/mygosite 后,我的目录结构如下所示:

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

在我的代码中,我使用路径 templates/index.html 引用模板。当我运行 bin/mygosite 时,应用程序找不到我的模板,因为它在源代码树中。由于模板对我的应用程序很重要,我不想将它们移出 mygosite 的 git 仓库。

鉴于我所做的事情,我的目录布局合理吗?我应该将代码中模板的路径更改为 src/github.com/wesleym/mygosite/templates/index.html 吗?至于 CSS 和 JavaScript 等静态资源,在引入它们时应该放在哪里?

简而言之:在 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?

答案1

得分: 4

更新2021年,使用Go 1.16,你可以使用以下方法:

> 新的embed通过新的//go:embed指令,在编译期间提供对程序中嵌入的文件的访问。

参考Amit Saha(@Atlassian的SRE)的文章“使用“embed”包在Go中嵌入文件”,他在项目中嵌入了一个模板。

>go >//go:embed templates/main.go.tmpl >var tmplMainGo []byte >
>
> 这将使上述模板的内容作为字节切片在tmplMainGo变量中可用。
>
> 然后我们可以按以下方式访问模板:
>
>go >tmpl, err := tmpl.Parse(string(tmplMainGo)) >

amitsaha/go-embed中可以看到一个演示。

还可以参考Barak Amar的“在Go 1.16版本中使用Embed”以及他自己的演示项目


原始答案2014年:你可以考虑使用jteeuwen/go-bindata项目(已于2018年存档)

> 该包将任何文件转换为可管理的Go源代码。用于将二进制数据嵌入到Go程序中。文件数据在转换为原始字节切片之前可以选择进行gzip压缩。

你可以在“golang embed file for later parsing execution use”中看到它的使用示例。

同样的页面还提到了GeertJohan/go.rice作为另一种选择。

另一个最近的好工具来自esc: Embedding Static Assets in Go

> 一个程序:
>
> - 可以接受一些目录,并以与http.FileSystem兼容的方式递归嵌入其中的所有文件
>- 可以选择在本地开发时禁用与本地文件系统的使用
>- 不会在后续运行中更改输出文件
>- 文件更改时具有合理大小的差异
>- 可以与供应商一起使用

英文:

Update 2021, with Go 1.16, you would use:

> The new embed package provides access to files embedded in the program during compilation using the new //go:embed directive.

See "Embedding files in Go using the "embed" package", from Amit Saha (SRE at @Atlassian) who does embed a template in his project.

>go
>//go:embed templates/main.go.tmpl
>var tmplMainGo []byte
>

>
> This makes the contents of the above template available as a slice of bytes in the tmplMainGo variable.
>
> We can then access the template as follows:
>
>go
>tmpl, err := tmpl.Parse(string(tmplMainGo))
>

See a demo at amitsaha/go-embed.

See also "Working with Embed in Go 1.16 Version" from Barak Amar and his own demo project.


Original answer2014: You could consider using the project jteeuwen/go-bindata (archived in 2018)

> 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

答案2

得分: 3

在Go Web应用程序项目中,你应该把模板和静态文件放在哪里?这个问题是"错误"的。如果你问"我的可执行文件如何找到它的资源?",你几乎已经找到了解决方案:你的可执行文件应该从一个(或多个)可配置的位置读取它的资源(并且提供合理的默认值总是很好的)。命令行标志、环境变量和当前工作目录中的配置文件在这种任务中很常见(当然,如果你只有少量的小资源:把它们打包到你的可执行文件中,就像VonC建议的那样;但是一旦你开始包含像图片或视频这样的大型资源,这种方案就会失效)。

英文:

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

答案3

得分: 1

如果你想要模板嵌套(类似于模板继承),可以尝试使用go(golang)模板管理器,特别适用于Web开发

一个建议的模板布局可以是:

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

英文:

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

huangapple
  • 本文由 发表于 2014年11月19日 13:42:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/27009798.html
  • directory-structure
  • go
  • templates
  • web-applications
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定