Home >Backend Development >Golang >What is the use of go get command?

What is the use of go get command?

青灯夜游
青灯夜游Original
2023-01-29 13:49:014632browse

The "go get" command can remotely pull or update code packages and their dependent packages with the help of code management tools, and automatically complete compilation and installation. The "go get" command can dynamically obtain the remote code package. Before using the "go get" command, you need to install a code management tool that matches the remote package, such as Git, SVN, HG, etc., and a package name needs to be provided in the parameter.

What is the use of go get command?

The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.

The go get command can remotely pull or update code packages and their dependent packages with the help of code management tools, and automatically complete compilation and installation. The whole process is as easy as installing an app.

This command can dynamically obtain remote code packages. Currently supported are BitBucket, GitHub, Google Code and Launchpad. Before using the go get command, you need to install a code management tool that matches the remote package, such as Git, SVN, HG, etc., and provide a package name in the parameter.

This command is actually divided into two steps internally: the first step is to download the source code package, and the second step is to execute go install.

go get command - get the code, compile and install with one click

hc@ubt:~$ go get github.com/hyper-carrot/go_lib/logging

Command go getcan be requested upon request And the actual situation is to download or update the specified code package and its dependent packages from the Internet, compile and install them. In the above example, we downloaded a project (or code package) from the famous code hosting site Github and installed it into the first workspace included in the environment variable GOPATH. At the same time, we also know that the import path of this code package is github.com/hyper-carrot/go_lib/logging.

Generally, in order to separate our own and third-party code, we will set up two or more workspaces. We now have a workspace with the directory path /home/hc/golang/lib, and it is the first directory path in the environment variable GOPATH value. Note that the path contained in the environment variable GOPATH cannot be the same as the value of the environment variable GOROOT. Well, if we use the go get command to download and install code packages, then these code packages will be installed in the workspace above. Let's temporarily call this workspace the Lib workspace. After we run go get github.com/hyper-carrot/go_lib/logging, the code package should be saved in the src directory of the Lib work and has been properly installed, as shown below:

/home/hc/golang/lib:
    bin/
    pkg/
        linux_386/
            github.com/
                hyper-carrot/
                    go_lib/
                        logging.a  
    src/
        github.com/
            hyper-carrot/
                go_lib/
                    logging/
    ...

On the other hand, if we want to upload a project to the Github website (or other code hosting website) and be used by others, then we should treat the project as a code package. In fact, we have mentioned the reason before. The go get command will store all subdirectories and source code files under the project into the src directory of the first workspace, and all subdirectories under the src directory They will all be part or all of the import path of a certain code package. In other words, we should store subcode packages and source code files directly in the project directory, and the package name declared by the source code file directly stored in the project directory should be the same as the project name (unless it is a command source code file). Doing this will allow others to use your project directly after downloading it from the Github site using the go get command.

In fact, it is not recommended to directly use the path of the project root directory as the workspace path like the goc2p project. The main reason for doing this is to make it easier for readers to understand the project structure and workspace concepts of the Go language, and also to allow readers to see another project structure. Of course, if your project uses tools like gb, that is another matter. The root directory of such a project should be considered a workspace (but you do not need to add it to the GOPATH environment variable). It should be downloaded somewhere outside the Go language workspace by git clone instead of using the go get command.

Remote import path analysis

In fact, the action performed by the go get command is also called code package remote import, and is passed to the The parameter of the command that is the code package import path is also called the code package remote import path. The

go get command can not only download code packages from famous code hosting sites like Github, but also from any code version control system (English Version Control System, referred to as Check out the code package for VCS). Any code hosting site provides code upload and download services through one or more code version control systems. So, more strictly speaking, what the go get command does is check out/update the code package from the remote repository of the code version control system and compile and install it.

The VCS information supported by this command is as follows:

Table 0-2 go getVCS supported by the command

