当前位置:网站首页>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 ,-o Execute 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 .

原网站

版权声明
本文为[It workers]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/12/202112161054416251.html