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 compile go language programs

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

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to compile a go language program". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to compile go language programs".

Go is a compiled static language (like C), so before running a Go program, compile it into a binary executable.

Go language programs can be compiled through the go build or go run commands provided by the Go language:

The go build command compiles the Go language program code into a binary executable file, but we need to run the binary file manually

The go run command is more convenient, it will run the Go language program directly after compilation, and a temporary file will be generated during the compilation process, but not an executable file, which is very suitable for debugging programs.

We can directly use Windows's own command-line tools (also known as CMD windows or command prompts) to compile Go language programs. Microsoft later made an upgrade to the command-line tool and changed its name to Powershell,power, which means more powerful.

There are a variety of ways to open command-line tools, two of which are listed below:

You can open the command line tool in the start menu and cd to the directory where the Go source file is located.

You can also hold down the Shift key and right-click in the space where the Go source file is located, and then select "Open Command Line window here" or "Open Powershell window here" from the pop-up menu, which will automatically cd to the directory where the Go source file is located, as shown in the following figure. This method is more convenient and is recommended.

Tip: cd is a command of the command line tool, which is used to change the current directory and is an abbreviation for change directory.

Go build command

The go build command is used to start compilation, which compiles Go language programs and related dependencies into an executable file in the following syntax format.

Go build fileName

Where fileName is the required parameter, it can be one or more Go source file names (when there are multiple parameters, you need to use a space to separate the two adjacent parameters), or you can omit it.

When compiling with the go build command, the execution results of different parameters are also different.

1) when the parameter is not empty

If fileName is all source file names under the same main package (there may be one or more), the compiler generates an executable file with the same name as the first fileName (such as executing go build abc.go def.go. An abc.exe file is generated); if fileName is a source file name under a non-main package, the compiler will only check the syntax of the package and not generate an executable file.

2) when the parameter is empty

If the main package exists in the current directory, an "directory name .exe" executable with the same name as the current directory name will be generated (for example, when the go build command is executed in the hello directory, the hello.exe file will be generated); if the main package does not exist, only the program source code in the current directory will be syntactically checked, not the executable file will be generated.

Use the go build command to compile the program we wrote in the previous section, and the result is as follows:

D:\ code > go build.\ demo.goD:\ code >.\ demo.exeHello World!

Where D:\ code > corresponds to the current directory, that is, the code folder under disk D, which is automatically added by the command line tool and is not part of the compilation command.

In the arguments after the go build command on line 1,.\ indicates the current directory. In Windows systems, the current directory is represented by.\; in Unix-like systems (such as Linux, MacOS, and so on), the current directory is represented by. /.

Note that in the go build command here,.\ can be omitted and will not affect the compilation.

In addition, the go build command will only return information if there is an error in the execution, and no information will be returned if the execution is successful, but an .exe executable file with the same name as the main package file will be generated in the current directory, as shown in the following figure.

The.\ demo.exe in line 2 means to execute the demo.exe program in the current directory.

Line 3 is the result of running the demo.exe program.

Go run command

In addition to using the go build command, the Go language also provides us with the go run command, which combines compilation and execution instructions into one, and executes the Go program immediately after compilation, but does not generate an executable file.

The syntax format of the go run command is as follows:

Go run fileName

Where fileName is the required parameter, the parameter must be all source file names under the same main package, and cannot be empty.

Use the go run command to compile the program we wrote in the previous section, and the result is as follows:

D:\ code > go run demo.goHello World! Thank you for your reading, the above is the content of "how to compile go language programs", after the study of this article, I believe you have a deeper understanding of how to compile go language programs, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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