go语言之go run/build/install/get

来源:互联网 发布:matlab模块化编程 编辑:程序博客网 时间:2024/05/24 07:12

go run

 用于运行命令源码文件,只能接受一个命令源码文件以及若干个库文件作为参数
  • go run 命令的常用标记参数:
    • -a :强制编译相关代码
    • -n:打印编译过程中所用的命令,但并不执行编译过程
    • -x: 打印编译过程所需运行的命令,并执行编译;(注意和-n标记的区别)
    • -p N :并行编译,其中N为并行的数量 (可以是逻辑cpu的个数),可以加速我们的编译速度
    • -v:列出被编译的代码包的名称
    • -work:显示编译时的临时工作目录的路径,并且不删除它
Inspiron-5759:~/goc2p/src/helper/ds$ go run -v showds.go command-line-argumentsgoc2p/src/helper/ds:  showds.gogoc2p/src/helper/ds$ go run -work showds.go WORK=/tmp/go-build114678238/home/emindsoft/workdir/golang/xztlearn/goc2p/src/helper/ds:  showds.go

go build

  • 命令用于编译源码文件或代码包
  • 编译非命令源码文件不会产生任何结果文件,只检查库文件的有效性;

    • 编译命令源码文件时会在执行目录下生成可执行文件;
    • 不加任何参数,会把当前目录作为代码包并编译;
    • 执行该命令且以代码包的导入路径作为参数时,改代码包及其依赖会被编译;
    • 执行该命令且以若干源码文件作为参数时,只有这些文件会被编译;
  • go build 命令的常用标记和go run 命令基本一致;

go install

  • 编译并安装代码包或者源码文件
  • 产生归档文件和可执行文件(默认为当前目录)
emindsoft@emindsoft-Inspiron-5759:~/workdir/golang/xztlearn/goc2p/src$ go help installusage: go install [build flags] [packages]Install compiles and installs the packages named by the import paths,along with their dependencies.For more about the build flags, see 'go help build'.For more about specifying packages, see 'go help packages'.See also: go build, go get, go clean.
  • go get
Get downloads and installs the packages named by the import paths,along with their dependencies.The -d flag instructs get to stop after downloading the packages; that is,it instructs get not to install the packages.The -f flag, valid only when -u is set, forces get -u not to verify thateach package has been checked out from the source control repositoryimplied by its import path. This can be useful if the source is a local forkof the original.The -fix flag instructs get to run the fix tool on the downloaded packagesbefore resolving dependencies or building the code.The -insecure flag permits fetching from repositories and resolvingcustom domains using insecure schemes such as HTTP. Use with caution.The -t flag instructs get to also download the packages required to buildthe tests for the specified packages.The -u flag instructs get to use the network to update the named packagesand their dependencies.  By default, get uses the network to check outmissing packages but does not use it to look for updates to existing packages.Get also accepts build flags to control the installation. See 'go help build'.

实例:
1.在kubernets的编译过程中我们采用如下方式拉取kubernets的源码:

$ go get -d k8s.io/kubernetes
原创粉丝点击