Search code examples
gogopath

How to add go code into an existing project


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?


Solution

  • 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"
    )