这是我的主文件(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) {
// stuff...
}
运行时出现go run server.go
此错误:
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)
当我将代码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"
将路由包移至/Users/me/server/src/routes
,您应该就可以了。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句