Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use go build command in Go language

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces how to use the go build command in Go language, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.

Go build

1. Used to test and compile multiple packages or a main package

2. The build command compiles the package to discard the compilation result of the non-main package, just to check whether it can be compiled

3. Preserve the results of main package compilation

A Go project under GOPATH will have the following three directories

Bin stores compiled executable files

Pkg stores compiled package files

Src stores project source files

In general, the bin and pkg directories can not be created, the go command will be created automatically (such as go install), you only need to create the src directory.

Use:

Add.go

The capitalization of the two numbers package cal / / indicates the public method func Add (a return b int) int {return aquib}.

Subtraction.go

The first letter of package cal// subtracts from each other, indicating the public method func Subtraction (a _ mai b int) int {return a mi b}.

Main.go

Package mainimport ("fmt"cal" / / to the custom package, cal must be in the src file and must be at the same level as main) func main () {fmt.Println ("hello word") fmt.Println ("add", cal.Add (10L20)) fmt.Println ("subtract", cal.Subtraction (40L50))}

1. Ordinary bag [non-mains bag]

Go build add.go [compiles add.go, does not generate exe execution files]

Go build-o add.exe add.go [specifies to generate the exe execution file, but cannot run this file, not the mainpackage]

2. Main package [package main]

Go build main.go [generate exe execution file]

Go build-o main.exe main.go [specify to generate main.exe execution file]

3. There are multiple files under the project folder

Enter the directory of the file

Go build [compiles all go files in the current directory by default]

Go build add.go subtraction.go [compiling add.go and subtraction.go]

Note:

1. If it is a normal package, when you execute go build, it will not produce any files. [non-mains package]

two。 If it's a main package, when you execute go

After build, it generates an executable file, exe, in the current directory. If you need to generate the corresponding file under $GOPATH/bin, you need to execute go

Install, or use the go build-o path / xxx.exe xxx.go

3. If there are multiple files under a project folder and you only want to compile a file, you can add the file name after go build, such as go build

The xxx.go;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, the default is your package name (main package), or the file name of the first source file (main package).

5.go build ignores the directory with "_" or "." The beginning of the go file.

Thank you for reading this article carefully. I hope the article "how to use go build commands in Go language" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report