无法在任何地方找到包”github.com/gorilla/mux”。

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

cannot find package "github.com/gorilla/mux" in any of:

问题

我使用命令 go get github.com/gorilla/mux。我使用 Golang 创建了一个 HTTP 服务器,并运行了这个程序:

package main

import (
    "fmt"
    "html"
    "log"
    "net/http"

    "github.com/gorilla/mux"
)

func main() {

    router := mux.NewRouter().StrictSlash(true)
    router.HandleFunc("/", Index)
    log.Fatal(http.ListenAndServe(":8080", router))
}

func Index(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
}

但是我遇到了这个错误:

/usr/local/go/bin/go build -i [/Users/imac/go/src]
http.go:9:5: cannot find package "github.com/gorilla/mux" in any of:
    /usr/local/go/src/github.com/gorilla/mux (from $GOROOT)
    ($GOPATH not set)
Error: process exited with code 1.

我的 Go 环境如下:

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/imac/go"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/v9/fkc_t97s5v1g9sr938zzvxvh0000gn/T/go-build096571864=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"

我已经为这个错误奋斗了一个星期,但是找不到解决办法。请帮帮我。

英文:

I use command go get github.com/gorilla/mux. I made http server using Golang, and I run this program :

package main

import (
    "fmt"
    "html"
    "log"
    "net/http"

    "github.com/gorilla/mux"
)

func main() {

    router := mux.NewRouter().StrictSlash(true)
    router.HandleFunc("/", Index)
    log.Fatal(http.ListenAndServe(":8080", router))
}

func Index(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))

But I conflict this error :

/usr/local/go/bin/go build -i [/Users/imac/go/src]
http.go:9:5: cannot find package "github.com/gorilla/mux" in any of:
	/usr/local/go/src/github.com/gorilla/mux (from $GOROOT)
	($GOPATH not set)
Error: process exited with code 1.

My Go environment is here :

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/imac/go"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/v9/fkc_t97s5v1g9sr938zzvxvh0000gn/T/go-build096571864=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"

I fight with this error for a week, But I can't find out solution. Please help me.

答案1

得分: 19

你可以尝试以下步骤来进行调试:

  1. 运行命令 ls -l /usr/local/go/src/github.com | grep gorilla
  2. 运行命令 cd $GOPATH,然后运行命令 go list ... | grep gorilla
  3. 如果在上述两个命令中没有看到 gorilla,那么你需要安装它,可以运行命令 go get -v -u github.com/gorilla/mux

请运行命令 export PATH=$PATH:$GOPATH/bin

尝试运行 go run main.go,如果可以正常运行,那么你应该能够在项目路径下运行 go build

英文:

Could you try this steps to debug it:

  1. ls -l /usr/local/go/src/github.com | grep gorilla

  2. cd $GOPATH

    go list ... | grep gorilla

  3. if you din't see gorilla in the above two command, then you need to install it:
    go get -v -u github.com/gorilla/mux

Please run this: export PATH=$PATH:$GOPATH/bin

How about running go run main.go ? is that working, if yes you should be able to do go build from your project path.

答案2

得分: 6

我希望这对你有帮助。
你可以关闭 'mod'。

$ export GO111MODULE=off
英文:

I wish this helpful.
You may off the 'mod'.

$ export GO111MODULE=off

答案3

得分: 6

这对我来说没问题...

  1. 执行命令
    > go mod init <你的主文件所在的目录名>
  2. 执行命令 > go get github.com/gorilla/mux

执行完这两个命令后,你将能看到两个文件:

  1. go.mod
  2. go.sum

最后,关闭 VS Code 并重新打开,错误将会解决

英文:

This works for me...

  1. fire command
    > go mod init <your-directoryNane-where-main.go-exits>
  2. fire command > go get github.com/gorilla/mux

after firing this both commands you will be able see 2 files :

  1. go.mod
  2. go.sum

finally, Close VS code and open again, the error will be resolved

答案4

得分: 5

只需删除引号,如下所示:

go get github.com/gorilla/mux
英文:

Just remove quotes like this:

go get github.com/gorilla/mux

答案5

得分: 5

如果你正在使用 VS Code 作为你的集成开发环境,并且遇到了这个问题:

VS Code 默认使用 $HOME/go 作为你的 GOPATH - 如果你导出了另一个 GOPATH,你就会遇到这个问题。

解决方法:

  1. 使用 VS Code 内部终端并导航到你的项目文件夹:cd prjectFolder。输入 go env 并检查 GOPATH 的条目是否与你使用 cmd+t 然后 &gt;Go: Current GOPATH 得到的结果相同。
  2. 如果不一致,在你的用户设置中添加以下内容:
"go.gopath": "/some/path"

