Reputation: 69
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,
Upvotes: 5
Views: 10795
Reputation: 26479
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
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). |
Folder | Description |
---|---|
/api | OpenAPI/Swagger specs, JSON schema files, protocol definition files. |
Folder | Description |
---|---|
/web | Web application specific components: static web assets, server side templates and SPAs. |
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. |
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. |
Upvotes: 12
Reputation: 11626
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.
Upvotes: 6