title: Organize local code in packages using Go modules
body: <p>I can not find a way to factor out some code from <code>main.go</code> into a local package when using Go modules (go version >= 1.11) outside of <code>$GOPATH</code>.</p>
  
  <p>I am not importing any external dependencies that need to be included into <code>go.mod</code>, I am just trying to organize locally the source code of this Go module.</p>
  
  <p>The file <code>main.go</code>:</p>
  
  <pre class="lang-golang prettyprint-override"><code>package main
  
  // this import does not work
  import "./stuff"
  
  func main() {
      stuff.PrintBaz()
  }
  </code></pre>
  
  <p>The file <code>./stuff/bar.go</code> (pretending to be a local package):</p>
  
  <pre class="lang-golang prettyprint-override"><code>package stuff
  
  import "log"
  
  type Bar struct {
      Baz int
  }
  
  func PrintBaz() {
      baz := Bar{42}
      log.Printf("Bar struct: %v", baz)
  }
  </code></pre>
  
  <p>The file <code>go.mod</code> (command <code>go mod init foo</code>):</p>
  
  <pre><code>module foo
  
  go 1.12
  </code></pre>
  
  <p>When executing <code>go run main.go</code>:</p>
  
  <ul>
  <li>If I <code>import "./stuff"</code>, then I see <code>build command-line-arguments: cannot find module for path _/home/&lt;PATH_TO&gt;/fooprj/stuff</code>.</li>
  <li>If I <code>import "stuff"</code>, then I see <code>build command-line-arguments: cannot load stuff: cannot find module providing package stuff</code>.</li>
  <li>If I <code>import stuff "./stuff"</code> with a package alias, then I see again: <code>build command-line-arguments: cannot find module for path _/home/&lt;PATH_TO&gt;/fooprj/stuff</code>.</li>
  </ul>
  
  <p>I can not find a way to make local packages work with go modules.</p>
  
  <ul>
  <li>What's wrong with the code above?</li>
  <li>How can I import local packages into other Go code inside a project defined with Go modules (file <code>go.mod</code>)?</li>
  </ul>