英文:
Cannot import custom go module
问题
这是我的主文件(server.go):
package main
import (
"net/http"
"routes"
)
func main() {
http.HandleFunc("/", routes.Handler)
http.ListenAndServe(":8000", nil)
}
我的路由模块在同一个目录下:
package routes
func Handler(w http.ResponseWriter, r *http.Request) {
// 处理逻辑...
}
当我运行 go run server.go
时,出现以下错误:
server.go:6:5: 找不到包 "routes" 在以下任何目录中:
/usr/local/Cellar/go/1.6/libexec/src/routes (来自 $GOROOT)
~/server/src/routes (来自 $GOPATH)
当我将 routes.go
中的代码放入 server.go
文件中时,它可以正常运行。我无法导入该模块。我尝试将 $GOPATH
变量设置为当前目录,尝试重新排列项目目录以模仿这里的目录结构。我已经没有更多的选择了。奇怪的是,这门语言在广泛应用的同时,对于如何做一些在其他语言中相对容易的事情的文档却非常贫乏。请帮助我找出我做错了什么。
更新:
这是 go env
的输出:
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/me/server"
GORACE=""
GOROOT="/usr/local/Cellar/go/1.6/libexec"
GOTOOLDIR="/usr/local/Cellar/go/1.6/libexec/pkg/tool/darwin_amd64"
GO15VENDOREXPERIMENT="1"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fno-common"
CXX="clang++"
CGO_ENABLED="1"
英文:
Here is my main file (server.go):
package main
import (
"net/http"
"routes"
)
func main() {
http.HandleFunc("/", routes.Handler)
http.ListenAndServe(":8000", nil)
}
My routes module is in the same directory:
package routes
func Handler(w http.ResponseWriter, r *http.Request) {
// stuff...
}
When I run go run server.go
I get this error:
server.go:6:5: cannot find package "routes" in any of:
/usr/local/Cellar/go/1.6/libexec/src/routes (from $GOROOT)
~/server/src/routes (from $GOPATH)
When I place the code in routes.go
into my server.go
file, it runs fine. I cannot import the module. I have tried setting the $GOPATH
variable to my current directory, I've tried rearranging my project directory to mimic the one here. I'm running out of options. It is strange that a language with such wide adoption has such poor documentation on how to do something that is relatively easy in almost every other language. Please help me figure out what I'm doing wrong.
UPDATE:
This is the output of go env
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/me/server"
GORACE=""
GOROOT="/usr/local/Cellar/go/1.6/libexec"
GOTOOLDIR="/usr/local/Cellar/go/1.6/libexec/pkg/tool/darwin_amd64"
GO15VENDOREXPERIMENT="1"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fno-common"
CXX="clang++"
CGO_ENABLED="1"
答案1
得分: 4
《如何编写Go代码》是入门指南页面上的推荐起点,该页面解释了这一点。(感谢@elithrar)
Peter Bourgon在良好结构化的Go应用程序方面有一篇很好的文章。
你应该将routes
包放在routes
文件夹中,或者如果你的主包在routes
文件夹中,你可以在该文件夹中再创建一个lib
文件夹,其中包含你的实际routes
包。
文件夹结构的原因是由于Go的import
语句的工作方式。考虑到导入的方式,如果在同一个文件夹中启用多个包,会产生歧义。
英文:
The "How To Write Go Code" article is the recommended starting point right on the Getting Started page which explains this. (thanks @elithrar)
Peter Bourgon has a good write up on well structured Go applications.
You should either have the routes
package in a routes
folder, or if your main package is in the routes folder you can have a lib
folder within that folder with your actual routes
package.
The reason for the folder structure is due to how go import
statements work. It would be ambiguous to enable multiple packages in the same folder given the way imports work.
答案2
得分: 3
将路由包移动到/Users/me/server/src/routes
,然后你就可以开始了。
英文:
Move the routes package to /Users/me/server/src/routes
and you should be good to go.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论