简体   繁体   中英

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

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.

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.

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 >Go: Current GOPATH
  2. If it doesn't fit, add in your user settings: "go.gopath": "/some/path" where /some/path is the same path you export in you shell, zsh and so on.

Hope this helps.

只需删除这样的引号:

go get github.com/gorilla/mux

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

在此处输入图片说明

 <pre> fail log: 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 success log: 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 </pre>

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

$ export GO111MODULE=off

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

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

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

  1. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM