没有必需的模块提供mux包错误。

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

go no required module provides package mux error

问题

所以我有以下的代码,

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

当我运行go run .时,它会给出以下错误信息:
binapi.go:4:2: no required module provides package github.com/gorilla/mux; to add it: go get github.com/gorilla/mux
我该如何解决这个错误?我已经以各种方式运行了go get github.com/gorilla/mux命令,并确保它已安装。我以前找到过一篇帖子,但它给出的命令没有起作用。命令是go env -w GO111MODULE=auto,但它没有解决问题。

这是我的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"
英文:

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"

答案1

得分: 6

看起来你的项目根目录下没有go.mod文件。首先运行go mod init <name>命令。

然后当你运行go mod tidy命令时,它会将"github.com/gorilla/mux"依赖项添加到所需模块列表中,或者你可以使用指定的命令go get github.com/gorilla/mux手动添加它。
这种行为在go1.16中已经发生了变化。在此之前,它会自动为你添加依赖项。

如果你使用版本控制系统,请确保提交go.modgo.sum文件。

英文:

Sounds like you do not have a go.mod file in the root of your project. Run go mod init &lt;name&gt; 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.

huangapple
  • 本文由 发表于 2022年1月25日 10:08:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/70842657.html
匿名

发表评论

匿名网友

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

确定