在Golang中,如何在没有GitHub的情况下组织本地导入?

huangapple go评论83阅读模式
英文:

Structuring local imports without GitHub in Golang

问题

我正在构建一个简单的应用程序,在阅读了关于组织Go应用程序结构的文档之后,我仍然感到困惑。

我想要这样的结构:

  • practice
    • models(作为models包)
      • a
      • b
    • routers(作为routers包)
      • a
      • b

app.go

app.go中,我有以下内容:

package main

import (
	"net/http"

    // 我尝试了以下导入方式:
    "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)
}

A和B模型的代码如下:

package models

import "net/http"

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

两个问题:

  1. 到底应该如何正确导入这些文件?我真的需要将它们推送到GitHub才能引用吗?我理解$GOPATH是本地机器上整个Go工作区的命名空间。我的$GOPATH已设置为包括此目录。

  2. 我需要在这些文件中定义一个main方法吗?我可以只导出一个函数并将其作为处理函数吗?

我已经查阅了文档

英文:

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

答案1

得分: 9

请问你需要将以下内容翻译成中文吗?

请参考如何编写Go代码

使用以下目录结构:

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

其中,使用命令go mod init practice创建go.mod文件,其中practice是模块路径。

按照以下方式导入包:

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

使用导入的包的示例:

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

即使在模块路径中使用了github.com,也不需要推送到github.com。

主包中的main函数是应用程序的入口点。请勿在除main之外的包中定义main函数。

英文:

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.

答案2

得分: 6

我认为另一个答案已经过时了,你不再需要使用GOPATH了。
运行:

go mod init yellow

然后创建一个名为yellow.go的文件:

package yellow

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

然后创建一个名为orange/orange.go的文件:

package main
import "yellow"

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

然后进行构建:

go build

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

英文:

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

huangapple
  • 本文由 发表于 2017年3月13日 09:07:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/42755174.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定