英文:
Structuring local imports without GitHub in Golang
问题
我正在构建一个简单的应用程序,在阅读了关于组织Go应用程序结构的文档之后,我仍然感到困惑。
我想要这样的结构:
- practice
- models(作为models包)
- a
- b
- routers(作为routers包)
- a
- b
- models(作为models包)
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) {
// 代码
}
两个问题:
-
到底应该如何正确导入这些文件?我真的需要将它们推送到GitHub才能引用吗?我理解
$GOPATH
是本地机器上整个Go工作区的命名空间。我的$GOPATH
已设置为包括此目录。 -
我需要在这些文件中定义一个
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
- models (packaged as models)
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:
-
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.
-
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
- practice
- src
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论