Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Structuring local imports without GitHub in Golang

Tags:

go

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

I want this structure:

  • practice
    • models (packaged as models)
      • a
      • b
    • routers (packaged as routers)
      • a
      • b

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

like image 834
user3162553 Avatar asked Mar 13 '17 01:03

user3162553


People also ask

Which one is the correct way to import multiple packages libraries in Golang?

Both single and multiple packages can be imported one by one using the import keyword.

Is Gopath still needed?

They are not necessary and mixing GOPATH configurations with Go Modules is a way of causing confusion. More specifically: The default value for the GOPATH (when not set) is ~/go , so when I mention the directory ~/go I mean the default directory used by Go.


1 Answers

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.

like image 112
Bayta Darell Avatar answered Nov 08 '22 09:11

Bayta Darell