本文介绍了安装后,导入无法识别该软件包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,我是Golang的新手,安装后,我想在项目中使用下一个软件包:
安装软件包后
解决方案
使用模块设置新的Go项目的步骤:
- 已安装Go.最好是最新版本,最好是> = v1.13,其中Go模块是默认模块.对于go1.11及更高版本,您将不得不执行一些额外的步骤来启用Go模块.
- 为您的项目创建一个新文件夹.最好不在GOPATH中.默认情况下,GOPATH为〜/go,因此请创建您自己的项目文件夹,例如
mkdir〜/projects
,然后mkdir〜/projects/myproject
. - 所有其他命令均从新项目根目录运行,因此最好在此处进行切换:
cd〜/projects/myproject
- 在新创建的文件夹中,运行
go mod init projectPath
,其中projectPath
应该是您将来的git repo的URL(例如,github.com/myname/myproject
).这将在当前文件夹中创建go.mod
文件.它将包含您在go mod init
中使用的模块名称以及当前安装的go版本(作为最低版本).(暂时不用担心,它不会对您造成影响.)如果您不打算发布项目,则可以为您的项目命名.但是,如果这与另一个程序包或模块名称发生冲突,则您会遇到麻烦. - 现在您可以运行
go get github.com/gin-gonic/gin
(不要使用-u
,这很危险,因为它会更新所有子依赖项而不是使用gin开发人员使用的依赖项).这应根据需要将github.com/gin-gonic/gin
添加到您的go.mod
文件中.如果要更新依赖项,只需再次调用go get depPath
.它将把您的go.mod
文件中的依赖项版本更新为可用的最新版本.如果要升级/降级到特定版本,请使用go get depPath@vX.Y.Z
. - 创建您的
main.go
并在其中使用github.com/gin-gonic/gin
. - 使用
go mod tidy
删除所有未使用的导入或将丢失的导入添加到go.mod
.(通常,您无需编辑go.mod
,go mod tidy
将为您完成此操作.)它还将整理您的go.sum
文件,其中包含所有依赖项的校验和.您可以看一下文件,但是(通常)永远不必编辑它.go mod tidy
将为您做到这一点. - 在Goland中,最重要的是确保启用
Go模块集成
.默认情况下,其他设置应该正确. - 如果依赖项仍然存在问题,则可以尝试
go clean -modcache
.它将清除整个本地模块缓存,因此您需要重新下载所有本地缓存.如果模块缓存以某种方式弄乱了,这有时会有所帮助.不过,这不应该正常发生.
希望这会有所帮助.如果没有,请告诉我,以便我添加缺少的部分.
Hi I'm pretty new at Golang, after install it I would like to use the next package in my project:https://github.com/gin-gonic/gin
After I created my project, I did the next command to install gingonic:
go get -u github.com/gin-gonic/gin
But the import is not recognized inside my project, I understand that it's something related with my GOROOT, but I wasn't able to solve the issue.
The next are are my Go env variables:
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/rpantoja/Library/Caches/go-build"
GOENV="/Users/rpantoja/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/rpantoja/go/pkg/mod"
GONOPROXY="github.com/mercadolibre"
GONOSUMDB="github.com/mercadolibre"
GOOS="darwin"
GOPATH="/Users/rpantoja/go"
GOPRIVATE="github.com/mercadolibre"
GOPROXY="http://goregistry.furycloud.io/"
GOROOT="/usr/local/Cellar/go/1.15/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.15/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/gz/zfy97n595rs5w_t0dr9wr29dzzxvs4/T/go-build960054223=/tmp/go-build -gno-record-gcc-switches -fno-common"
And this is how my project it's configured:
After install the package:
解决方案
The steps to do to set up a new Go project with modules:
- Have Go installed. Latest version preferably, best >= v1.13 which Go modules the default. For go1.11 and above you will have to do some extra steps to enable Go modules.
- Create a new folder for your project. Preferably NOT in GOPATH. By default GOPATH is ~/go, so create your own projects folder, e.g.
mkdir ~/projects
and thenmkdir ~/projects/myproject
. - All further commands are run from the new projects root, so best switch there:
cd ~/projects/myproject
- In the newly created folder run
go mod init projectPath
whereprojectPath
should be the URL of your future git repo (e.g.github.com/myname/myproject
). This will create thego.mod
file in the current folder. It will contain the module name you used ingo mod init
and the currently installed go version as a minimum version. (Don't worry about that for now, it won't get in your way.) If you don't plan on ever releasing your project, you can name your project anything. But if that ever conflicts with another package or module name, you are in trouble. - Now you can run
go get github.com/gin-gonic/gin
(don't use-u
, that is dangerous as it update all sub-dependencies instead of using the dependencies the gin developers used). This should addgithub.com/gin-gonic/gin
to yourgo.mod
file as a requirement. If you want to update a dependency, just callgo get depPath
again. It will update the dependency version in yourgo.mod
file to the latest version available. If you want to up-/downgrade to a specific version usego get depPath@vX.Y.Z
. - Create your
main.go
and usegithub.com/gin-gonic/gin
in there. - Use
go mod tidy
to remove all unused imports or add missing ones togo.mod
. (Usually you don't need to editgo.mod
,go mod tidy
will do that for you.) It will also tidy up yourgo.sum
file which holds check sums for all your dependencies. You can have a look at the file, but will (usually) never have to edit it.go mod tidy
will do that for you. - In Goland the most important is to make sure
Go modules integration
is enabled. The other settings should be correct by default. - If you still have problems with the dependencies, you can try a
go clean -modcache
. It will clear your entire local modules cache, so you need to download all of it again. This can sometimes help if the modules cache got messed up somehow. Should not happen normally, though.
Hope this helps. If it doesn't, let me know so I can add the missing parts.
这篇关于安装后,导入无法识别该软件包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!