当前位置:网站首页>Go build command (go language compilation command) complete introduction
Go build command (go language compilation command) complete introduction
2022-06-23 22:17:00 【It workers】
Go The language compiles very fast .Go 1.17 After the version, the default is Go The concurrency of the language is used for function granularity concurrent compilation .
Go Language programming is basically in the form of source code , Whether it's your own code or third-party code , And take GOPATH As a working directory and a complete set of project directory rules . therefore Go There is no need to compile a language like C++ Configure various include paths as before 、 Linkbase address, etc .
Go Used in language go build The command is mainly used to compile code . During package compilation , If necessary , Will compile the package associated with it at the same time .
go build There are many ways to compile , For example, compile without parameters 、 File list compilation 、 Specify package compilation, etc , Using these methods, you can output executable files .
go build Parameterless compilation
The specific location of the code to be used in this section is ./src/chapter11/gobuild.
This set of tutorials all source download address :https://pan.baidu.com/s/1ORFVTOLEYYqDhRzeq0zIiQ Extract password :hfyf
The code is relative to GOPATH The directory relationship of is as follows :
. └── src └── chapter11 └── gobuild ├── lib.go └── main.go
main.go The code is as follows :
package mainimport (
"fmt"
)
func main() {
// Functions in the same package pkgFunc()
fmt.Println("hello world")
}lib.go The code is as follows :
package main
import "fmt"
func pkgFunc() {
fmt.Println("call pkgFunc")
}If there is no dependency in the source code GOPATH Package reference for , Then these source codes can use parameterless go build. The format is as follows :
go build
In the directory where the code is located (./src/chapter11/gobuild) Next use go build command , As shown below :
$ cd src/chapter11/gobuild/ $ go build $ ls gobuild lib.go main.go $ ./gobuild call pkgFunc hello world
The command line instructions and output are described below :
- The first 1 That's ok , Go to the source directory of this example .
- The first 2 That's ok ,go build At the beginning of compilation , Will search the current directory go Source code . In this case ,go build Will find lib.go and main.go Two documents . After compiling these two files , Generate an executable file with the current directory name and place it in the current directory , The executable here is go build.
- The first 3 Xing He 4 That's ok , List the files in the current directory , Compile successfully , Output go build Executable file .
- The first 5 That's ok , Run the executable of the current directory go build.
- The first 6 Xing He 7 That's ok , perform go build Output content after .
go build+ File list
When compiling multiple source files in the same directory , Can be in go build Multiple file names are provided after the ,go build It compiles the source code , Output the executable ,“go build+ File list ” The format is as follows :
go build file1.go file2.go……
In the directory where the code is located (./src/chapter11/gobuild) Use in go build, stay go build Add the source file name to compile , 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 are described below :
- The first 1 Walk in go build Add file list after , Select the to compile Go Source code .
- The first 2 Xing He 3 List the files in the current directory after compilation . This time the executable name becomes main.
- The first 4~6 That's ok , perform main file , Get the desired output .
- The first 7 That's ok , Try to adjust the order of the file list , take lib.go At the top of the list .
- The first 8 Xing He 9 That's ok , In the compilation result lib Executable file .
Tips
Use “go build+ File list ” Mode compile time , By default, the executable file selects the first source file in the file list as the executable file name .
If you need to specify the output executable file name , have access to -o Parameters , See the example below :
$ go build -o myexec main.go lib.go $ ls lib.go main.go myexec $ ./myexec call pkgFunc hello world
In the above code , stay go build And file list -o myexec Parameters , Indicates that the specified output file name is myexec.
Be careful
Use “go build+ File list ” Compile mode compile time , Each file in the file list must be of the same package Go Source code . in other words , Can not be like C++ The same applies to all projects Go The source code uses the file list method to compile . When compiling a complex project, you need to use “ Specify package compilation ” The way .
“go build+ File list ” This method is more suitable for Go Language tools with only a few files .
go build+ package
“go build+ package ” Set up GOPATH after , You can compile directly according to the package name , Even if the files in the package are added ( Add ) Delete ( except ) It does not affect the compilation instructions .
1) Code location and source code
The specific location of the code to be used in this section is ./src/chapter11/goinstall.
This set of tutorials all source download address :https://pan.baidu.com/s/1ORFVTOLEYYqDhRzeq0zIiQ Extract password :hfyf
be relative to GOPATH The directory relationship of is as follows :
. └── src └── chapter11 └──goinstall ├── main.go └── mypkg └── mypkg.go
main.go The code is as follows :
package main
import (
"chapter11/goinstall/mypkg"
"fmt"
)
func main() {
mypkg.CustomPkgFunc()
fmt.Println("hello world")
}mypkg.go The code is as follows :
package mypkg
import "fmt"
func CustomPkgFunc() {
fmt.Println("call CustomPkgFunc")
}2) Compile commands by package
Execute the following command to compile as a package goinstall Code :
$ export GOPATH=/home/davy/golangbook/code $ go build -o main chapter11/goinstall $ ./goinstall call CustomPkgFunc hello world
The code description is as follows :
- The first 1 That's ok , Set the environment variable GOPATH, The path here is the author's directory , It can be set according to the actual directory GOPATH.
- The first 2 That's ok ,
-oExecute the specified output file as main, Followed by the package name to compile . The package name is relative to GOPATH Under the src The directory starts with . - The first 3~5 That's ok , Compile successfully , perform main Get the desired output after .
When the reader compiles the code with reference to this example , Need to put GOPATH Change to your own directory . Be careful GOPATH The directory structure under , The source code must be placed in GOPATH Under the src Under the table of contents . Do not include Chinese in all directories .
go build Additional parameters at compile time
go build There are some additional parameters , More compilation information and more operations can be displayed , See the table below .
Additional parameters | To prepare notes |
|---|---|
-v | The package name is displayed at compile time |
-p n | Enable concurrent compilation , By default, this value is CPU Logic auditing |
-a | Forced rebuild |
-n | Print all the commands that will be used when compiling , But not really executed |
-x | Print all the commands that will be used when compiling |
-race | Turn on race detection |
The additional parameters in the table are arranged according to the frequency of use , Readers can choose to use .
边栏推荐
- Integrated management and control system of 3D intelligent warehousing and logistics park
- How does the API gateway intercept requests? How does the security of the API gateway reflect?
- DM sub database and sub table DDL "pessimistic coordination" mode introduction - tidb tool sharing
- How to defend the security importance of API gateway
- Error running PyUIC: Cannot start process, the working directory ‘-m PyQt5. uic. pyuic register. ui -o
- What is dynamic registration? What is static registration?
- [vulnerability recurrence]log4j vulnerability rce (cve-2021-44228)
- How to batch output EAN13 code to PDF
- Using barcode software to make certificates
- Modify jar package
猜你喜欢

