英文:
Handling Go Git repositories and its non-code resources
问题
我是Go的新手,我正在努力寻找处理Git仓库的最佳方法。
目前我正在使用Go官方提议的目录结构,但我认为它有很多缺点。
它大致是这样的:
$GOPATH
bin
非重要的东西
pkg
非重要的东西
src
github.com
用户名
仓库名
问题是我有很多非代码资源,比如默认的设置文件、HTML模板和图片...但是把它放在"src/github.com/用户名/仓库名"目录下对我来说有点奇怪(因为第一个"src"部分)。
这个问题的最常见解决方案是什么?
我现在面临的另一个问题是,我不知道构建整个项目的最常见的"make"工具,这些工具可以处理外部资源,比如默认的设置文件或HTML("go build/install"命令只处理源代码文件)。当可执行文件被"安装"时,你是如何处理相对路径的?
**免责声明:**我已经阅读了这个帖子:https://stackoverflow.com/questions/9443418/how-to-access-resource-files-after-the-go-tool-installed-the-executable,但我没有看到可靠的解决方案。我的可执行文件是一个服务器,每次重新配置服务器时嵌入设置文件并重新编译是不可行的(通过执行参数传递所有设置参数也不可行,因为设置文件非常复杂)。
英文:
I'm new to Go and I'm struggling to find the best way to handle Git repositories.
At this moment I'm using the Go's official proposed directories structure, but I think it has many pitfalls.
It's something like
$GOPATH
bin
non-important-stuff
pkg
non-important-stuff
src
github.com
username
reponame
The case is that i have many non-code resources, like default settings files, html templates and images... but it seems a little weird to me place it in the "src/github.com/username/reponame" directory (because the first "src" part).
What are the most common solutions to this problem?
Another problem I'm facing now is that I don't know the most common "make" tools to build an entire project with external resources like default settings files or html (the "go build/install" commands only take care of source code files). How do you take care of relative paths when the executable is "installed"?
Disclaimer: I've read this thread: https://stackoverflow.com/questions/9443418/how-to-access-resource-files-after-the-go-tool-installed-the-executable , but I didn't see reliable solutions. My executable is a server, and it's not an option to embed the settings files and recompile every time I want to reconfigure the server (and its not an option to pass all the settings parameters via execution parameters, because the settings file is very complex).
答案1
得分: 1
我假设你在谈论网页开发。
正如你正确指出的,目录结构是为代码而设计的,go工具套件只负责构建二进制文件并将其放在一个传统的位置(pkg
或bin
)。
你想要的是一个完整的二进制文件+资源文件的部署过程。
你可以自己编写部署过程,或者使用现有的框架。
一个自制部署过程的草图:
- 将所有资源放在项目的特定目录下(例如
resources/
) - 使用
git archive resources/
打包文件 - 将文件复制到可执行文件旁边
- 选择一种方式来调整本地参数(数据库服务器,数据库名称等)
- 选择一种方式让服务器重新加载资源(重新启动服务器/系统地重新读取资源/检查修改日期等)
如果你正在寻找现有的Web框架:
Revel是一个选择,它可以处理很多问题,包括开发过程中的热重新编译和部署过程。
英文:
I assume you are talking about web development.
As you correctly noted, the directory structure is meant for the code, and the go tool suite only takes care of building binaries and putting them in a conventional place (pkg
or bin
).
What you want is a complete deployment procedure for a binary + resource files.
You should either roll your own deployment procedure, or use an existing framework.
A sketch of a homemade procedure :
- put all the resources under a specific directory in your project (e.g.
resources/
) - use
git archive resources/
to bundle the files - copy the files next to your executable
- choose a way to adjust the local parameters (db server, db name, etc..)
- choose a way to make to have your server reload the resources (restart your server / systematically re-read the resources / check modification dates / ... )
If you are looking for an existing web framework :
Revel is one option, which takes care of a good number of points, among which hot recompilation during development, and a deployment procedure.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论