25

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.

Diana
  • 405
  • 1
  • 5
  • 13

8 Answers8

19

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.

James Sapam
  • 16,036
  • 12
  • 50
  • 73
6

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

$ export GO111MODULE=off
August Gong
  • 371
  • 4
  • 5
  • 2
    Great, this works for me. The reason it wasn't working for me is that gorilla was being downloaded in the mod folder and not the src folder as expected. – Rishabh Apr 23 '21 at 15:50
  • After struggling for 2-3 hrs, this one worked for me.By default pkgs are installed in mod instead of src. – dynamo Nov 01 '21 at 12:27
6

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

5

Just remove quotes like this:

go get github.com/gorilla/mux
St. John Johnson
  • 6,590
  • 7
  • 35
  • 56
No_20
  • 101
  • 3
  • Can you find `github.com/gorilla/mux` in $GOPATH/pkg subdirectory. If not. Try `go install github.com/gorilla/mux`. – No_20 Jan 09 '17 at 03:37
  • I try this but not working... `github.com/gorilla/mux` is in `$GOPATH/pkg/darwin_amd64`. – Diana Jan 09 '17 at 03:55
5

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.

David Joos
  • 926
  • 11
  • 16
2

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

enter image description here

<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>
Mark Simon
  • 362
  • 4
  • 11
0

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

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

M-AbdalRahman
  • 251
  • 3
  • 11
0

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
hkutluay
  • 6,794
  • 2
  • 33
  • 53