JB

Golang Project Structure

Storing because I had to google it too many times.

I have a larger project that is better suited towards these kinds of posts however I’m going to dump this here because it’s getting annoying having to constantly look it up.

For a basic project, all you really need is this:

src
├── cmd
│   └── app_name 
│       └── main.go
└── internal
    └── namespace
        └── namespace.go
        └── subfunc.go
    └── namespace
        └── namespace.go
        └── subfunc.go

The above covers the use cases for the majority of smaller projects intended to deploy docker containers. Here’s an explanation of their usage:

File Usage
cmd/app_name Used to define the binaries that you compile.
internal/ Contains all of the included functions that you use in each cmd
internal/namespace You’d import this with import ( {modname}/internal/namespace )

Go is a bit of a quirky language. This style ensures compliance with the compilers expectations while keeping your project logical and understandable by people who develop Go code.

The only other directory that you may want to consider is src/api. This directory documents the API spec for your project, and could be useful.

The general recommendation on GoLang directory structure is documented here: https://github.com/golang-standards/project-layout/tree/master

You can see on major projects how this seems to work nicely for readability: https://github.com/kubernetes/kubernetes/tree/master