英文:
Go Lang folder structure for a web app
问题
如你所知,Go是一种现代的面向对象编程方法,我个人认为它非常出色,比如强制使用组合而不是继承。我只是想了解一个良好编写的Go代码在文件夹和包结构方面应该如何设计。
我只是想知道下面这种类似React的方法是否可行-将应用程序划分为不同的包作为组件?
或者你可以给我一个示例、链接或者关于Web API文件夹/包结构的好的设计思路。Go只会作为我开源项目的后台API,客户端我计划使用单页React应用。
非常感谢。
英文:
As you know Go is a modern approach to OOP imo, with brilliant things like forcing you to use composition over inheritance. I am just trying to understand how a well written go code has to be designed in terms of folder and package structure.
I am just wondering if this React like approach which can be seen below - dividing app as components to different packages can work?
Or can you give me an example, link, idea for a good structure in terms of folder/packaging of a web api. Go will only be an api in the background to my open source project, on client side I am planing to have a single-page React app btw.
Thanks a lot,
答案1
得分: 12
标准的Go项目布局
GitHub上有一个拥有很多星星的仓库(截至撰写本文时有22k个星星)
这种方法实际上非常完整,但对于较小的项目可能有些过度。
https://github.com/golang-standards/project-layout
Go目录(基础)
文件夹 | 描述 |
---|---|
/cmd | 该项目的主要应用程序。 |
/internal | 私有应用程序和库代码。 |
/pkg | 可供外部应用程序使用的库代码。 |
/vendor | 应用程序依赖项(手动管理或使用您喜欢的依赖管理工具,如新的内置Go模块功能)。 |
服务应用程序目录
文件夹 | 描述 |
---|---|
/api | OpenAPI/Swagger规范,JSON模式文件,协议定义文件。 |
Web应用程序目录
文件夹 | 描述 |
---|---|
/web | Web应用程序特定组件:静态Web资源,服务器端模板和单页应用程序。 |
通用应用程序目录
文件夹 | 描述 |
---|---|
/configs | 配置文件模板或默认配置。 |
/init | 系统初始化(systemd,upstart,sysv)和进程管理器/监视器(runit,supervisord)配置。 |
/scripts | 执行各种构建、安装、分析等操作的脚本。 |
/build | 打包和持续集成。 |
/deployments | IaaS、PaaS、系统和容器编排部署配置和模板。 |
/test | 附加的外部测试应用程序和测试数据。 |
其他目录
文件夹 | 描述 |
---|---|
/docs | 设计和用户文档(除了您的godoc生成的文档)。 |
/tools | 该项目的支持工具。 |
/examples | 应用程序和/或公共库的示例。 |
/third_party | 外部辅助工具、分叉代码和其他第三方实用程序(例如Swagger UI)。 |
/githooks | Git钩子。 |
/assets | 与您的存储库一起使用的其他资产(图像、标志等)。 |
/website | 如果您不使用GitHub页面,则将项目的网站数据放在这里。 |
英文:
Standard Go Project Layout
There is a Repo on GitHub that has lot's of stars (22k as of writing)
This approach is actually quite complete but might be overkill for smaller projects.
https://github.com/golang-standards/project-layout
Go Directories (Base)
Folder | Description |
---|---|
/cmd | Main applications for this project. |
/internal | Private application and library code. |
/pkg | Library code that's ok to use by external applications. |
/vendor | Application dependencies (managed manually or by your favorite dependency management tool like the new built-in Go Modules feature). |
Service Application Directories
Folder | Description |
---|---|
/api | OpenAPI/Swagger specs, JSON schema files, protocol definition files. |
Web Application Directories
Folder | Description |
---|---|
/web | Web application specific components: static web assets, server side templates and SPAs. |
Common Application Directories
Folder | Description |
---|---|
/configs | Configuration file templates or default configs. |
/init | System init (systemd, upstart, sysv) and process manager/supervisor (runit, supervisord) configs. |
/scripts | Scripts to perform various build, install, analysis, etc operations. |
/build | Packaging and Continuous Integration. |
/deployments | IaaS, PaaS, system and container orchestration deployment configurations and templates. |
/test | Additional external test apps and test data. |
Other Directories
Folder | Description |
---|---|
/docs | Design and user documents (in addition to your godoc generated documentation). |
/tools | Supporting tools for this project. |
/examples | Examples for your applications and/or public libraries. |
/third_party | External helper tools, forked code and other 3rd party utilities (e.g., Swagger UI). |
/githooks | Git hooks. |
/assets | Other assets to go along with your repository (images, logos, etc). |
/website | This is the place to put your project's website data if you are not using GitHub pages. |
答案2
得分: 6
我倾向于将我的应用程序结构化如下:
$GOPATH/github.com/yourname/projectname/
cmd/
app1/
main.go
app2/
main.go
db/
001_initial_schema.sql
002_add_timestamps.sql
...等等...
lib/
lib1/
lib2/
html/
..所有的HTML内容..
其中app1/app2是命令行应用程序。通常情况下,可能只有一个应用程序(你的Web服务器)。
lib/*只是你拥有的功能模块的分离部分。
通常,我从只有cmd/app1开始,然后在项目变得足够复杂时扩展到lib。
如果你有静态文件服务器(假设你有一个),可以将"html"作为目录。
对于数据库迁移,我使用了一个我自己编写的非常简单的迁移工具,因为我觉得其他工具太复杂/太庞大。
这是我在项目中使用的代码。
我曾考虑过将其制作成一个真正的库,但我相当确定这是特定于PostgreSQL的,因为它假设DDL是事务性的。
有了这个结构,你可以简单地在项目根目录执行以下命令:
go install ./... && app1
来构建/测试你的应用程序。
如果你想要部署到Heroku,这个结构也自然适用,因为Heroku会将你的工作目录设置为项目根目录。
英文:
I tend to structure my apps as:
$GOPATH/github.com/yourname/projectname/
cmd/
app1/
main.go
app2/
main.go
db/
001_initial_schema.sql
002_add_timestamps.sql
... etc ...
lib/
lib1/
lib2/
html/
..all the html stuff..
where app1 / app2 are the command line apps. Often it is only be one app (your web server).
lib/* is just whatever segregated pieces of functionality you have.
Usually, i start with just cmd/app1 and then expand in to lib when the project gets sufficiently complex.
And make your static file server (assuming you have one), use "html" as the directory.
For database migrations, I use a really simple migrator I wrote as I find the others to be too complicated / too large.
Here's the code I'm using in my projects.
I thought of making it a real library, but I'm pretty sure this is postgresql specific as it assumes that DDL is transactional.
With this structure, you can simply do (from the project root):
go install ./... && app1
To build / test your app.
This structure will also naturally work if you then want to deploy to Heroku as heroku sets your working dir to your project root.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论