title: Why can't I add a main to my library in golang?
body: <p>I'm having trouble achieving what should be an easy task. I understand the GitHub model for code organization (ie a library repo and an app repo that consumes the library). I think it's fantastic. But I find often that I want <code>mylib</code> to come bundled with a simple executable in a single <code>main.go</code> file. The <code>main.go</code> should be <code>package main</code> and should import <code>mylib</code>. In other words, it should be an exact documentation of how to build an app that consumes this library. </p>
  
  <p>My point is, since it is often enough convenient to provide a simple command line interface that wraps your library, there should be an easy way to do this without having to make another repo, and golang should help.</p>
  
  <p>I'd like something like the following:</p>
  
  <pre><code>$GOPATH/src/github.com/me/mylib
      mylib.go
      mylib_also.go
      main.go
  </code></pre>
  
  <p>where <code>mylib</code> is the library (<code>package mylib</code>) and <code>main.go</code> is <code>package main</code> and on running <code>go install</code> it generates <code>bin/mylib</code> and <code>pkg/mylib.a</code>. </p>
  
  <p>Either <code>main.go</code> should import <code>"github.com/me/mylib"</code> (if I do that now, I get cyclical import error) or <code>go</code> would understand what's happening since this feature should be built in and the one <code>main.go</code> in the repo generates the exec. Probably requiring the import (and dropping the cyclical error) is the better way.</p>
  
  <p>Right now, I have to do </p>
  
  <pre><code>$GOPATH/src/github.com/me/mylib
      mylib/
          mylib.go
      main.go
  </code></pre>
  
  <p>So I have to import github.com/me/mylib/mylib which is ridiculous.</p>
  
  <p>In sum, <code>go install</code> should allow the special case of a package and a main which imports the package and provides a simple cli that demonstrates the packages API. The GitHub model promotes two repos, but it should be easy to do simple clis in one!</p>