Home  >  Article  >  Backend Development  >  How to use the build command in go language

How to use the build command in go language

青灯夜游
青灯夜游Original
2023-01-17 13:52:123548browse

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.

How to use the build command in go language

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 compilation

The 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:

    Line 1, go to the source code directory of this example.
  • Line 2, go build will search the go source code of the current directory when compilation starts. In this example, go build will find two files, lib.go and main.go. After compiling these two files, an executable file with the current directory name is generated and placed in the current directory. The executable file here is go build.
  • Lines 3 and 4 list the files in the current directory. The compilation is successful and the go build executable file is output.
  • Line 5, run the executable file go build in the current directory.
  • Lines 6 and 7, the output content after executing go build.
go build file list

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:

    Line 1 adds the file list after go build. , select the Go source code that needs to be compiled.
  • Lines 2 and 3 list the files in the current directory after compilation. This time the executable file name becomes main.
  • Lines 4 to 6 execute the main file and get the expected output.
  • Line 7, try to adjust the order of the file list to put lib.go at the top of the list.
  • On lines 8 and 9, the lib executable file appears in the compilation result.
  • Tips:

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

-o

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

-o myexec

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.

go build package

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 还有一些附加参数,可以显示更多的编译信息和更多的操作,详见下表所示。

go build编译时的附加参数
附加参数 备  注
-v 编译时显示包名
-p n 开启并发编译,默认情况下该值为 CPU 逻辑核数
-a 强制重新构建
-n 打印编译时会用到的所有命令,但不真正执行
-x 打印编译时会用到的所有命令
-race 开启竞态检测

表中的附加参数按使用频率排列,读者可以根据需要选择使用。

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

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!

Statement:
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