title: How to properly import a package from sub-directory in Golang?
body: <p>I am pretty new to Golang and trying to make a simple REST api app work.</p>
  
  <p>Initially, everything was all fine since I had all code in the same directory under the <code>main</code> package.</p>
  
  <p>But, now I am at a stage where I need to start refactoring code into sub-directories and packages. Unfortunately, I have not been able to compile the app successfully.</p>
  
  <p>My <code>GOPATH</code> is set to: <code>~/.workspace</code>
  The current app is at: <code>~/.workspace/src/gitlab.com/myapp/api-auth</code></p>
  
  <p>This is how my current code organization is:</p>
  
  <p><a href="https://i.stack.imgur.com/atiDJ.png" rel="noreferrer"><img src="https://i.stack.imgur.com/atiDJ.png" alt="enter image description here"></a> </p>
  
  <p>Here is my <code>main.go</code></p>
  
  <pre><code>package main
  
  import (
      "net/http"
      "os"
      "strings"
  
      "github.com/gorilla/context"
      "github.com/justinas/alice"
      "gopkg.in/mgo.v2"
  
      "gitlab.com/myapp/api-auth/middlewares"
  )
  
  func main() {
      privateKey := []byte(strings.Replace(os.Getenv("JWT_KEY"), "\\n", "\n", -1))
  
      conn, err := mgo.Dial(os.Getenv("MONGO_CONN"))
  
      if err != nil {
          panic(err)
      }
  
      defer conn.Close()
      conn.SetMode(mgo.Monotonic, true)
  
      ctx := appContext{
          conn.DB(os.Getenv("MONGO_DB")),
          privateKey,
      }
  
      err = ctx.db.C("users").EnsureIndex(mgo.Index{
          Key:        []string{"username"},
          Unique:     true,
          Background: true,
          Sparse:     false,
      })
  
      if err != nil {
          panic(err)
      }
  
      commonHandlers := alice.New(LoggingHandler, context.ClearHandler, RecoveryHandler, AcceptHandler, ContentTypeHandler)
  
      router := NewRouter()
      router.Post("/users", commonHandlers.Append(BodyParserHandler(UserResource{})).ThenFunc(ctx.userCreationHandler))
      router.Post("/sessions", commonHandlers.Append(BodyParserHandler(UserResource{})).ThenFunc(ctx.sessionCreationHandler))
  
      http.ListenAndServe(":8080", router)
  }
  
  type appContext struct {
      db         *mgo.Database
      privateKey []byte
  }
  </code></pre>
  
  <p>Here is one of the middleware <code>accept.go</code> (Rest of the middleware are constructed similarly)</p>
  
  <pre><code>package middlewares
  
  import "net/http"
  
  // AcceptHandler ensures proper accept headers in requests
  func AcceptHandler(next http.Handler) http.Handler {
      fn := func(w http.ResponseWriter, r *http.Request) {
          if r.Header.Get("Accept") != "application/vnd.api+json" {
              writeError(w, errNotAcceptable)
              return
          }
  
          next.ServeHTTP(w, r)
      }
  
      return http.HandlerFunc(fn)
  }
  </code></pre>
  
  <p>This is the error I get when I run <code>go build</code> from root of my app.</p>
  
  <pre><code># gitlab.com/utiliti.es/api-auth
  ./main.go:11: imported and not used: "gitlab.com/myapp/api-auth/middlewares"
  ./main.go:42: undefined: LoggingHandler
  ./main.go:42: undefined: RecoveryHandler
  ./main.go:42: undefined: AcceptHandler
  ./main.go:42: undefined: ContentTypeHandler
  ./main.go:45: undefined: BodyParserHandler
  ./main.go:46: undefined: BodyParserHandler
  </code></pre>