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

What is the use of go install command?

青灯夜游
Release: 2023-01-29 11:20:29
Original
4051 people have browsed it

The "go install" command is used to compile and install the specified code packages and their dependent packages; when the dependent packages of the specified code package have not been compiled and installed, this command will first process the dependent packages. . The "go install" command will place the compiled intermediate files in the pkg directory of GOPATH, and permanently place the compilation results in the bin directory of GOPATH.

What is the use of go install command?

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

go install command - compile and install

go install command is used to compile and install the specified code package and their dependent packages. When the dependent packages of the specified code package have not been compiled and installed, this command will first process the dependent packages. As with the go build command, the code package parameters passed to the go install command should be provided in the form of import paths. Moreover, most of the tags of the go build command can also be used in the go install command. In fact, the go install command only does one more thing than the go build command, which is: install the compiled result file to the specified directory.

Before giving a detailed explanation of the go install command, let us first review the directory structure of goc2p. To save space, I have hidden the source code files in the code package here. As follows:

$HOME/golang/goc2p:
    bin/
    pkg/
    src/
        cnet/
        logging/
        helper/
            ds/
        pkgtool/
Copy after login

We see that there are three subdirectories in the goc2p project, namely the bin directory, the pkg directory and the src directory. Now only the src directory contains some directories, while the other two directories are empty.

Now, let’s look at the rules for installing code packages.

Install the code package

If the code package followed by the go install command only contains the library source code file, then go install## The # command will save the compiled result file in the pkg directory of the workspace where the source code file is located. For code packages that only contain library source files, this result file is the corresponding code package archive file (also called a static link library file, the name ends with .a). In contrast, when we use the go build command to compile a code package that only contains library source files, no result files will be generated in the src directory and pkg directory of the current workspace. The result file will be generated in a temporary directory for compilation purposes, but will not cause any changes to the current workspace directory.

If we execute the

go install command without any code package parameters, the command will try to compile the code package corresponding to the current directory. For example, we now want to install the code package pkgtool:

hc@ubt:~/golang/goc2p/src/pkgtool$ go install -v -work
WORK=D:\cygwin\tmp\go-build758586887
pkgtool
Copy after login

As we said before, after executing the

go install command, the specified code package will be compiled and then installed. Among them, the compiled code package uses the same program as the go build command. Therefore, after executing the go install command, a temporary directory with a name prefixed with go-build will first be created. If we want to forcefully reinstall the specified code package and its dependent packages, we need to add the tag -a:

hc@ubt:~/golang/goc2p/src/pkgtool$ go install -a -v -work
WORK=/tmp/go-build014992994
runtime
errors
sync/atomic
unicode
unicode/utf8
sort
sync
io
syscall
strings
bytes
bufio
time
os
path/filepath
pkgtool
Copy after login

. As you can see, the code package

pkgtool only depends on Code packages in the Go language standard library.

Now let’s take a look at the goc2p project directory:

$HOME/golang/goc2p:
    bin/
    pkg/
        linux_386/
            pkgtool.a
    src/
Copy after login

Now there is an additional subdirectory in the pkg directory. Readers who have read Section 0.0 should already know that linux_386 is called a platform-related directory. Its name can be obtained from ${GOOS}_${GOARCH}. Among them, ${GOOS} and ${GOARCH} are the values ​​of the environment variables GOOS and GOARCH respectively in the current operating system. If they are not present, the Go language will use its internal predetermined values. The above example is run on a computer with compute architecture 386 and operating system Linux. Therefore, the platform-related directory here is linux_386. We also see that there is a file named pkgtool.a in the platform-related directory in the goc2p project. This is the archive file of the code package pkgtool. The file name is the combination of the code package name and the ".a" suffix.

In fact, the archive files of the code package are not always saved directly in the platform-related directory of the pkg directory, but may also be saved in a subdirectory of this platform-related directory. Next we install the cnet/ctcp package:

hc@ubt:~/golang/goc2p/src/pkgtool$ go install -a -v -work ../cnet/ctcp
WORK=/tmp/go-build083178213
runtime
errors
sync/atomic
unicode
unicode/utf8
math
sync
sort
io
syscall
internal/singleflight
bytes
strings
strconv
bufio
math/rand
time
reflect
os
fmt
log
runtime/cgo
logging
net
cnet/ctcp
Copy after login

Please note that we install the cnet/ctcp package in the directory corresponding to the code package pkgtool. We used a directory relative path.

In fact, this method of providing the location of the code package is called the local code package path method, and it is also a method accepted by all Go commands, including the go build command that has been introduced before. However, it should be noted that the local code package path can only be presented in the form of a directory relative path, and the directory absolute path cannot be used. Please see the following example:

hc@ubt:~/golang/goc2p/src/cnet/ctcp$ go install -v -work ~/golang/goc2p/src/cnet/ctcp
can't load package: package /home/hc/golang/goc2p/src/cnet/ctcp: import "/home/hc/golang/goc2p/src/cnet/ctcp": cannot import absolute path
Copy after login

From the command prompt information in the above example, we know that providing the code package location in the form of an absolute path to the directory will not be recognized by the Go command.

这是由于Go认为本地代码包路径的表示只能以“./”或“../”开始,再或者直接为“.”或“..”,而代码包的代码导入路径又不允许以“/”开始。所以,这种用绝对路径表示代码包位置的方式也就不能被支持了。

上述规则适用于所有Go命令。读者可以自己尝试一下,比如在执行go build命令时分别以代码包导入路径、目录相对路径和目录绝对路径的形式提供代码包位置,并查看执行结果。

