0

I am pretty new to go. We are currently splitting some microservices off of our monolithic Django+python web app and we have decided to do at least some of them in go. The problem is that the sources for the services are supposed to live in the same repo as the main app. So I have all the python code in ~/GloriousMomolith/thedjangoapp and split off services in ~/GloriousMomolith/services/some-service-name.

I could move ~/GloriousMonolith under ~/src (I have $GOPATH set to $HOME), but then every time I refer to a go package I create I would have to do import GloriousMomolith/services/someservice/somepackage. I want to avoid that. At the very least, I want to avoid having GloriousMomolith part hard-coded anywhere. Any suggestions?

Mad Wombat
  • 14,490
  • 14
  • 73
  • 109
  • Can you just put the packages in some other path? Why put the source if the GloriousMomolith, if you don't want it to be associated? – JimB Jun 06 '17 at 17:24
  • I want it to be associated. But the actual GloriousMomolith name is arbitrary and is different in different environments. Its just a location where I cloned the repo. So I don't want to hard-coded it anywhere. – Mad Wombat Jun 06 '17 at 17:31

1 Answers1

1

You can add a Go source directory to your project. For example:

~/
   GloriousMomolith/
      thedjangoapp/
      src/
         services/
            someservice/
               service.go

Set GOPATH to $HOME/GloriousMomolith:$HOME.

You can now import relative to the the src directory:

import (
   "services/someservice"
)
Charlie Tumahai
  • 113,709
  • 12
  • 249
  • 242
  • 1
    I did not realize I could have multiple paths in my $GOPATH. Thanks for the advice. I am thinking that I will have services/src/servicename and add ~/GloriousMomolith/services to my GOPATH. – Mad Wombat Jun 06 '17 at 17:32
  • Keep in mind that, while having multiple paths in your `$GOPATH` is possible, it tends to muck with things. The _intent_ of the `$GOPATH` is to be a singular directory for all of your projects, any many tools and instructions assume you only have a single path in that variable and/or won't play nicely with paths after the first (for example, `go get` always installs to the first path in the variable, with no option to alter that behavior). – Kaedys Jun 06 '17 at 17:58