how to write golang project layout

Introduction

Project layout is important in golang because each folder is a package. So I will follow up golang project layout and go project sample two projects integrate to my project layout.

Project layout

Go Directories

/cmd

Main applications for this project. (e.g., /cmd/todo-app)

/internal

Private application and library code. (e.go., /internal/app/todo, internal/pkg/lib).

/pkg

Library code that’s ok to use by external application (e.g., pkg/utils).

/pb

It’s protobuf for this project.

You can generate protobuf in Makefile.

1
2
3
4
.PHONY: gen-pb
gen-pb:
@protoc --go_out=paths=source_relative:. --go-grpc_out=paths=source_relative:. ./pb/*.proto
@echo Successfully generated proto

Service application directories

/api

OpenAPI/Swagger specs.

You can generate Swagger spec in Makefile.

1
2
3
.PHONY: gen-swagger
gen-swagger:
@swag init -g cmd/$(APP_NAME)/main.go --parseInternal -o api/docs

/web

Web application specific components: static web assets, server side templates and SPAs.

Common application directories

/configs

Configuration file templates or default configs.

/scripts

Scripts to perform various build, install, analysis, etc. operations.

/deployments

IaaS, PaaS, system and container orchestration deployment configurations and templates (docker-compose, kubernetes/helm, mesos, terraform, bosh).

/test

Additional external test apps and test data. For example, you can have /test/testdata.

Other directories

/docs

Design and user documents (in addition to your godoc generated documentation).

/examples

Examples for your applications and/or public libraries.

Example

You can view my complete open source repository.

Todo APP