Standard Go Project Layout
The developer of this repository has not created any items for sale yet. Need a bug fixed? Help with integration? A different license? Create a request here:
This is a basic layout for Go application projects. It's not an official standard defined by the core Go dev team; however, it is a set of common historical and emerging project layout patterns in the Go ecosystem. Some of these patterns are more popular than others. It also has a number of small enhancements along with several supporting directories common to any large enough real world application.
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
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
) 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. None of these patterns are used in every single project. Even the
pattern is not universal.
With Go 1.14 [
](https://github.com/golang/go/wiki/Modules) are finally ready for production. Use [
](https://blog.golang.org/using-go-modules) unless you have a specific reason not to use them and if you do then you don’t need to worry about $GOPATH and where you put your project. The basic
file in the repo assumes your project is hosted on Github, but it's not a requirement. The module path can be anything though the first module path component should have a dot in its name (the current version of Go doesn't enforce it anymore, but if you are using slightly older versions don't be surprised if your builds fail without it). See Issues [
](https://github.com/golang/go/issues/32819) if you want to know more about it.
This project layout is intentionally generic and it doesn't try to impose a specific Go package structure.
This is a community effort. Open an issue if you see a new pattern or if you think one of the existing patterns needs to be updated.
If you need help with naming, formatting and style start by running [
](https://golang.org/cmd/gofmt/) and [
](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 * Style guideline for Go packages (rakyll/JBD)
Go Project Layout
](https://medium.com/golang-learn/go-project-layout-e5213cdcfaa2) for additional background information.
More about naming and organizing packages as well as other code structure recommendations: * GopherCon EU 2018: Peter Bourgon - Best Practices for Industrial Programming* GopherCon Russia 2018: Ashley McNamara + Brian Ketelsen - Go best practices.* GopherCon 2017: Edward Muller - Go Anti-Patterns* GopherCon 2018: Kat Zien - How Do You Structure Your Go Apps
Main applications for this project.
The directory name for each application should match the name of the executable you want to have (e.g.,
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
directory. If the code is not reusable or if you don't want others to reuse it, put that code in the
directory. You'll be surprised what others will do, so be explicit about your intentions!
It's common to have a small
function that imports and invokes the code from the
directories and nothing else.
See the [
](https://github.com/golang-standards/project-layout/blob/master/cmd/README.md) directory for examples.
Private application and library code. This is the code you don't want others importing in their applications or libraries. Note that this layout pattern is enforced by the Go compiler itself. See the Go 1.4 [
](https://golang.org/doc/go1.4#internalpackages) for more details. Note that you are not limited to the top level
directory. You can have more than one
directory at any level of your project tree.
You can optionally add a bit of extra structure to your internal packages to separate your shared and non-shared internal code. It's not required (especially for smaller projects), but it's nice to have visual clues showing the intended package use. Your actual application code can go in the
) and the code shared by those apps in the
Library code that's ok to use by external applications (e.g.,
). Other projects will import these libraries expecting them to work, so think twice before you put something here :-) Note that the
directory is a better way to ensure your private packages are not importable because it's enforced by Go. The
directory is still a good way to explicitly communicate that the code in that directory is safe for use by others. The [
I'll take pkg over internal
](https://travisjeffery.com/b/2019/11/i-ll-take-pkg-over-internal/) blog post by Travis Jeffery provides a good overview of the
directories and when it might make sense to use them.
It's also a way to group Go code in one place when your root directory contains lots of non-Go components and directories making it easier to run various Go tools (as mentioned in these talks: [
Best Practices for Industrial Programming
](https://www.youtube.com/watch?v=PTE4VJIdHPg) from GopherCon EU 2018, GopherCon 2018: Kat Zien - How Do You Structure Your Go Apps and GoLab 2018 - Massimiliano Pippi - Project layout patterns in Go).
See the [
](https://github.com/golang-standards/project-layout/blob/master/pkg/README.md) directory if you want to see which popular Go repos use this project layout pattern. This is a common layout pattern, but it's not universally accepted and some in the Go community don't recommend it.
Ok not to use it if your app project is really small and where an extra level of nesting doesn't add much value (unless you really want to :-)). Think about it when it's getting big enough and your root directory gets pretty busy (especially if you have a lot of non-Go app components).
Application dependencies (managed manually or by your favorite dependency management tool like the new built-in [
](https://github.com/golang/go/wiki/Modules) feature). The
go mod vendor
command will create the
directory for you. Note that you might need to add the
flag to your
command if you are not using Go 1.14 where it's on by default.
Don't commit your application dependencies if you are building a library.
Note that since [
](https://golang.org/doc/go1.13#modules) Go also enabled the module proxy feature (using [
](https://proxy.golang.org) as their module proxy server by default). Read more about it [
](https://blog.golang.org/module-mirror-launch) to see if it fits all of your requirements and constraints. If it does, then you won't need the
directory at all.
OpenAPI/Swagger specs, JSON schema files, protocol definition files.
See the [
](https://github.com/golang-standards/project-layout/blob/master/api/README.md) directory for examples.
Web application specific components: static web assets, server side templates and SPAs.
Configuration file templates or default configs.
template files here.
System init (systemd, upstart, sysv) and process manager/supervisor (runit, supervisord) configs.
Scripts to perform various build, install, analysis, etc operations.
These scripts keep the root level Makefile small and simple (e.g., [
See the [
](https://github.com/golang-standards/project-layout/blob/master/scripts/README.md) directory for examples.
Packaging and Continuous Integration.
Put your cloud (AMI), container (Docker), OS (deb, rpm, pkg) package configurations and scripts in the
Put your CI (travis, circle, drone) configurations and scripts in the
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
directory linking them to the location where the CI tools expect them (when possible).
IaaS, PaaS, system and container orchestration deployment configurations and templates (docker-compose, kubernetes/helm, mesos, terraform, bosh). Note that in some repos (especially apps deployed with kubernetes) this directory is called
Additional external test apps and test data. Feel free to structure the
directory anyway you want. For bigger projects it makes sense to have a data subdirectory. For example, you can have
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.
See the [
](https://github.com/golang-standards/project-layout/blob/master/test/README.md) directory for examples.
Design and user documents (in addition to your godoc generated documentation).
See the [
](https://github.com/golang-standards/project-layout/blob/master/docs/README.md) directory for examples.
Supporting tools for this project. Note that these tools can import code from the
See the [
](https://github.com/golang-standards/project-layout/blob/master/tools/README.md) directory for examples.
Examples for your applications and/or public libraries.
See the [
](https://github.com/golang-standards/project-layout/blob/master/examples/README.md) directory for examples.
External helper tools, forked code and other 3rd party utilities (e.g., Swagger UI).
Other assets to go along with your repository (images, logos, etc).
This is the place to put your project's website data if you are not using Github pages.
See the [
](https://github.com/golang-standards/project-layout/blob/master/website/README.md) directory for examples.
Some Go projects do have a
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 :-)
Don't confuse the project level
directory with the
directory Go uses for its workspaces as described in [
How to Write Go Code
environment variable points to your (current) workspace (by default it points to
on non-windows systems). This workspace includes the top level
directories. Your actual project ends up being a sub-directory under
, so if you have the
directory in your project the project path will look like this:
. Note that with Go 1.11 it's possible to have your project outside of your
, but it still doesn't mean it's a good idea to use this layout pattern.
Go Report Card - It will scan your code with
with your project reference.
GoDoc - 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.
A more opinionated project template with sample/reusable configs, scripts and code is a WIP.