【问题标题】:The package could not be imported after you installed it using Go Get ?使用 Go Get 安装后无法导入包?
【发布时间】:2021-03-26 11:25:32
【问题描述】:

我开启了GO111MODULE=on,使用Go Get -u安装包时会安装到PGK目录下,但是安装后无法使用

Go 版本 go1.15.6 Windows/amd64

go.mod 文件

module github.com/xanzy/go-gitlab

go 1.15

去环境

E:\code\awesomeProject2\src>go env
set GO111MODULE=on
set GOARCH=amd64
set GOBIN=D:\Go\bin
set GOCACHE=C:\Users\code\AppData\Local\go-build
set GOENV=C:\Users\code\AppData\Roaming\go\env
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=E:\code\awesomeProject2\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=E:\code\awesomeProject2
set GOPRIVATE=
set GOPROXY=https://mirrors.aliyun.com/goproxy/,direct
set GOROOT=D:\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=D:\Go\pkg\tool\windows_amd64
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=E:\code\awesomeProject2\src\go.mod
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\guji\AppData\Loc
al\Temp\go-build895842770=/tmp/go-build -gno-record-gcc-switches

去构建 gitlab_merge_requests.go 我得到了错误

package github.com/xanzy/go-gitlab
imports github.com/xanzy/go-gitlab: import cycle not allowed

这是我的代码

package main

import (
"log"
"net/http"
"net/http/httptest"
"github.com/xanzy/go-gitlab"
)



var (
Token = "XXXXXX" // token information
url = "https://xxxxx" // gitlab URL
)

func setup() (*http.ServeMux, *httptest.Server, *gitlab.Client) {

// mux is the HTTP request multiplexer used with the test server.

mux := http.NewServeMux()



// server is a test HTTP server used to provide mock API responses.

server := httptest.NewServer(mux)



// client is the Gitlab client being tested.

client := gitlab.NewClient(nil, token)
client.SetBaseURL(url)


return mux, server, client

}



func main(){
_, _, client := setup()


users, _, err := client.Users.ListUsers(&gitlab.ListUsersOptions{})
if err ! = nil {

panic(err)

}

for _, user := range users {
log.Println(user.Username, user.Name, user.CreatedAt.Format("2006-01-02"))

}
}

我刚学Golang,谁能帮帮我?

【问题讨论】:

  • 不要混淆“模块”和“包”。一个包包含代码并且可以被导入。模块是一组一起版本化的包。无法导入模块。一个模块不再是多个包的单个版本标签。
  • 为什么要导入自己的代码?主包已经是模块的一部分,所以导入它是一个依赖循环(作为错误消息详细信息)。

标签: go go-packages


【解决方案1】:

您的 go.mod 文件显示 您的模块 名为 github.com/xanzy/go-gitlab。当您尝试导入该路径时,Go 认为您的包正在尝试导入 自身,这是不可能的,并引发错误。

删除你的 go.mod 并使用你自己的模块路径重新运行go mod init(如果你有一个 repo,使用它,如果你从不打算与任何人共享它,你可以使用任何你喜欢的东西)。然后你就可以正确go get了。

【讨论】:

    【解决方案2】:

    import cycle not allowed 告诉我你有导入的循环引用。 A 导入 B,B 导入 A,甚至 A 导入 A。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-24
      • 1970-01-01
      • 2021-03-15
      • 2021-05-20
      • 2014-11-16
      • 1970-01-01
      • 2012-11-27
      • 2020-10-30
      相关资源
      最近更新 更多