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.
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 get
can 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 get
VCS supported by the command
Name | Main command | Description |
---|---|---|
hg | Mercurial is a lightweight distributed version control system, implemented in Python language, easy to learn and use, and highly scalable. | |
git | Git 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. | |
svn | Subversion 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. | |
bzr | Bazaar is an open source distributed version control system. But in comparison, there are not many projects that use it as a VCS. |
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.
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.
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
Master Domain name | Supported VCS | Code package remote import path example | |
---|---|---|---|
bitbucket.org | Git, Mercurial | bitbucket.org/user/project | bitbucket.org/user/project/sub/directory |
github.com | Git | github.com/user/project | github.com/user/project/sub/directory |
code.google.com | Git, Mercurial, Subversion | code.google.com/p/project | code. google.com/p/project/sub/directory code.google.com/p/project.subrepository code.google.com/p/project.subrepository/sub/directory |
launchpad.net | Bazaar | launchpad.net/project | 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 | hub.jazz.net/git/user/project | 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语言的开发者们已想到了这点,并提供了官方的代码升级工具——fix
。fix
工具可以修复因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)”后缀意味着命令从远程仓库检出或更新了该行显示的代码包。如果我们要查看附带-u
的go 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 get
This 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!