MySQL de duplication query only keeps one latest record

北大、加州伯克利大学等联合| Domain-Adaptive Text Classification with Structured Knowledge from Unlabeled Data(基于未标记数据的结构化知识的领域自适应文本分类)

使用 Provider 改造屎一样的代码,代码量降低了2/3!

Sending network request in wechat applet

Installation and use of Minio

Leetcode algorithm interview sprint sorting algorithm theory (32)

Ten thousand words! Understand the inheritedwidget local refresh mechanism

Acl2022 | MVR: multi view document representation for open domain retrieval

Using the provider to transform the shit like code, the amount of code is reduced by 2/3!

CAD图在线Web测量工具代码实现(测量距离、面积、角度等)
随机推荐
Apt attack
H264_ AVC analysis
Detailed explanation of lkadoc interface tool
Trident tutorial
How to batch generate video QR code
[log service CLS] one click to start the efficient operation and maintenance journey of Tencent E-Sign
德国弗莱堡大学、希尔德斯海姆大学等联合 | Zero-Shot AutoML with Pretrained Models(基于预训练模型的零样本AutoML)
The most common usage scenarios for redis
Practice of business level disaster recovery switching drill
使用 Provider 改造屎一样的代码,代码量降低了2/3!
How many times can the server address and fortress address be entered before the connection is successful? Why did the connection fail?
TMUX support, file transfer tool Trz / Tsz (trzsz) similar to RZ / SZ
One article to help you understand automatic injection
Important announcement: Tencent cloud es' response strategy to log4j vulnerability
MySQL de duplication query only keeps one latest record
Redis function discarding Encyclopedia redis:: delete() is deprecated solution
万字长文!一文搞懂InheritedWidget 局部刷新机制
Tencent cloud database tdsql elite challenge Q & A (real-time update)
CMU doctoral thesis | meta reinforcement learning through memory, 118 Pages pdf
Ten thousand words! Understand the inheritedwidget local refresh mechanism