Structuring local imports without github in golang

See How to Write Go Code.

Create your directory structure under $GOPATH/src.

  • $GOPATH
    • src
      • practice
        • models
        • routers

Import the packages as follows:

import (
  "practice/routers"
  "practice/models"
  ...
)

Use the imported packages like this:

func main() {
  http.HandleFunc("/a", models.AHandler)
  http.HandleFunc("/b", models.BHandler)
  http.ListenAndServe(":8080", nil)
}

You do not need to push to github.com, even if you use 'github.com' in the file path.

The main function in the main package is the entry point for the application. Do not define main functions in packages other than main.

Tags:

Go