title: How to setup protobuf, grpc and Go modules in a monorepo?
body: <p>I'm trying to create a microservice-based API in Go, very basic, just to learn some stuff. I have three main goals I have to achieve.</p>
  
  <ol>
  <li>Monorepo.</li>
  <li>Be able to use different back-end languages as need arises (ie. one microservice in Go, two in Node.js, one in Python).</li>
  <li>Use gRPC.</li>
  </ol>
  
  <p>Currently my structure looks like this (and is stored in <code>~/Projects/tkg</code>, outside of GOPATH).</p>
  
  <p><a href="https://i.stack.imgur.com/oYOkn.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/oYOkn.png" alt="project structure"></a></p>
  
  <p>Each "service" should be a self-contained application written in a "whatever". As you can see I have a Go service and a React front-end application. Additionally there is a Makefile there that I want to use for building stuff, but I might move to shell scripts, Docker, whatever. Doesn't matter.</p>
  
  <p>So now the question. How can I make generated proto files play well with this setup? I think I don't understand something about Go modules and packages because I cannot set it so <code>articles.go</code> (from <code>cmd</code>) can access the generated <code>api/article.pb.go</code>. How to do it?</p>
  
  <pre class="lang-golang prettyprint-override"><code>// services/articles/go.mod
  module tkg/services/articles
  
  go 1.12
  
  require (
      github.com/golang/protobuf v1.3.2
      google.golang.org/grpc v1.22.1
  )
  </code></pre>
  
  <pre class="lang-golang prettyprint-override"><code>// services/articles/cmd/article.go
  package main
  
  import (
      pb "tkg/services/articles/api/article"
  )
  
  type repository interface {
      Create(*pb.Article) (*pb.Article, error)
  }
  
  func main() {
  
  }
  </code></pre>
  
  <pre class="lang-proto prettyprint-override"><code>// services/articles/api/article.proto
  syntax = "proto3";
  
  package article;
  option go_package = "tkg/services/articles/api/article";
  ...
  </code></pre>
  
  <pre><code>// Makefile
  build:
      protoc services/articles/api/article.proto --go_out=.
  </code></pre>
  
  <p>I have tried various different package names in go.mod, different go_packages in the proto file, I had tried different protoc commands and paths. I bet this is silly and it's very obvious to someone who is well-versed in Go, but for someone from Node.js backgroud like me the inability to do <code>import "../api/article.pb.go"</code> is infuriating. :(</p>
  
  <p>The error I am getting is: <code>could not import tkg/services/articles/api/article (no parsed files for package tkg/services/articles/api/article)</code>. Of course with different values for package names. I've been trying to solve it for two days now.</p>
  
  <p>How would you approach this problem?</p>