4

so I have the following code,

package main
import (
    "github.com/gorilla/mux"
)
func main() {
    router := mux.NewRouter()
}

when I do go run ., it gives the following error binapi.go:4:2: no required module provides package github.com/gorilla/mux; to add it: go get github.com/gorilla/mux, how do I fix this error? I have run the go get github.com/gorilla/mux command in every way possible and im sure it is installed. I found a post a while back but the command it gave didn't work. The command was go env -w GO111MODULE=auto but it didnt solve the issue.

here is the go env

GO111MODULE="auto"
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/ProfMonkey07/Library/Caches/go-build"
GOENV="/Users/ProfMonkey07/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/ProfMonkey07/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/ProfMonkey07/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.17.6"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/ProfMonkey07/binapi/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/8g/ssbpssj956ncjflh7w2k4w5m0000gn/T/go-build2892181555=/tmp/go-build -gno-record-gcc-switches -fno-common"
ProfMonkey07
  • 93
  • 1
  • 1
  • 7
  • ```go env``` show your go's env and paste it here – hao Jan 25 '22 at 02:32
  • I edited it to include the go env – ProfMonkey07 Jan 25 '22 at 04:17
  • I suggest ```GO111MODULE=off go run main.go``` , and if it does not work, you should use ```go mod init``` to create a go.mod file and run again with ```GO111MODULE=on go run main.go``` – hao Jan 25 '22 at 05:25
  • 1
    The error message reads "no required module provides package github.com/gorilla/mux; to add it: go get github.com/gorilla/mux", so just try what this message tells you: `go get github.com/gorilla/mux`. The idea behind error messages (and documentation) is that people should _read_ the message/documentation. – Volker Jan 25 '22 at 05:32
  • 2
    @Volker The poster had tried install the package already. And it seems that he imports the package in a stand-alone file. – hao Jan 25 '22 at 05:37
  • 1
    @Volker I understand the concept of reading documentation and error messages, but you need to read the post. I said I already ran the command multiple times in multiple ways from multiple locations and it still got the error – ProfMonkey07 Jan 25 '22 at 17:06

1 Answers1

6

Sounds like you do not have a go.mod file in the root of your project. Run go mod init <name> first.

Then when you run a go mod tidy it will add the "github.com/gorilla/mux"-dependency into the list of required modules, or manually add it with the stated go get github.com/gorilla/mux.
This behavior has changed in go1.16. Before it would automatically add the dependencies for you.

Make sure to commit both the go.mod and go.sum files should you use a VCS.

Ferdy Pruis
  • 1,073
  • 8
  • 9