search
HomeBackend DevelopmentGolangHow to compile go language program

How to compile go language program

Jun 04, 2021 pm 02:31 PM
go languageprogramcompile

Method for compiling go language program: 1. Execute the "go build fileName" command in the command line tool, which will compile the program code into a binary executable file; 2. Execute "go" in the command line tool run fileName" command, it will run the Go language program directly after compilation.

How to compile go language program

The operating environment of this tutorial: Windows 10 system, GO version 1.18, Dell G3 computer.

Go language is a compiled static language (the same as C language), so before running a Go language program, it must be compiled into a binary executable file.

You can compile Go language programs through the go build or go run commands provided by Go language:

  • # The ##go build command can compile the Go language program code into a binary executable file, but we need to manually run the binary file;

  • go run## The # command is more convenient. It will run the Go language program directly after compilation. A temporary file will be generated during the compilation process, but an executable file will not be generated. This feature is very suitable for debugging programs.

  • We can directly use the command line tool that comes with Windows (also called CMD window or command prompt) to compile Go language programs. Microsoft later upgraded the command line tool and renamed it Powershell. Power means more powerful.

There are many ways to open command line tools. The following are two of the more commonly used ones:

    You can open the command line tool in the start menu, and then cd Go to the directory where the Go source files are located.
  • You can also hold down the Shift key and right-click the mouse in a blank space of the directory where the Go source file is located, and then select "Open command line window here" or "Open command line window here" in the pop-up menu Open the Powershell window here", which will automatically cd to the directory where the Go source file is located, as shown in the figure below. This method is more convenient and is recommended for everyone to use.
Tip: cd is a command of the command line tool, used to change the current directory, and is the abbreviation of change directory.

How to compile go language program

go build command The go build command is used to start compilation. The Go language program and related dependencies are compiled into an executable file with the following syntax format.

go build fileName

where fileName is the required parameter, which can be one or more Go source file names (when there are multiple parameters, you need to use spaces to separate two adjacent parameters), or you can omit them. Write.

When using the go build command to compile, the execution results of different parameters are also different.

1) When the parameter is not empty

If fileName is the name of all source files under the same main package (there may be one or more), the compiler will Generate an executable file with the same name as the first fileName (for example, executing go build abc.go def.go... will generate an abc.exe file); if fileName is a source file name under a non-main package, the compiler will Only syntax checking is performed on the package and no executable file is generated.

2) When the parameter is empty

If the main package exists in the current directory, a "directory name.exe" with the same name as the current directory name will be generated. Execution file (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 syntax check of the program source code in the current directory will be performed, and no executable file will be generated.

Use the

go build

command to compile the program we wrote in the previous section. The running results are as follows: <pre class='brush:php;toolbar:false;'>D:\code&gt; go build .\demo.go D:\code&gt; .\demo.exe Hello World!</pre>where

D:\code>

corresponds to the current directory, which is the code folder under the D drive. It is automatically added by the command line tool and is not part of the compilation command. Line 1

go build

In the parameters after the command, .\ represents the current directory. In Windows systems, the current directory is represented by .\; in Unix-like systems (such as Linux, MacOS, etc.), the current directory is represented by ./.

Note that
.\

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

In addition, the
go build

command will only return information if there is an error in execution. If the execution is successful, no information will be returned, but a file with The .exe executable file with the same name as the main package file is shown in the figure below.

How to compile go language program

.\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

除了使用go build命令外,Go语言还为我们提供了go run命令,go run命令将编译和执行指令合二为一,会在编译之后立即执行Go语言程序,但是不会生成可执行文件。

go run命令的语法格式如下:

go run fileName

其中 fileName 为所需要的参数,参数必须是同一 main 包下的所有源文件名,并且不能为空。

使用go run命令对我们上一节编写的程序进行编译,运行结果如下所示:

D:\code> go run demo.go
Hello World!

可以看到第 1 行的go run命令执行后,直接在第 2 行输出了程序的运行结果。

推荐学习:Golang教程

The above is the detailed content of How to compile go language program. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Choosing Between Golang and Python: The Right Fit for Your ProjectChoosing Between Golang and Python: The Right Fit for Your ProjectApr 19, 2025 am 12:21 AM

Golangisidealforperformance-criticalapplicationsandconcurrentprogramming,whilePythonexcelsindatascience,rapidprototyping,andversatility.1)Forhigh-performanceneeds,chooseGolangduetoitsefficiencyandconcurrencyfeatures.2)Fordata-drivenprojects,Pythonisp

