2

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".

chmike
  • 20,922
  • 21
  • 83
  • 106
  • 1
    Related / possible duplicate of [Splitting client/server code](http://stackoverflow.com/questions/38875016/splitting-client-server-code) – icza Sep 02 '16 at 10:01
  • And don't forget about the workaround for [this](https://golang.org/issue/15093) – Sridhar Sep 02 '16 at 10:09
  • @icza It's not a duplicate in that I'm not concerned by dead code removal. ? I'll give an example to make my question more clear. – chmike Sep 02 '16 at 10:32
  • Maybe I don't understand the question, but what's the problem with importing from a shared package ? – Sridhar Sep 02 '16 at 11:18
  • @Sridhar I thought that creating a package would require me to create a directory outside of the project directory `thing`. See my Edit2. I now understood how it must be done. If you provide this info as answer I'll validate it. I can't answer myself yet. – chmike Sep 02 '16 at 11:30
  • I'm pretty sure you can create a directory called util under thing and have all files in util as package util. You should then be able to import thing/util from your client & server – Sridhar Sep 02 '16 at 11:36

1 Answers1

2

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

Sridhar
  • 2,416
  • 1
  • 26
  • 35