• go install
  • go run
  • go clean
  • go env
  • go get

目录

go install

完成两步操作:

  • 第一步是生成结果文件(可执行文件或.a文件)
  • 第二步是把编译好的结果文件移到 $GOPATH/pkg 或 $GOPATH/bin
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
usage: go install [-i] [build flags] [packages]

Install compiles and installs the packages named by the import paths.

The -i flag installs the dependencies of the named packages as well.

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 run

编译并运行 Go 程序

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
usage: go run [build flags] [-exec xprog] gofiles... [arguments...]

Run compiles and runs the main package comprising(包括) the named Go source files.
A Go source file is defined to be a file ending in a literal ".go" suffix.

By default, 'go run' runs the compiled binary directly: 'a.out arguments...'.
If the -exec flag is given, 'go run' invokes the binary using xprog:
        'xprog a.out arguments...'.
If the -exec flag is not given, GOOS or GOARCH is different from the system
default, and a program named go_$GOOS_$GOARCH_exec can be found
on the current search path, 'go run' invokes the binary using that program,
for example 'go_nacl_386_exec a.out arguments...'. This allows execution of
cross-compiled programs when a simulator or other execution method is
available.

For more about build flags, see 'go help build'.

See also: go build.

go run 命令需要一个 go 文件作为参数,这个 go 源码文件必须包含 main 包和 main 函数,这样才可以运行,其他的参数和 go build 差不多.

1
go run hello.go

在运行 go run 的时候也可以给 Go 程序传递参数

1
go run main.go xxx

go clean

用来移除当前源码包和关联源码包里面编译生成的文件

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
usage: go clean [-i] [-r] [-n] [-x] [-cache] [-testcache] [build flags] [packages]

Clean removes object files from package source directories.
The go command builds most objects in a temporary directory,
so go clean is mainly concerned(关心) with object files left by other
tools or by manual invocations(调用) of go build.

Specifically(具体来说), clean removes the following files from each of the
source directories corresponding(相应) to the import paths:

        _obj/            old object directory, left from Makefiles
        _test/           old test directory, left from Makefiles
        _testmain.go     old gotest file, left from Makefiles
        test.out         old test log, left from Makefiles
        build.out        old test log, left from Makefiles
        *.[568ao]        object files, left from Makefiles

        DIR(.exe)        from go build
        DIR.test(.exe)   from go test -c
        MAINFILE(.exe)   from go build MAINFILE.go
        *.so             from SWIG

In the list, DIR represents the final path element of the
directory(最终路径元素), and MAINFILE is the base name of any Go source
file in the directory that is not included when building
the package.

The -i flag causes clean to remove the corresponding installed
archive or binary (what 'go install' would create).

The -n flag causes clean to print the remove commands it would execute,
but not run them.

The -r flag causes clean to be applied recursively(递归)) to all the
dependencies of the packages named by the import paths.

The -x flag causes clean to print remove commands as it executes them.

The -cache flag causes clean to remove the entire go build cache.

The -testcache flag causes clean to expire all test results in the
go build cache.

For more about build flags, see 'go help build'.

For more about specifying packages, see 'go help packages'.

go env

查看当前 go 的环境变量

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
set GOBIN=D:/code/Go_Path/bin
set GOCACHE=C:\Users\zhe\AppData\Local\go-build
set GOEXE=.exe
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=D:\code\Go_Path
set GORACE=
set GOROOT=D:\Go
set GOTMPDIR=
set GOTOOLDIR=D:\Go\pkg\tool\windows_amd64
set GCCGO=gccgo
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\zhe\AppData\Local\Temp\go-build534551030=/tmp/go-build -gno-record-gcc-switches

go get

Download and install packages and dependencies

下载或更新指定的代码包及其依赖包,并对他们进行编译安装

Usage:

1
2
3
go get [-d] [-f] [-fix] [-insecure] [-t] [-u] [-v] [build flags] [packages]

go get = git clone + go install

Options:

1
2
3
4
5
6
-d:    只下载,不安装
-f:     仅在使用 -u 标记时才有效。不让 `get -u` 去验证 import 中的每一个包都已经被获取了,这对于本地fork的包特别有用
-fix:   让 get 指令在解决依赖包或编译代码之前,先对已下载的包运行 fix tool 进行修复
-t:     同时下载需要为运行测试时所需要的包
-u:     强制使用网络去更新包和它的依赖包。
-v:     输出详细的执行过程和调试信息

Click here to checkout the Repo

See Also

Thanks to the authors 🙂

返回目录