user3162553
user3162553

Reputation: 2859

Structuring local imports without GitHub in Golang

I'm building a simple app and after reading the doc on structuring go applications, I'm still confused.

I want this structure:

app.go

Inside of app.go, I have the following:

package main

import (
    "net/http"

    // I have tried the following:
    "practice/models/a"
    "practice/models/b"
    "practice/models"
    "$GOPATH/practice/models/a"
    "$GOPATH/practice/models/b"
    "$GOPATH/practice/models"
    ...
)

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

The A and B models look like this:

package models

import "net/http"

func AHandler(w http.ResponseWriter, r *http.Request) {
   // code
}

Two questions:

  1. What in the world is the right way to import these files? Do I really have to push them to github in order to be able to reference them? I understand the $GOPATH is the namespace for the entire go workspace on a local machine. My $GOPATH is set to include this directory.

  2. Do I need to define a main method inside of these files? Can I just export a single function and have that be the handling function?

I have consulted the docs

Upvotes: 7

Views: 11242

Answers (2)

Zombo
Zombo

Reputation: 1

I think the other answer is out of date, you don't need to use GOPATH anymore. Run:

go mod init yellow

Then create a file yellow.go:

package yellow

func Mix(s string) string {
   return s + "Yellow"
}

Then create a file orange/orange.go:

package main
import "yellow"

func main() {
   s := yellow.Mix("Red")
   println(s)
}

Then build:

go build

https://golang.org/doc/code.html

Upvotes: 7

Thundercat
Thundercat

Reputation: 120951

See How to Write Go Code.

Use this directory structure:

- practice
    - go.mod
    - app.go
    - models 
       - a.go
       - b.go
    - routers 
       - a.go
       - b.go

where go.mod is created with the command go mod init practice where practice is the module path.

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 module 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.


What follows is the original answer based on GOPATH workspaces:

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.

Upvotes: 12

Related Questions