Reputation: 22134
I have a client and server application that share some code. I would like to group them all in a single directory under git control.
In this directory I can create two sub-directories, one for the client and one for the server. The compiled code will have the name of the directory provided I compile them using go install
from within their respective directory.
What is unclear is how to deal with the shared code (.go files) which I don't want to copy in each repository.
Am I required to create a separate package ? Can this package be stored in the common root directory ?
EDIT 1: Here is an example to make the question clear
The directory thing is for the project. It contains the thingClient and thingServer directory containing the client and server specific go files.
The client and server use common functions stored in specific files. I want these common files to be stored in .../go/src/thing so that they remain in the same git repository. I don't want them to be compiled into a shared library.
.../go/src/thing <-- under git control
.../go/src/thing/thingClient <-- contains main.go of client
.../go/src/thing/thingServer <-- contains main.go of server
.../go/src/thing/ ??? <-- contains go files used by client and server
I would prefer avoiding to create a package containing those common files if possible.
EDIT 2: According to this documentation How to Write Go Code I do have to create a package containing the shared code and import it in the client and server code. The package would be stored in a specific directory named for instance .../go/src/thing/common
. All go files inside common should declare to be member of the package named common. The client and server source files should then import thing/common
to access the shared source code.
Another important point is the project directory. To avoid package name collisions, the project directory should be renamed using a URL like path with a unique domain name, e.g. "github/user/thing". This will avoid any collision. The project source files should then be stored under .../go/src/github/user/thing/...
. The client and server should then import "github/user/thing/common".
Upvotes: 3
Views: 3178
Reputation: 2532
As per this, How to Write Go Code create a package containing the shared code and import it in the client and server code.
The package can be stored in a specific directory named for instance .../go/src/thing/common. All go files inside common should declare to be a member of the package named common. The client and server source files should then import thing/common to access the shared source code.
To avoid package name collisions, the project directory can be renamed using a URL like path with a unique domain name, e.g. "github.com/user/thing". This will avoid any collision. The project source files can then be stored under .../go/src/github.com/user/thing/....
The client and server can then import "github.com/user/thing/common".
If there are no source files directly under github.com/user/thing you can create a new empty file with just a package name in it to get around this until 1.8
Upvotes: 2