无法导入自定义go模块
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"
这是我的主文件(server.go): p>
包main
import(
“ net / http”
“ routes”
)
func main(){
http.HandleFunc(“ /”,routes.Handler)
http.ListenAndServe (“:8000”,nil)
}
code> pre>
我的路由模块位于同一目录中: p>
包路由
func处理程序(w http.ResponseWriter,r * http.Request){
//东西...
}
code> pre>
何时 我运行 go run server.go code>我收到此错误: p>
server.go:6:5:在以下任何位置都找不到软件包“ routes” :
/usr/local/Cellar/go/1.6/libexec/src/routes(来自$ GOROOT)
〜/ server / src / routes(来自$ GOPATH)
code> pre>
当我将代码放入 routes.go code>到我的 server.go code>文件中时,它运行正常。 我无法导入模块。 我尝试将 $ GOPATH code>变量设置为当前目录,我尝试重新排列项目目录以模仿一个此处。 我没有其他选择了。 奇怪的是,一门语言被如此广泛地采用,而其关于如何执行几乎所有其他语言中相对容易的事情的文档却很少。 请帮我弄清楚我在做什么错。 p>
更新: h1>
这是 go env code>的输出 p>
GOARCH = “AMD64”
GOBIN = “”
GOEXE = “”
GOHOSTARCH = “AMD64”
GOHOSTOS = “达尔文”
GOOS = “达尔文”
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“
code> pre>
div>
Move the routes package to /Users/me/server/src/routes and you should be good to go.
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.