##MercurialhgMercurial is a lightweight distributed version control system, implemented in Python language, easy to learn and use, and highly scalable. GitgitGit was originally an open source distributed version control software developed by Linux Torvalds to help manage Linux kernel development. But it is now widely used. It is used for effective, high-speed version management of projects of all sizes. SubversionsvnSubversion is a version control system and the first system to incorporate the concept and functionality of branches into the version control model. But compared to Git and Mercurial, it is only a member of the traditional version control system. BazaarbzrBazaar is an open source distributed version control system. But in comparison, there are not many projects that use it as a VCS.
Name Main command Description

The go get command must know the version control system and the URL of the remote warehouse corresponding to the remote import path of the code package before checking out the code package.

If the code package already exists in the local workspace, these pieces of information will be determined directly by analyzing its path. Several version control systems supported by the

go get command have one thing in common, that is, a metadata directory will be stored in the checked-out project directory, with a name prefixed with "." plus its main command name. For example, Git will add a subdirectory named ".git" to the checked-out project directory. Therefore, it is easy to determine the version control system used by the code package. In addition, since the code package already exists, we only need to update the code package through the update command of the code version control system, so there is no need to know the URL of its remote warehouse. For code packages that already exist in the local workspace, the go get command will not perform repeated downloads unless the code package is forcibly updated. If you want to forcibly update the code package, you can add the -u tag when executing the go get command. This markup will be introduced later.

If the code package does not exist in the local workspace, the relevant information can only be obtained by analyzing the remote import path of the code package. First, the

go get command will perform static analysis on the remote import path of the code package. In order to make the analysis process more convenient and faster, the go get command program has preset the information of several famous code hosting websites. As shown in the following table:

Table 0-3 Information about the preset code hosting site

NameMaster Domain nameSupported VCSCode package remote import path exampleBitbucketbitbucket.org Git, Mercurialbitbucket.org/user/projectGitHubgithub.comGitgithub.com/user/projectGoogle Code Project Hostingcode.google.comGit, Mercurial, Subversioncode.google.com/p/projectLaunchpadlaunchpad.netBazaarlaunchpad.net/projectIBM DevOps Serviceshub.jazz.netGithub.jazz.net/git/user/project

一般情况下,代码包远程导入路径中的第一个元素就是代码托管网站的主域名。在静态分析的时候,go get命令会将代码包远程导入路径与预置的代码托管站点的主域名进行匹配。如果匹配成功,则在对代码包远程导入路径的初步检查后返回正常的返回值或错误信息。如果匹配不成功,则会再对代码包远程导入路径进行动态分析。至于动态分析的过程,我就不在这里详细展开了。

如果对代码包远程导入路径的静态分析或/和动态分析成功并获取到对应的版本控制系统和远程仓库URL,那么go get命令就会进行代码包检出或更新的操作。随后,go get命令会在必要时以同样的方式检出或更新这个代码包的所有依赖包。

自定义代码包远程导入路径

如果你想把你编写的(被托管在不同的代码托管网站上的)代码包的远程导入路径统一起来,或者不希望让你的代码包中夹杂某个代码托管网站的域名,那么你可以选择自定义你的代码包远程导入路径。这种自定义的实现手段叫做“导入注释”。导入注释的写法示例如下:

package analyzer // import "hypermind.cn/talon/analyzer"

代码包analyzer实际上属于我的一个网络爬虫项目。这个项目的代码被托管在了Github网站上。它的网址是:https://github.com/hyper-carrot/talon。如果用标准的导入路径来下载analyzer代码包的话,命令应该这样写go get github.com/hyper-carrot/talon/analyzer。不过,如果我们像上面的示例那样在该代码包中的一个源码文件中加入导入注释的话,这样下载它就行不通了。我们来看一看这个导入注释。

