Home > Article > Backend Development > How to use the build command in go language
In the go language, the "go build" command is mainly used to compile code. The Go language program code can be compiled into a binary executable file, but the binary file needs to be run manually. "go build" has many compilation methods, such as no-parameter compilation, file list compilation, specified package compilation, etc. You can use these methods to output executable files.
The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.
Go language is a compiled static language (the same as C language), so before running a Go language program, it must be compiled into a binary executable file.
You can compile Go language programs through the go build
or go run
commands provided by Go language:
# The ##go build command can compile the Go language program code into a binary executable file, but we need to manually run the binary file;
go run## The # command is more convenient. It will run the Go language program directly after compilation. A temporary file will be generated during the compilation process, but an executable file will not be generated. This feature is very suitable for debugging programs.
go build command (go language compilation command) The go build command used in Go language is mainly used for compilation code. During the compilation process of the package, if necessary, the packages associated with it will be compiled at the same time.
go build has many compilation methods, such as no-parameter compilation, file list compilation, specified package compilation, etc. You can use these methods to output executable files.
go build No-parameter compilationThe specific location of the code needed in this section is
./src/chapter11/gobuild. The directory relationship of the code relative to GOPATH is as follows:
. └── src └── chapter11 └── gobuild ├── lib.go └── main.go
main.go code is as follows:
package main import ( "fmt" ) func main() { // 同包的函数 pkgFunc() fmt.Println("hello world") }
lib.go code as follows:
package main import "fmt" func pkgFunc() { fmt.Println("call pkgFunc") }
If the source code There are no package references that depend on GOPATH, then these source codes can use parameterless go build. The format is as follows:
go build
Use the go build command in the directory where the code is located (
./src/chapter11/gobuild), as shown below: <pre class="brush:js;toolbar:false">$ cd src/chapter11/gobuild/
$ go build
$ ls
gobuild lib.go main.go
$ ./gobuild
call pkgFunc
hello world</pre>
Command line instructions and The output description is as follows:
When compiling multiple source code files in the same directory, you can provide multiple file names after go build. go build will compile these source codes and output executable files. The format of "go build file list" is as follows:
go build file1.go file2.go……
Use go build in the directory where the code is located (./src/chapter11/gobuild), in go Add the source code file name to be compiled after build. The code is as follows:
$ go build main.go lib.go $ ls lib.go main main.go $ ./main call pkgFunc hello world $ go build lib.go main.go $ ls lib lib.go main main.go
The command line instructions and output instructions are as follows:
When compiling using the "
go build file list" method, the executable file selects the first source code file in the file list by default Output as executable file name. If you need to specify the output executable file name, you can use the
parameter, see the example below: <pre class="brush:js;toolbar:false">$ go build -o myexec main.go lib.go
$ ls
lib.go main.go myexec
$ ./myexec
call pkgFunc
hello world</pre>
In the above code, between go build and the file list The
parameter is inserted, indicating that the specified output file name is myexec. Note:
When compiling using the "go build file list" compilation method, each file in the file list must be the Go source code of the same package. In other words, the Go source code of all projects cannot be compiled using the file list method like C. When compiling complex projects, you need to use the "specified package compilation" method.
The "
go build file list" method is more suitable for tools written in Go language with only a small number of files.
After setting GOPATH, "go build package" can be compiled directly based on the package name, even if the files in the package are added (added) or deleted ( Except) does not affect compilation directives.
1) Code location and source code
本小节需要用到的代码具体位置是./src/chapter11/goinstall。
本套教程所有源码下载地址:https://pan.baidu.com/s/1ORFVTOLEYYqDhRzeq0zIiQ 提取密码:hfyf
相对于GOPATH的目录关系如下:
. └── src └── chapter11 └──goinstall ├── main.go └── mypkg └── mypkg.go
main.go代码如下:
package main import ( "chapter11/goinstall/mypkg" "fmt" ) func main() { mypkg.CustomPkgFunc() fmt.Println("hello world") }
mypkg.go代码如下:
package mypkg import "fmt" func CustomPkgFunc() { fmt.Println("call CustomPkgFunc") }
2) 按包编译命令
执行以下命令将按包方式编译 goinstall 代码:
$ export GOPATH=/home/davy/golangbook/code $ go build -o main chapter11/goinstall $ ./goinstall call CustomPkgFunc hello world
代码说明如下:
第 1 行,设置环境变量 GOPATH,这里的路径是笔者的目录,可以根据实际目录来设置 GOPATH。
第 2 行,-o执行指定输出文件为 main,后面接要编译的包名。包名是相对于 GOPATH 下的 src 目录开始的。
第 3~5 行,编译成功,执行 main 后获得期望的输出。
go build 编译时的附加参数
go build 还有一些附加参数,可以显示更多的编译信息和更多的操作,详见下表所示。
附加参数 | 备 注 |
---|---|
-v | 编译时显示包名 |
-p n | 开启并发编译,默认情况下该值为 CPU 逻辑核数 |
-a | 强制重新构建 |
-n | 打印编译时会用到的所有命令,但不真正执行 |
-x | 打印编译时会用到的所有命令 |
-race | 开启竞态检测 |
表中的附加参数按使用频率排列,读者可以根据需要选择使用。
The above is the detailed content of How to use the build command in go language. For more information, please follow other related articles on the PHP Chinese website!