doutui7955 2018-10-19 01:56
浏览 2106
已采纳

为什么许多golang项目直接从GitHub导入?

I'm totally new to golang, and am wondering, why many golang projects write in their source code like this way:

import  "github.com/stretchr/testify/assert"

What if this testify moved to bitbucket?

Why not download testify and import testify, like other languages?

  • 写回答

3条回答 默认 最新

  • duanping3587 2018-10-19 02:07
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制