导入注释的写法如同一条代码包导入语句。不同的是,它出现在了单行注释符//的右边,因此Go语言编译器会忽略掉它。另外,它必须出现在源码文件的第一行语句(也就是代码包声明语句)的右边。只有符合上述这两个位置条件的导入注释才是有效的。再来看其中的引号部分。被双引号包裹的应该是一个符合导入路径语法规则的字符串。其中,hypermind.cn是我自己的一个域名。实际上,这也是用来替换掉我想隐去的代码托管网站域名及部分路径(这里是github.com/hyper-carrot)的那部分。在hypermind.cn右边的依次是我的项目的名称以及要下载的那个代码包的相对路径。这些与其标准导入路径中的内容都是一致的。为了清晰起见,我们再来做下对比。

github.com/hyper-carrot/talon/analyzer // 标准的导入路径
hypermind.cn           /talon/analyzer // 导入注释中的导入路径

你想用你自己的域名替换掉标准导入路径中的哪部分由你自己说了算。不过一般情况下,被替换的部分包括代码托管网站的域名以及你在那里的用户ID就可以了。这足以达到我们最开始说的那两个目的。

虽然我们在talon项目中的所有代码包中都加入了类似的导入注释,但是我们依然无法通过go get hypermind.cn/talon/analyzer命令来下载这个代码包。因为域名hypermind.cn所指向的网站并没有加入相应的处理逻辑。具体的实现步骤应该是这样的:

  • 编写一个可处理HTTP请求的程序。这里无所谓用什么编程语言去实现。当然,我推荐你用Go语言去做。

  • 将这个处理程序与hypermind.cn/talon这个路径关联在一起,并总是在作为响应的HTML文档的头中写入下面这行内容:

    <meta name="go-import" content="hypermind.cn/talon git https://github.com/hyper-carrot/talon">

    hypermind.cn/talon/analyzer熟悉HTML的读者都应该知道,这行内容会被视为HTML文档的元数据。它实际上go get命令的文档中要求的写法。它的模式是这样的:

<meta name="go-import" content="import-prefix vcs repo-root">

实际上,content属性中的import-prefix的位置上应该填入我们自定义的远程代码包导入路径的前缀。这个前缀应该与我们的处理程序关联的那个路径相一致。而vcs显然应该代表与版本控制系统有关的标识。还记得表0-2中的主命令列吗?这里的填入内容就应该该列中的某一项。在这里,由于talon项目使用的是Git,所以这里应该填入git。至于repo-root,它应该是与该处理程序关联的路径对应的Github网站的URL。在这里,这个路径是hypermind.cn/talon,那么这个URL就应该是https://github.com/hyper-carrot/talon。后者也是talon项目的实际网址。

好了,在我们做好上述处理程序之后,go get hypermind.cn/talon/analyzer命令的执行结果就会是正确的。analyzer代码包及其依赖包中的代码会被下载到GOPATH环境变量中的第一个工作区目录的src子目录中,然后被编译并安装。

注意,具体的代码包源码存放路径会是/home/hc/golang/lib/src/hypermind.cn/talon/analyzer。也就是说,存放路径(包括代码包源码文件以及相应的归档文件的存放路径)会遵循导入注释中的路径(这里是hypermind.cn/talon/analyzer),而不是原始的导入路径(这里是github.com/hyper-carrot/talon/analyzer)。另外,我们只需在talon项目的每个代码包中的某一个源码文件中加入导入注释,但这些导入注释中的路径都必须是一致的。在这之后,我们就只能使用hypermind.cn/talon/作为talon项目中的代码包的导入路径前缀了。一个反例如下:

hc@ubt:~$ go get github.com/hyper-carrot/talon/analyzer
package github.com/hyper-carrot/talon/analyzer: code in directory /home/hc/golang/lib/src/github.com/hyper-carrot/talon/analyzer expects import "hypermind.cn/talon/analyzer"

与自定义的代码包远程导入路径有关的内容我们就介绍到这里。从中我们也可以看出,Go语言为了让使用者的项目与代码托管网站隔离所作出的努力。只要你有自己的网站和一个不错的域名,这就很容易搞定并且非常值得。这会在你的代码包的使用者面前强化你的品牌,而不是某个代码托管网站的。当然,使你的代码包导入路径整齐划一是最直接的好处。

