当前位置:网站首页>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 .
边栏推荐
- The time deviation is more than 15 hours (54000 seconds), and the time cannot be automatically calibrated
- Redis function discarding Encyclopedia redis:: delete() is deprecated solution
- How to batch generate video QR code
- What is the meaning of the two-way and one-way cn2 lines in Hong Kong, China?
- Introduction to scikit learn machine learning practice
- Icml2022 | robust task representation for off-line meta reinforcement learning based on contrastive learning
- How do API gateways set up dynamic routing? What are the benefits of dynamic routing?
- Manually push a message platform
- WordPress plugin wpschoolpress 2.1.16 -'multiple'cross site scripting (XSS)
- Leetcode algorithm interview sprint sorting algorithm theory (32)
猜你喜欢

Teacher lihongyi from National Taiwan University - grade Descent 2

Ten thousand words! Understand the inheritedwidget local refresh mechanism

Intel openvino tool suite advanced course & experiment operation record and learning summary

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

Configuring error sets using MySQL for Ubuntu 20.04.4 LTS

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

ICML2022 | 基于对比学习的离线元强化学习的鲁棒任务表示

CAD图在线Web测量工具代码实现(测量距离、面积、角度等)

MySQL de duplication query only keeps one latest record

Icml2022 | robust task representation for off-line meta reinforcement learning based on contrastive learning
随机推荐
Detailed explanation of logical structure, physical structure and data operation
What is dynamic registration? What is static registration?
使用 Provider 改造屎一样的代码,代码量降低了2/3!
How to defend the security importance of API gateway
How to do API gateway routing? What are the other functions of API gateway?
How to improve the high concurrency of the server
Why is the server fortress machine error code 110? How to solve error code 110?
After CVM is configured with IPv6, it cannot be accessed as IPv6 or cannot access IPv6 sites
2021-12-18: find all letter ectopic words in the string. Given two characters
Detailed explanation of redisson distribution lock
Performance optimization of database 5- database, table and data migration
University of North China, Berkeley University of California, etc. | Domain Adaptive Text Classification with structural Knowledge from unlabeled data
Learn about reentrantlock
H264_ AVC analysis
Devops sharing: how to hold the meeting?
How does the API gateway intercept requests? How does the security of the API gateway reflect?
Intel openvino tool suite advanced course & experiment operation record and learning summary
How API gateway extends the importance of gateway extension
[tutorial] build a personal email system using Tencent lightweight cloud
The time deviation is more than 15 hours (54000 seconds), and the time cannot be automatically calibrated