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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