其中 /some/path 是你在 shell、zsh 等中导出的路径。

希望能对你有所帮助。

英文:

If you are using VS Code as your IDE and facing this problem:

VS Code uses $HOME/go as your default GOPATH - if you export another GOPATH you running into this trouble.

How to solve:

  1. Use the VS Code internal terminal and navigate to your project folder: cd prjectFolder. Type go env and check if the GOPATH entry is the same as you get when you use cmd+t and then &gt;Go: Current GOPATH
  2. If it doesn't fit, add in your user settings:
&quot;go.gopath&quot;: &quot;/some/path&quot;

where /some/path is the same path you export in you shell, zsh and so on.

Hope this helps.

答案6

得分: 2

我尝试移除github.com/gorillagithub.com/peterbourgon目录,然后重新运行make命令,它成功了。

以下是失败日志:

mac@user:~/TempPlace/temp/ngrok% make
go fmt ngrok/...
go get github.com/jteeuwen/go-bindata
GOOS="" GOARCH="" go install github.com/jteeuwen/go-bindata/go-bindata
bin/go-bindata -nomemcopy -pkg=assets -tags=debug \
	-debug=true \
	-o=src/ngrok/client/assets/assets_debug.go \
	assets/client/...
go get github.com/jteeuwen/go-bindata
GOOS="" GOARCH="" go install github.com/jteeuwen/go-bindata/go-bindata
bin/go-bindata -nomemcopy -pkg=assets -tags=debug \
	-debug=true \
	-o=src/ngrok/server/assets/assets_debug.go \
	assets/server/...
go get -tags 'debug' -d -v ngrok/...
src/ngrok/server/config.go:16:2: no Go files in /Users/apple/TempPlace/temp/ngrok/src/github.com/gorilla/mux
src/ngrok/server/config.go:17:2: no Go files in /Users/apple/TempPlace/temp/ngrok/src/github.com/peterbourgon/diskv
make: *** [deps] Error 1

以下是成功日志:

mac@user:~/TempPlace/temp/ngrok% make
go fmt ngrok/...
go get github.com/jteeuwen/go-bindata
GOOS="" GOARCH="" go install github.com/jteeuwen/go-bindata/go-bindata
bin/go-bindata -nomemcopy -pkg=assets -tags=debug \
	-debug=true \
	-o=src/ngrok/client/assets/assets_debug.go \
	assets/client/...
go get github.com/jteeuwen/go-bindata
GOOS="" GOARCH="" go install github.com/jteeuwen/go-bindata/go-bindata
bin/go-bindata -nomemcopy -pkg=assets -tags=debug \
	-debug=true \
	-o=src/ngrok/server/assets/assets_debug.go \
	assets/server/...
go get -tags 'debug' -d -v ngrok/...
github.com/gorilla/websocket (download)
github.com/gorilla/mux (download)
src/ngrok/server/config.go:17:2: no Go files in /Users/apple/TempPlace/temp/ngrok/src/github.com/peterbourgon/diskv
make: *** [deps] Error 1
mac@user:~/TempPlace/temp/ngrok% make
go fmt ngrok/...
go get github.com/jteeuwen/go-bindata
GOOS="" GOARCH="" go install github.com/jteeuwen/go-bindata/go-bindata
bin/go-bindata -nomemcopy -pkg=assets -tags=debug \
	-debug=true \
	-o=src/ngrok/client/assets/assets_debug.go \
	assets/client/...
go get github.com/jteeuwen/go-bindata
GOOS="" GOARCH="" go install github.com/jteeuwen/go-bindata/go-bindata
bin/go-bindata -nomemcopy -pkg=assets -tags=debug \
	-debug=true \
	-o=src/ngrok/server/assets/assets_debug.go \
	assets/server/...
go get -tags 'debug' -d -v ngrok/...
github.com/peterbourgon/diskv (download)
github.com/google/btree (download)
go install -tags 'debug' ngrok/main/ngrok
go install -ldflags "-s" -tags 'debug' ngrok/main/ngrokd

图片链接:点击这里

英文:

I try to remove github.com/gorilla and github.com/peterbourgon directory, then, retry: make, it works.

无法在任何地方找到包”github.com/gorilla/mux”。

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;pre&gt;
fail log:


mac@user:~/TempPlace/temp/ngrok% make
go fmt ngrok/...
go get github.com/jteeuwen/go-bindata
GOOS=&quot;&quot; GOARCH=&quot;&quot; go install github.com/jteeuwen/go-bindata/go-bindata
bin/go-bindata -nomemcopy -pkg=assets -tags=debug \
		-debug=true \
		-o=src/ngrok/client/assets/assets_debug.go \
		assets/client/...
