我使用命令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"

我为这个错误解决了一个星期,但是我找不到解决方法。请帮我。

最佳答案

您可以尝试以下步骤对其进行调试:

  • ls -l /usr/local/go/src/github.com | grep gorilla
  • cd $GOPATHgo list ... | grep gorilla
  • 如果在以上两个命令中没有看到 gorilla ,则需要安装它:go get -v -u github.com/gorilla/mux

  • 请运行以下命令:export PATH=$PATH:$GOPATH/bin
    运行go run main.go怎么样?是可行的,如果是的话,您应该能够从项目路径执行go build

    关于go - 在:的任何一个中找不到软件包 “github.com/gorilla/mux”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41539909/

    10-10 10:09