Search code examples
gogo-packages

The package could not be imported after you installed it using Go Get ?


I turned on GO111MODULE=on, and when using the Go Get -u installation package, it will be installed to the PGK directory, but I can't use it after installation

Go version go1.15.6 Windows/amd64

go.mod file

module github.com/xanzy/go-gitlab

go 1.15

Go env

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

go build gitlab_merge_requests.go I got the error

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

This is my code

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"))

}
}

I just learn Golang, who can help me?


Solution

  • Your go.mod file says that your module is named github.com/xanzy/go-gitlab. When you try to import that path, Go thinks that your package is trying to import itself, which is impossible, and raises an error.

    Delete your go.mod and re-run go mod init with your own module path (if you have a repo, use that, if you never intend to share it with anyone ever, you can use anything you like). Then you will be able to go get correctly.