go get github.com/jteeuwen/go-bindata
GOOS=&quot;&quot; GOARCH=&quot;&quot; go install github.com/jteeuwen/go-bindata/go-bindata
bin/go-bindata -nomemcopy -pkg=assets -tags=debug \
		-debug=true \
		-o=src/ngrok/server/assets/assets_debug.go \
		assets/server/...
go get -tags &#39;debug&#39; -d -v ngrok/...
src/ngrok/server/config.go:16:2: no Go files in /Users/apple/TempPlace/temp/ngrok/src/github.com/gorilla/mux
src/ngrok/server/config.go:17:2: no Go files in /Users/apple/TempPlace/temp/ngrok/src/github.com/peterbourgon/diskv
make: *** [deps] Error 1


success log:


mac@user:~/TempPlace/temp/ngrok% make
go fmt ngrok/...
go get github.com/jteeuwen/go-bindata
GOOS=&quot;&quot; GOARCH=&quot;&quot; go install github.com/jteeuwen/go-bindata/go-bindata
bin/go-bindata -nomemcopy -pkg=assets -tags=debug \
		-debug=true \
		-o=src/ngrok/client/assets/assets_debug.go \
		assets/client/...
go get github.com/jteeuwen/go-bindata
GOOS=&quot;&quot; GOARCH=&quot;&quot; go install github.com/jteeuwen/go-bindata/go-bindata
bin/go-bindata -nomemcopy -pkg=assets -tags=debug \
		-debug=true \
		-o=src/ngrok/server/assets/assets_debug.go \
		assets/server/...
go get -tags &#39;debug&#39; -d -v ngrok/...
github.com/gorilla/websocket (download)
github.com/gorilla/mux (download)
src/ngrok/server/config.go:17:2: no Go files in /Users/apple/TempPlace/temp/ngrok/src/github.com/peterbourgon/diskv
make: *** [deps] Error 1
mac@user:~/TempPlace/temp/ngrok% make
go fmt ngrok/...
go get github.com/jteeuwen/go-bindata
GOOS=&quot;&quot; GOARCH=&quot;&quot; go install github.com/jteeuwen/go-bindata/go-bindata
bin/go-bindata -nomemcopy -pkg=assets -tags=debug \
		-debug=true \
		-o=src/ngrok/client/assets/assets_debug.go \
		assets/client/...
go get github.com/jteeuwen/go-bindata
GOOS=&quot;&quot; GOARCH=&quot;&quot; go install github.com/jteeuwen/go-bindata/go-bindata
bin/go-bindata -nomemcopy -pkg=assets -tags=debug \
		-debug=true \
		-o=src/ngrok/server/assets/assets_debug.go \
		assets/server/...
go get -tags &#39;debug&#39; -d -v ngrok/...
github.com/peterbourgon/diskv (download)
github.com/google/btree (download)
go install -tags &#39;debug&#39; ngrok/main/ngrok
go install -ldflags &quot;-s&quot; -tags &#39;debug&#39; ngrok/main/ngrokd

&lt;/pre&gt;

<!-- end snippet -->

答案7

得分: 0

尝试执行以下命令:go build /Users/imac/go/src/project,因为我看到你试图在/Users/imac/go/src目录下使用go build命令。

英文:

Try go build /Users/imac/go/src/project

because I see you try to use go build under /Users/imac/go/src

答案8

得分: 0

也许这可以帮助其他在Windows上运行的用户。
在我的情况下,我需要创建两个符号链接:

 1. 以管理员身份运行cmd
 2. cd %gopath%
 3. mklink /D src pkg\mod
这将在src和pkg\mod之间创建一个符号链接
 4. cd src\github.com\gorilla

在这里,你会注意到mux包可能被列为mux@v1.8.0

 5. mklink /D mux mux@v1.8.0

有了这个,go将能够在%gopath%\src\github.com\gorilla\mux下找到github.com/gorilla/mux

最后,你需要将GO111Module设置为off
set GO111Module=off

现在你可以构建你的应用程序:

go build app.go
英文:

Maybe this can help other users running in Windows.
In my case I had to create two symlinks:

 1. run cmd as administrator  
 2. cd %gopath%  
 3. mklink /D src pkg\mod  
this creates a symlink between src and pkg\mod
 4. cd src\github.com\gorilla

Here you'll notice that the mux package might be listed as mux@v1.8.0

 5. mklink /D mux mux@v1.8.0

With this, go will be able to find github.com/gorilla/mux
under %gopath%\src\github.com\gorilla\mux

Finally, you have to set GO111Module to off
set GO111Module=off

and now you can build your app:

go build app.go

huangapple
  • 本文由 发表于 2017年1月9日 09:51:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/41539909.html
匿名

发表评论

匿名网友

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

确定