Automatically import 3rd party go packages

流过昼夜 提交于 2020-03-15 07:30:30

问题


I was wondering if there is a way to automatically download all the imports. So let's assume I need to use github.com/gorilla/mux and several other packages in my code base. Should I manually go to my ~/go/src and run go get repo or is there a smarter way of doing dependency management. I am using Goland IDE for my development.


回答1:


if there is a way to automatically download all the imports

You can download all imported pkgs and their dependencies by running go get from the command line.

I am using Goland IDE for my development

I'm using Goland too. When imports can't be found (ie the import path is highlighted in red), you can place your typing caret over it and press alt + enter and select go get ... from the popup window to automatically import.




回答2:


There are several approaces:

  • Simply go get github.com/gorilla/mux which will download sources in your $GOPATH and will be resolved automatically when compiling
  • Use dependency management (godep, glide[deprecated])
  • Use modules (experimental feature in Go 1.11 - Module. Check more here)

If you want a good and stable solution, use dep (.First you have to install it, then run:

cd $GOPATH/src/path/to/project
dep init
dep ensure -add github.com/gorilla/mux

You will see a new folder vendor in your project and 2 new dependency configuration files Gopkg.lock and Gopkg.toml. Read more about godep here.

Then run your main file as usual.




回答3:


You can use dep package manager which will go through your code and automatically import all the packages you use in you code. If you are working with >go1.11 I would suggest to use newly added go mod.



来源:https://stackoverflow.com/questions/52939473/automatically-import-3rd-party-go-packages

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!