Before I answer your question, you need to understand about the way golang manages it's packages and dependencies.
To download packages, use the go get
command. Usage example:
$ go get github.com/stretchr/testify
Above command will downloads the testify package then store it locally under work space with structure following it's url (TL;DR work space path is registered as $GOPATH
env variable).
$GOPATH/src/github.com/stretchr/testify
An explanation from the doc why the downloaded package name will follow it's url:
Get downloads the packages named by the import paths, along with their dependencies.
Now about your question:
Why does many golang project directly import from GitHub?
The statement import "github.com/stretchr/testify/assert"
doesn't mean that the package is directly imported from github website (through http). It's imported from your local, from github.com/stretchr/testify
path under $GOPATH/src
. The package was downloaded and stored there before, so it can be imported into any project.
Below is an explanation from the documentation about the import statements:
The Go path ($GOPATH
) is used to resolve import statements.
Also take a look at some code below (taken from my local).
$ echo $GOPATH
/Users/novalagung/Documents/go
$ go get -u github.com/stretchr/testify
# package will be downloaded into $GOPATH/src folder using the same structure as the github url
$ tree -L 1 $GOPATH/src/github.com/stretchr/testify
/Users/novalagung/Documents/go/src/github.com/stretchr/testify
├── Gopkg.lock
├── Gopkg.toml
├── LICENSE
├── README.md
├── _codegen
├── assert
├── doc.go
├── http
├── mock
├── package_test.go
├── require
├── suite
└── vendor
So if you want to import any packages, it must be under $GOPATH/src
folder.
what if this testify moved to bitbucket?
It has no effect on your local project, because the required package already downloaded before. However, if you want to update it, you might need to change the package path to follows it's new url, maybe like bitbucket.org/stretchr/testify/assert
? depends on what the new bitbucket url looks like.
But I don't think the owner of the testify will do that, since it'll break lot people of codes.