OK,言归正传,我下面继续关注go get这个命令本身。

命令特有标记

go get命令可以接受所有可用于go build命令和go install命令的标记。这是因为go get命令的内部步骤中完全包含了编译和安装这两个动作。另外,go get命令还有一些特有的标记,如下表所示:

表0-4 go get命令的特有标记说明

bitbucket.org/user/project/sub/directory
github.com/user/project/sub/directory
code. google.com/p/project/sub/directory
code.google.com/p/project.subrepository
code.google.com/p/project.subrepository/sub/directory
launchpad.net/project/series
launchpad.net/ project/series/sub/directory
launchpad.net/
user/project/branchlaunchpad.net/
user/project/branch/sub/directory
hub.jazz.net/ git/user/project/sub/directory
标记名称 标记描述
-d 让命令程序只执行下载动作,而不执行安装动作。
-f 仅在使用-u标记时才有效。该标记会让命令程序忽略掉对已下载代码包的导入路径的检查。如果下载并安装的代码包所属的项目是你从别人那里Fork过来的,那么这样做就尤为重要了。
-fix 让命令程序在下载代码包后先执行修正动作,而后再进行编译和安装。
-insecure 允许命令程序使用非安全的scheme(如HTTP)去下载指定的代码包。如果你用的代码仓库(如公司内部的Gitlab)没有HTTPS支持,可以添加此标记。请在确定安全的情况下使用它。
-t 让命令程序同时下载并安装指定的代码包中的测试源码文件中依赖的代码包。
-u 让命令利用网络来更新已有代码包及其依赖包。默认情况下,该命令只会从网络上下载本地不存在的代码包,而不会更新已有的代码包。

为了更好的理解这几个特有标记,我们先清除Lib工作区的src目录和pkg目录中的所有子目录和文件。现在我们使用带有-d标记的go get命令来下载同样的代码包:

hc@ubt:~$ go get -d github.com/hyper-carrot/go_lib/logging

现在,让我们再来看一下Lib工作区的目录结构:

/home/hc/golang/lib:
    bin/
    pkg/
    src/
        github.com/
        hyper-carrot/
        go_lib/
            logging/
    ...

我们可以看到,go get命令只将代码包下载到了Lib工作区的src目录,而没有进行后续的编译和安装动作。这个加入-d标记的结果。

再来看-fix标记。我们知道,绝大多数计算机编程语言在进行升级和演进过程中,不可能保证100%的向后兼容(Backward Compatibility)。在计算机世界中,向后兼容是指在一个程序或者代码库在更新到较新的版本后,用旧的版本程序创建的软件和系统仍能被正常操作或使用,或在旧版本的代码库的基础上编写的程序仍能正常编译运行的能力。Go语言的开发者们已想到了这点,并提供了官方的代码升级工具——fixfix工具可以修复因Go语言规范变更而造成的语法级别的错误。关于fix工具,我们将放在本节的稍后位置予以说明。

假设我们本机安装的Go语言版本是1.5,但我们的程序需要用到一个很早之前用Go语言的0.9版本开发的代码包。那么我们在使用go get命令的时候可以加入-fix标记。这个标记的作用是在检出代码包之后,先对该代码包中不符合Go语言1.5版本的语言规范的语法进行修正,然后再下载它的依赖包,最后再对它们进行编译和安装。

标记-u的意图和执行的动作都比较简单。我们在执行go get命令时加入-u标记就意味着,如果在本地工作区中已存在相关的代码包,那么就是用对应的代码版本控制系统的更新命令更新它,并进行编译和安装。这相当于强行更新指定的代码包及其依赖包。我们来看如下示例:

hc@ubt:~$ go get -v github.com/hyper-carrot/go_lib/logging

