我使用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"
型
我与这个错误斗争了一个星期,但我找不到解决办法。请帮助我。
8条答案
按热度按时间piah890a1#
你可以尝试以下步骤来调试它:
1.第一个月
cd $GOPATH
个go list ... | grep gorilla
个1.如果你在上面的两个命令中没有看到gorilla,那么你需要安装它:
go get -v -u github.com/gorilla/mux
请运行:
export PATH=$PATH:$GOPATH/bin
运行
go run main.go
怎么样?这是否有效,如果是,您应该能够从项目路径执行go build
。0dxa2lsx2#
这对我来说很有效...
1.*fire命令 >
go mod init <your-directoryNane-where-main.go-exist>
1.fire命令>
go get github.com/gorilla/mux
go.mod
个go.sum
个wvmv3b1j3#
我希望这对你有帮助。你可以关闭“国防部”。
字符串
23c0lvtd4#
就像这样删除引号:
字符串
xj3cbfub5#
如果您使用VS Code作为IDE并面临此问题:
VS Code使用
$HOME/go
作为默认的GOPATH
-如果你导出另一个GOPATH
,你就会遇到这个麻烦。如何解决:
1.使用VS Code内部终端并导航到项目文件夹:
cd prjectFolder
。键入go env
并检查GOPATH
条目是否与使用cmd+t
然后>Go: Current GOPATH
时获得的条目相同1.如果它不适合,在你的用户设置中添加:
"go.gopath": "/some/path"
其中/some/path
是你在shell、zsh等中导出的相同路径。希望这能帮上忙。
cld4siwp6#
我尝试删除
github.com/gorilla
和github.com/peterbourgon
目录,然后,重试:make
,它的作品.的数据
字符串
vvppvyoh7#
试试
go build /Users/imac/go/src/project
因为我看到您尝试在
/Users/imac/go/src
下使用go build
,vsnjm48y8#
也许这可以帮助其他用户在Windows中运行。在我的情况下,我必须创建两个符号链接:
字符串
在这里,您会注意到mux包可能被列为email protected(https://stackoverflow.com/cdn-cgi/l/email-protection)
型
这样,go就可以在%gopath%\src\github.com\gorilla\mux下找到github.com/gorilla/mux
最后,您必须设置GO111Module为off设置GO111Module=off
现在你可以构建你的app了:
型