我们已经通过上面的示例强行的重新安装了cnet/ctcp包及其依赖包。现在我们再来看一下goc2p的项目目录:

$HOME/golang/goc2p:
    bin/
    pkg/
        linux_386/
            /cnet
                ctcp.a
            logging.a
            pkgtool.a
    src/
Copy after login

我们发现在pkg目录的平台相关目录下多了一个名为cnet的目录,而在这个目录下的就是名为ctcp.a的代码包归档文件。由此我们可知,代码包归档文件的存放目录的相对路径(相对于当前工作区的pkg目录的平台相关目录)即为代码包导入路径除去最后一个元素后的路径。而代码包归档文件的名称即为代码包导入路径中的最后一个元素再加“.a”后缀。再举一个例子,如果代码包导入路径为x/y/z,则它的归档文件存放路径的相对路径即为x/y/,而这个归档文件的名称即为z.a。

回顾代码包pkgtool的归档文件的存放路径。因为它的导入路径中只有一个元素,所以其归档文件就被直接存放到了goc2p项目的pkg目录的平台相关目录下了。

此外,我们还发现pkg目录的平台相关目录下还有一个名为logging.a的文件。很显然,我们并没有显式的安装代码包logging。这是因为go install命令在安装指定的代码包之前,会先去安装指定代码包的依赖包。当依赖包被正确安装后,指定的代码包的安装才会开始。由于代码包cnet/ctcp依赖于goc2p项目(即当前工作区)中的代码包logging,所以当代码包logging被成功安装之后,代码包cnet/ctcp才会被安装。

还有一个问题:上述的安装过程涉及到了那么多代码包,那为什么goc2p项目的pkg目录中只包含该项目中代码包的归档文件呢?实际上,go install命令会把标准库中的代码包的归档文件存放到Go语言安装目录的pkg子目录中,而把指定代码包依赖的第三方项目的代码包的归档文件存放到当前工作区的pkg目录下。这样就实现了Go语言标准库代码包的归档文件与用户代码包的归档文件,以及处在不同工作区的用户代码包的归档文件之间的分离。

安装命令源码文件

除了安装代码包之外,go install命令还可以安装命令源码文件。为了看到安装命令源码文件是goc2p项目目录的变化,我们先把该目录还原到原始状态,即清除bin子目录和pkg子目录下的所有目录和文件。然后,我们来安装代码包helper/ds下的命令源码文件showds.go,如下:

hc@ubt:~/golang/goc2p/src$ go install helper/ds/showds.go
go install: no install location for .go files listed on command line (GOBIN not set)
Copy after login

这次我们没能成功安装。该Go命令认为目录/home/hc/golang/goc2p/src/helper/ds不在环境GOPATH中。我们可以通过Linux的echo命令来查看一下环境变量GOPATH的值:

hc@ubt:~/golang/goc2p/src$ echo $GOPATH
/home/hc/golang/lib:/home/hc/golang/goc2p
Copy after login

环境变量GOPATH的值中确实包含了goc2p项目的根目录。这到底是怎么回事呢?

我通过查看Go命令的源码文件找到了其根本原因。在上一小节我们提到过,在环境变量GOPATH中包含多个工作区目录路径时,我们需要在编译命令源码文件前先对环境变量GOBIN进行设置。实际上,这个环境变量所指的目录路径就是命令程序生成的结果文件的存放目录。go install命令会把相应的可执行文件放置到这个目录中。

由于命令go build在编译库源码文件后不会产生任何结果文件,所以自然也不用会在意结果文件的存放目录。在该命令编译单一的命令源码文件或者包含一个命令源码文件和多个库源码文件时,在结果文件存放目录无效的情况下会将结果文件(也就是可执行文件)存放到执行该命令时所在的目录下。因此,即使环境变量GOBIN的值无效,我们在执行go build命令时也不会见到这个错误提示信息。

然而,go install命令中一个很重要的步骤就是将结果文件(归档文件或者可执行文件)存放到相应的目录中。所以,go install命令在安装命令源码文件时,如果环境变量GOBIN的值无效,则它会在最后检查结果文件存放目录的时候发现这一问题,并打印与上述示例所示内容类似的错误提示信息,最后直接退出。

这个错误提示信息在我们安装多个库源码文件时也有可能遇到。示例如下:

hc@ubt:~/golang/goc2p/src/pkgtool$ go install envir.go fpath.go ipath.go pnode.go util.go
go install: no install location for .go files listed on command line (GOBIN not set)
Copy after login

而且,在我们为环境变量GOBIN设置了正确的值之后,这个错误提示信息仍然会出现。这是因为,只有在安装命令源码文件的时候,命令程序才会将环境变量GOBIN的值作为结果文件的存放目录。而在安装库源码文件时,在命令程序内部的代表结果文件存放目录路径的那个变量不会被赋值。最后,命令程序会发现它依然是个无效的空值。所以,命令程序会同样返回一个关于“无安装位置”的错误。这就引出一个结论,我们只能使用安装代码包的方式来安装库源码文件,而不能在go install命令罗列并安装它们。另外,go install命令目前无法接受标记-o以自定义结果文件的存放位置。这也从侧面说明了go install命令不支持针对库源码文件的安装操作。

至此,我们对怎样用go install命令来安装代码包以及命令源码文件进行了说明。如果你已经熟知了go build命令,那么理解这些内容应该不在话下。

【相关推荐:Go视频教程编程教学

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template