因为我们在之前已经检出并安装了这个代码包,所以我们执行上面这条命令后什么也没发生。还记得加入标记-v标记意味着会打印出被构建的代码包的名字吗?现在我们使用标记-u来强行更新代码包:

hc@ubt:~$ go get -v -u  github.com/hyper-carrot/go_lib/logging
github.com/hyper-carrot/go_lib (download)

其中,“(download)”后缀意味着命令从远程仓库检出或更新了该行显示的代码包。如果我们要查看附带-ugo get命令到底做了些什么,还可以加上一个-x标记,以打印出用到的命令。读者可以自己试用一下它。

智能的下载

命令go get还有一个很值得称道的功能。在使用它检出或更新代码包之后,它会寻找与本地已安装Go语言的版本号相对应的标签(tag)或分支(branch)。比如,本机安装Go语言的版本是1.x,那么go get命令会在该代码包的远程仓库中寻找名为“go1”的标签或者分支。如果找到指定的标签或者分支,则将本地代码包的版本切换到此标签或者分支。如果没有找到指定的标签或者分支,则将本地代码包的版本切换到主干的最新版本。

前面我们说在执行go get命令时也可以加入-x标记,这样可以看到go get命令执行过程中所使用的所有命令。不知道读者是否已经自己尝试了。下面我们还是以代码包github.com/hyper-carrot/go_lib为例,并且通过之前示例中的命令的执行此代码包已经被检出到本地。这时我们再次更新这个代码包:

hc@ubt:~$ go get -v -u -x github.com/hyper-carrot/go_lib
github.com/hyper-carrot/go_lib (download)
cd /home/hc/golang/lib/src/github.com/hyper-carrot/go_lib
git fetch
cd /home/hc/golang/lib/src/github.com/hyper-carrot/go_lib
git show-refcd /home/hc/golang/lib/src/github.com/hyper-carrot/go_lib
git checkout origin/masterWORK=/tmp/go-build034263530

在上述示例中,go get命令通过git fetch命令将所有远程分支更新到本地,而后有用git show-ref命令列出本地和远程仓库中记录的代码包的所有分支和标签。最后,当确定没有名为“go1”的标签或者分支后,go get命令使用git checkout origin/master命令将代码包的版本切换到主干的最新版本。下面,我们在本地增加一个名为“go1”的标签,看看go get命令的执行过程又会发生什么改变:

hc@ubt:~$ cd ~/golang/lib/src/github.com/hyper-carrot/go_lib
hc@ubt:~/golang/lib/src/github.com/hyper-carrot/go_lib$ git tag go1
hc@ubt:~$ go get -v -u -x github.com/hyper-carrot/go_lib
github.com/hyper-carrot/go_lib (download)
cd /home/hc/golang/lib/src/github.com/hyper-carrot/go_lib
git fetch
cd /home/hc/golang/lib/src/github.com/hyper-carrot/go_lib
git show-ref
cd /home/hc/golang/lib/src/github.com/hyper-carrot/go_lib
git show-ref tags/go1 origin/go1
cd /home/hc/golang/lib/src/github.com/hyper-carrot/go_lib
git checkout tags/go1
WORK=/tmp/go-build636338114

将这两个示例进行对比,我们会很容易发现它们之间的区别。第二个示例的命令执行过程中使用git show-ref查看所有分支和标签,当发现有匹配的信息又通过git show-ref tags/go1 origin/go1命令进行精确查找,在确认无误后将本地代码包的版本切换到标签“go1”之上。

Commandgo getThis function is very useful. When our code directly or indirectly depends on some code packages that are developed for multiple Go language versions at the same time, the correct version can be automatically checked out. It can also be said that the go get command has built-in functions for multi-version dependency management of code packages.

Here, I introduce to you how to use the go get command. The go get command, like the two commands introduced before, is an indispensable auxiliary tool when we write Go language programs and build Go language projects.

【Related recommendations: Go video tutorial, Programming teaching

The above is the detailed content of What is the use of go get command?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn