Go language foundation go build command usage and example explanation

Time:2022-1-3
catalogue
  • go build

    • A go project under gopath will have the following three directories
    • use:
  • be careful:

Go language foundation go build command usage and example explanation

go build

1. It is used to test and compile multiple packages or one main package
2. The build command compiles the package, discards the compilation result of the non main package, and only checks whether it can be compiled
3. Keep the main package compilation results

A go project under gopath will have the following three directories

Go language foundation go build command usage and example explanation

  • Bin stores the compiled executable file
  • PKG stores the compiled package file
  • SRC stores project source files
  • Generally, the bin and PKG directories can be created without the go command (for example, go install). You only need to create the SRC directory.

use:

add.go

?
1
2
3
4
5
package cal
//The two numbers are capitalized to represent the public method
func Add(a,b int)int{
    return a+b
}

subtraction.go

?
1
2
3
4
5
package cal
//The two numbers are subtracted and the initial letter is capitalized, indicating the public method
func Subtraction(a,b int) int {
    return a-b
}

main.go

?
1
2
3
4
5
6
7
8
9
10
package main
import (
    "fmt"
    // the custom cal file must be in the same level as the "SRC cal" file
)
func main() {
    fmt.Println("hello word")
    fmt. Println ("add", Cal. Add (10,20))
    fmt. Println ("subtraction", Cal. Subtraction (40,50))
}

1. Ordinary package [non main package]

go build add. Go [compile add.go without generating exe executable]
go build -o add. exe add. Go [specify to generate an EXE executable file, but this file cannot be run, not a main package]

2. Package main

go build main. Go [generate exe executable]
go build -o main. exe main. Go [specify to generate the main.exe executable]

3. There are multiple files under the project folder
Enter the directory of the file

Go build [compile all go files in the current directory by default]
go build add. go subtraction. Go [compile add.go and subtract. Go]

be careful:

1. If it is an ordinary package, it will not generate any files after you execute go build. [non main package]

2. If it is the main package, when you execute go
After build, it will generate an executable exe in the current directory. If you need to generate the corresponding file under $gopath / bin, you need to execute go
Install, or use go build – O path / xxx exe xxx. go

3. If there are multiple files in a project folder and you only want to compile a file, you can add the file name after go build, such as go build
xxx. go; The go build command compiles all go files in the current directory by default.

4. You can also specify the file name of the compiled output. We can specify go build – O
xxxx. Exe. By default, it is your package name (main package) or the file name of the first source file (main package).

5. Go build will ignore the directory with “” Or “.” Start with the go file.

The above is the basic go language. Go build command usage and examples are explained in detail. For more information about go language commands, please pay attention to other relevant articles of developeppaper!