Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Golang, importing packages from Github requests me to remember the Github URL?

Tags:

github

package

go

I'm very new to Golang. I see that in Golang you can import packages directly from Github like:

import "github.com/MakeNowJust/heredoc" 

Does that mean I have to remember this URL in order to use this package? IMHO this is not cool. What if later the author of the package removed it or changed the URL? Any ideas?

like image 287
user130268 Avatar asked Aug 02 '16 09:08

user130268


People also ask

How do I import packages to Go?

To import a package, we use import syntax followed by the package name. 💡 Unlike other programming languages, a package name can also be a subpath like some-dir/greet and Go will automatically resolve the path to the greet package for us as you will see in the nested package topic ahead.

What is _ in import Golang?

Short answer: It's for importing a package solely for its side-effects. From the Go Specification: To import a package solely for its side-effects (initialization), use the blank identifier as explicit package name: import _ "lib/math"

How do I import multiple packages in Go?

2. Grouped import. Go also supports grouped imports. This means that you don't have to write the import keyword multiple times; instead, you can use the keyword import followed by round braces, (), and mention all the packages inside the round braces that you wish to import.


2 Answers

I would recommend you to read the How to Write Go Code documentation and this blog post.

The path you're seeing in your import line is not a url, but only the path the package is located in (normally relative to $GOROOT/src/pkg or $GOPATH/src). So your package heredoc is most probably located in the directory $GOPATH/src/github.com/MakeNowJust/heredoc.

The recommended way to use external packages is by downloading and installing them via go get. You might want to check out the documentation of go get by go get --help.

like image 186
tsabsch Avatar answered Sep 27 '22 22:09

tsabsch


The path that import statement refers is just appended to $GOPATH/src. So that import statement basically says "import the package located at $GOPATH/src/github.com/MakeNowJust/heredoc"

What if later the author of the package removed it or changed the URL?

As long as you already have the source files for that package at the expected location, it should be included even if the repo has moved.

like image 22
blee Avatar answered Sep 27 '22 22:09

blee