Golang code organization for multi-platform multi-language project
code-organization, go, organization, project-organization
Solution
This is how I organized a similar project :
$GOPATH/src/project-root/
lib.go
lib_test.go
server/
server.go
server_test.go
main/
server.go // package main; import "project-root/server"
client/
client.go
client_test.go
main/
client.go //package main; import "project-root/client"
client-ios/
....
client-android/
....
While mostly having `server/server.go` and `client/client.go` as `package main` should work, it's better to separate it so you could embed the client/server in other projects.
Problem
I am looking for a good project organization for a multi-platform project with multiple components written in Go. I am aware of the recommended layout from http://golang.org/doc/code.html, but the layout suggested there does not seem to fit my requirements. Project components are: - server (written in Go) - client, cross-platform (Go) - library, shared between server and client (Go) - some more clients (iOS, Android) My requirements are: - All components in a single git repository - Keep components separate (e.g. one directory per component) - Go components can be structured into multiple sub-packages My current approach: ``` project/ (this is the repository root) server/ server.go (package main) src/ server/ package1/ package1.go ... client/ client.go (package main) src/ client/ package2/ package2.go ... lib/ src/ lib/ lib.go ... client-ios/ ... client-android/ ... ``` To build, I use a Makefile which - Copies lib/ into both server/ and client/ - Builds server/ and client/ separately, setting GOPATH every time to the respective directory. It works, but feels really klunky and is quite different to the recommended code layout. Here is the alternative I am considering: ``` project/ (this is the repository root) gospace/ src/ server/... client/... lib/... client-ios/ ... client-android/ ... ``` With this layout I have a single GOPATH (gospace/) and don't need the klunky Makefile. However, the components are not separated as neatly as in the first alternative (i.e. via a top level directory). My question: Which project layout would be best suited for both my requirements and Go conventions and tooling support? Are there better options which I did not see yet?