project-layout/README.md

158 lines
7.4 KiB
Markdown
Raw Normal View History

2017-09-10 00:39:12 +08:00
# Standard Go Project Layout
2018-07-16 04:30:46 +08:00
This is a basic layout for Go application projects. It represents the most common directory structure with a number of small enhancements along with several supporting directories common to any large enough real world application.
2018-07-16 04:30:46 +08:00
If you are trying to learn Go or if you are building a PoC or a toy project for yourself this project layout is an overkill. Start with something really simple (a single `main.go` file is more than enough). As your project grows keep in mind that it'll be important to make sure your code is well structured otherwise you'll end up with a messy code with lots of hidden dependencies and global state. When you have more people working on the project you'll need even more structure. That's when it's important to introduce a common way to manage packages/libraries. When you have an open source project or when you know other projects import the code from your project repository that's when it's important to have private (aka `internal`) packages and code. Clone the repository, keep what you need and delete everything else! Just because it's there it doesn't mean you have to use it all.
2017-09-10 04:10:53 +08:00
2018-07-16 04:30:46 +08:00
This project layout is intentionally generic and it doesn't try to impose a specific Go package structure.
2017-09-10 06:18:01 +08:00
If you need help with naming, formatting and style start by running [`gofmt`](https://golang.org/cmd/gofmt/) and [`golint`](https://github.com/golang/lint). Also make sure to read these Go code style guidelines and recommendations:
* https://talks.golang.org/2014/names.slide
* https://golang.org/doc/effective_go.html#names
* https://blog.golang.org/package-names
* https://github.com/golang/go/wiki/CodeReviewComments
See [Go Project Layout](https://medium.com/golang-learn/go-project-layout-e5213cdcfaa2) for additional background information.
2017-09-12 05:09:15 +08:00
2017-09-10 06:18:01 +08:00
## Go Directories
2017-09-12 02:50:28 +08:00
### `/cmd`
2017-09-10 06:18:01 +08:00
Main applications for this project.
The directory name for each application should match the name of the executable you want to have (e.g., `/cmd/myapp`).
Don't put a lot of code in the application directory. If you think the code can be imported and used in other projects, then it should live in the `/pkg` directory. If the code is not reusable or if you don't want others to reuse it, put that code in the `/internal` directory. You'll be surprised what others will do, so be explicit about your intentions!
2017-09-10 06:18:01 +08:00
It's common to have a small `main` function that imports and invokes the code from the `/internal` and `/pkg` directories and nothing else.
2017-09-10 06:18:01 +08:00
2018-05-14 01:08:28 +08:00
See the [`/cmd`](cmd/README.md) directory for examples.
2018-04-29 13:33:08 +08:00
2017-09-12 02:50:28 +08:00
### `/internal`
2017-09-10 06:18:01 +08:00
Private application and library code. This is the code you don't want others importing in their applications or libraries.
2017-09-10 06:18:01 +08:00
Put your actual application code in the `/internal/app` directory (e.g., `/internal/app/myapp`) and the code shared by those apps in the `/internal/pkg` directory (e.g., `/internal/pkg/myprivlib`).
2017-09-12 02:50:28 +08:00
### `/pkg`
2017-09-10 06:18:01 +08:00
Library code that's safe to use by external applications (e.g., `/pkg/mypubliclib`).
Other projects will import these libraries expecting them to work, so think twice before you put something here :-)
2018-05-14 01:14:35 +08:00
See the [`/pkg`](pkg/README.md) directory for examples.
2018-04-29 23:42:16 +08:00
2017-09-12 02:50:28 +08:00
### `/vendor`
2017-09-10 06:18:01 +08:00
2018-06-28 12:05:15 +08:00
Application dependencies (managed manually or by your favorite dependency management tool like [`dep`](https://github.com/golang/dep)).
2017-09-10 06:18:01 +08:00
Don't commit your application dependencies if you are building a library.
## Service Application Directories
2017-09-12 02:50:28 +08:00
### `/api`
2017-09-10 06:18:01 +08:00
OpenAPI/Swagger specs, JSON schema files, protocol definition files.
2018-05-14 01:14:35 +08:00
See the [`/api`](api/README.md) directory for examples.
2018-04-29 13:33:08 +08:00
2017-09-10 06:18:01 +08:00
## Web Application Directories
2017-09-12 02:50:28 +08:00
### `/web`
2017-09-10 06:18:01 +08:00
Web application specific components: static web assets, server side templates and SPAs.
## Common Application Directories
2017-09-12 02:50:28 +08:00
### `/configs`
2017-09-10 06:18:01 +08:00
Configuration file templates or default configs.
2018-05-11 11:03:44 +08:00
Put your `confd` or `consul-template` template files here.
2017-09-10 06:18:01 +08:00
2017-09-12 02:50:28 +08:00
### `/init`
2017-09-10 06:18:01 +08:00
System init (systemd, upstart, sysv) and process manager/supervisor (runit, supervisord) configs.
2017-09-12 02:50:28 +08:00
### `/scripts`
2017-09-10 06:18:01 +08:00
Scripts to perform various build, install, analysis, etc operations.
2018-04-29 23:42:16 +08:00
These scripts keep the root level Makefile small and simple (e.g., `https://github.com/hashicorp/terraform/blob/master/Makefile`).
2018-05-14 01:14:35 +08:00
See the [`/scripts`](scripts/README.md) directory for examples.
2017-09-10 06:18:01 +08:00
2017-09-12 02:50:28 +08:00
### `/build`
2017-09-10 06:18:01 +08:00
Packaging and Continous Integration.
Put your cloud (AMI), container (Docker), OS (deb, rpm, pkg) package configurations and scripts in the `/build/package` directory.
Put your CI (travis, circle, drone) configurations and scripts in the `/build/ci` directory. Note that some of the CI tools (e.g., Travis CI) are very picky about the location of their config files. Try putting the config files in the `/build/ci` directory linking them to the location where the CI tools expect them (when possible).
2017-09-10 06:18:01 +08:00
2017-09-12 02:50:28 +08:00
### `/deployments`
2017-09-10 06:18:01 +08:00
2017-09-11 14:17:05 +08:00
IaaS, PaaS, system and container orchestration deployment configurations and templates (docker-compose, kubernetes/helm, mesos, terraform, bosh).
2017-09-10 06:18:01 +08:00
2017-09-12 02:50:28 +08:00
### `/test`
2017-09-10 06:18:01 +08:00
Additional external test apps and test data. Feel free to structure the `/test` directory anyway you want. For bigger projects it makes sense to have a data subdirectory. For example, you can have `/test/data` or `/test/testdata` if you need Go to ignore what's in that directory. Note that Go will also ignore directories or files that begin with "." or "_", so you have more flexibility in terms of how you name your test data directory.
2017-09-10 06:18:01 +08:00
2018-05-14 01:14:35 +08:00
See the [`/test`](test/README.md) directory for examples.
2018-04-29 13:33:08 +08:00
2017-09-10 06:18:01 +08:00
## Other Directories
2017-09-12 02:50:28 +08:00
### `/docs`
2017-09-10 06:18:01 +08:00
2017-09-11 14:17:05 +08:00
Design and user documents (in addition to your godoc generated documentation).
2017-09-10 06:18:01 +08:00
2018-05-14 01:14:35 +08:00
See the [`/docs`](docs/README.md) directory for examples.
2018-04-29 13:33:08 +08:00
2017-09-12 02:50:28 +08:00
### `/tools`
2017-09-10 06:18:01 +08:00
Supporting tools for this project. Note that these tools can import code from the `/pkg` and `/internal` directories.
2018-05-14 01:14:35 +08:00
See the [`/tools`](tools/README.md) directory for examples.
2018-04-29 13:33:08 +08:00
2017-09-12 02:50:28 +08:00
### `/examples`
2017-09-10 06:18:01 +08:00
Examples for your applications and/or public libraries.
2018-05-14 01:14:35 +08:00
See the [`/examples`](examples/README.md) directory for examples.
2018-04-29 13:33:08 +08:00
2017-09-12 02:50:28 +08:00
### `/third_party`
2017-09-10 06:18:01 +08:00
External helper tools, forked code and other 3rd party utilities (e.g., Swagger UI).
2017-09-12 02:50:28 +08:00
### `/githooks`
2017-09-10 06:18:01 +08:00
Git hooks.
2017-09-12 02:50:28 +08:00
### `/assets`
2017-09-10 06:18:01 +08:00
Other assets to go along with your repository.
## Directories You Shouldn't Have
### `/src`
Some Go projects do have a `src` folder, but it usually happens when the devs came from the Java world where it's a common pattern. If you can help yourself try not to adopt this Java pattern. You really don't want your Go code or Go projects to look like Java :-)
2017-09-11 14:17:05 +08:00
## Badges
2017-09-10 06:18:01 +08:00
2017-09-12 02:16:11 +08:00
* [Go Report Card](https://goreportcard.com/) - It will scan your code with `gofmt`, `go vet`, `gocyclo`, `golint`, `ineffassign`, `license` and `misspell`. Replace `github.com/golang-standards/project-layout` with your project reference.
* [GoDoc](http://godoc.org) - It will provide online version of your GoDoc generated documentation. Change the link to point to your project.
* Release - It will show the latest release number for your project. Change the github link to point to your project.
2017-09-11 14:17:05 +08:00
[![Go Report Card](https://goreportcard.com/badge/github.com/golang-standards/project-layout?style=flat-square)](https://goreportcard.com/report/github.com/golang-standards/project-layout)
[![Go Doc](https://img.shields.io/badge/godoc-reference-blue.svg?style=flat-square)](http://godoc.org/github.com/golang-standards/project-layout)
[![Release](https://img.shields.io/github/release/golang-standards/project-layout.svg?style=flat-square)](https://github.com/golang-standards/project-layout/releases/latest)
2017-09-10 06:18:01 +08:00
2017-09-12 02:16:11 +08:00
## Notes
2017-09-10 06:18:01 +08:00
2017-09-12 02:16:11 +08:00
A more opinionated project template with sample/reusable configs, scripts and code is a WIP.
2017-09-10 04:10:53 +08:00