Golang: Concurrency and Performance in ActionGolang: Concurrency and Performance in ActionApr 19, 2025 am 12:20 AM

Golang achieves efficient concurrency through goroutine and channel: 1.goroutine is a lightweight thread, started with the go keyword; 2.channel is used for secure communication between goroutines to avoid race conditions; 3. The usage example shows basic and advanced usage; 4. Common errors include deadlocks and data competition, which can be detected by gorun-race; 5. Performance optimization suggests reducing the use of channel, reasonably setting the number of goroutines, and using sync.Pool to manage memory.

Golang vs. Python: Which Language Should You Learn?Golang vs. Python: Which Language Should You Learn?Apr 19, 2025 am 12:20 AM

Golang is more suitable for system programming and high concurrency applications, while Python is more suitable for data science and rapid development. 1) Golang is developed by Google, statically typing, emphasizing simplicity and efficiency, and is suitable for high concurrency scenarios. 2) Python is created by Guidovan Rossum, dynamically typed, concise syntax, wide application, suitable for beginners and data processing.

Golang vs. Python: Performance and ScalabilityGolang vs. Python: Performance and ScalabilityApr 19, 2025 am 12:18 AM

Golang is better than Python in terms of performance and scalability. 1) Golang's compilation-type characteristics and efficient concurrency model make it perform well in high concurrency scenarios. 2) Python, as an interpreted language, executes slowly, but can optimize performance through tools such as Cython.

Golang vs. Other Languages: A ComparisonGolang vs. Other Languages: A ComparisonApr 19, 2025 am 12:11 AM

Go language has unique advantages in concurrent programming, performance, learning curve, etc.: 1. Concurrent programming is realized through goroutine and channel, which is lightweight and efficient. 2. The compilation speed is fast and the operation performance is close to that of C language. 3. The grammar is concise, the learning curve is smooth, and the ecosystem is rich.

Golang and Python: Understanding the DifferencesGolang and Python: Understanding the DifferencesApr 18, 2025 am 12:21 AM

The main differences between Golang and Python are concurrency models, type systems, performance and execution speed. 1. Golang uses the CSP model, which is suitable for high concurrent tasks; Python relies on multi-threading and GIL, which is suitable for I/O-intensive tasks. 2. Golang is a static type, and Python is a dynamic type. 3. Golang compiled language execution speed is fast, and Python interpreted language development is fast.

Golang vs. C  : Assessing the Speed DifferenceGolang vs. C : Assessing the Speed DifferenceApr 18, 2025 am 12:20 AM

Golang is usually slower than C, but Golang has more advantages in concurrent programming and development efficiency: 1) Golang's garbage collection and concurrency model makes it perform well in high concurrency scenarios; 2) C obtains higher performance through manual memory management and hardware optimization, but has higher development complexity.

Golang: A Key Language for Cloud Computing and DevOpsGolang: A Key Language for Cloud Computing and DevOpsApr 18, 2025 am 12:18 AM

Golang is widely used in cloud computing and DevOps, and its advantages lie in simplicity, efficiency and concurrent programming capabilities. 1) In cloud computing, Golang efficiently handles concurrent requests through goroutine and channel mechanisms. 2) In DevOps, Golang's fast compilation and cross-platform features make it the first choice for automation tools.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment