Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically import 3rd party go packages

Tags:

go

goland

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.

like image 744
Achilleus Avatar asked Oct 21 '25 14:10

Achilleus


2 Answers

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.

like image 184
jonathangersam Avatar answered Oct 24 '25 07:10

jonathangersam


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.

like image 44
Maxian Nicu Avatar answered Oct 24 '25 06:10

Maxian Nicu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!