Table of Contents
How to compile and run Go language program
Home Backend Development Golang Can go language be compiled?

Can go language be compiled?

Dec 09, 2022 pm 06:20 PM
go golang go language compile

Go language can be compiled. Go language is a compiled static language, a programming language that requires compilation to run. There are two commands for compiling Go language programs: 1. "go build" command, which can compile Go language program code into a binary executable file, but the binary file needs to be run manually; 2. "go run" command, The Go language program will be run directly after compilation. A temporary file will be generated during the compilation process, but an executable file will not be generated.

Can go language be compiled?

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

go language can be compiled.

Go language is a compiled static language. It is a programming language that needs to be compiled to run. That is to say, the code needs to be generated by the compiler to generate binary machine code before running, and then the binary file can be run on the target machine.

How to compile and run Go language program


You can use go build or go run provided by Go language Command to compile Go language program:

  • go build command can compile 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. During the compilation process, a Temporary files, but will not generate executable files. This feature is very suitable for debugging programs. [Related recommendations: Go video tutorial, Programming teaching]

1. Command line tools

Here we use the command line tool that comes with Windows (also called CMD window or command prompt) to compile the Go language program. 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.

Can go language be compiled?
Select "Open a command window here" under Win7

Can go language be compiled?
Select "Open a Powershell window here" under Win10 go build command

2, go build command

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

go build fileName
Copy after login
  • 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. ), you can also omit it.

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 under a non-main package file name, the compiler will only perform syntax checking on the package and will not generate an executable file.

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 will be performed on the program source code in the current directory and will not be generated. executable file.

Use the go build command to compile the program we wrote in the previous section. The running results are as follows:

D:\code> go build .\demo.go
D:\code> .\demo.exe
Hello World!
Copy after login

where D:\code> corresponds to the current Directory, which is the code folder under the D drive, is automatically added by the command line tool and is not part of the compilation command.

第 1 行go build命令后面的参数中,.\表示当前目录。在Windows 系统中,当前目录使用.\表示;在类 Unix 系统(例如 Linux、MacOS 等)中,当前目录使用./表示。

注意,这里的go build命令中.\可以省略不写,不会影响编译。

另外,go build命令只有在执行出错的情况下才会有返回信息,执行成功的话是没有返回信息的,但是会在当前目录生成一个与 main 包文件同名的 .exe 可执行文件,如下图所示。

Can go language be compiled?

第 2 行中的.\demo.exe表示执行当前目录下的 demo.exe 程序。

第 3 行则是 demo.exe 程序的运行结果。

3、go run 命令

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

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

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

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

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

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

更多编程相关知识,请访问:编程视频!!

The above is the detailed content of Can go language be compiled?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What libraries are used for floating point number operations in Go? What libraries are used for floating point number operations in Go? Apr 02, 2025 pm 02:06 PM

The library used for floating-point number operation in Go language introduces how to ensure the accuracy is...

What is the problem with Queue thread in Go's crawler Colly? What is the problem with Queue thread in Go's crawler Colly? Apr 02, 2025 pm 02:09 PM

Queue threading problem in Go crawler Colly explores the problem of using the Colly crawler library in Go language, developers often encounter problems with threads and request queues. �...

How to solve the user_id type conversion problem when using Redis Stream to implement message queues in Go language? How to solve the user_id type conversion problem when using Redis Stream to implement message queues in Go language? Apr 02, 2025 pm 04:54 PM

The problem of using RedisStream to implement message queues in Go language is using Go language and Redis...

In Go, why does printing strings with Println and string() functions have different effects? In Go, why does printing strings with Println and string() functions have different effects? Apr 02, 2025 pm 02:03 PM

The difference between string printing in Go language: The difference in the effect of using Println and string() functions is in Go...

What should I do if the custom structure labels in GoLand are not displayed? What should I do if the custom structure labels in GoLand are not displayed? Apr 02, 2025 pm 05:09 PM

What should I do if the custom structure labels in GoLand are not displayed? When using GoLand for Go language development, many developers will encounter custom structure tags...

Which libraries in Go are developed by large companies or provided by well-known open source projects? Which libraries in Go are developed by large companies or provided by well-known open source projects? Apr 02, 2025 pm 04:12 PM

Which libraries in Go are developed by large companies or well-known open source projects? When programming in Go, developers often encounter some common needs, ...

How to solve the problem of Golang generic function type constraints being automatically deleted in VSCode? How to solve the problem of Golang generic function type constraints being automatically deleted in VSCode? Apr 02, 2025 pm 02:15 PM

Automatic deletion of Golang generic function type constraints in VSCode Users may encounter a strange problem when writing Golang code using VSCode. when...

How to ensure concurrency is safe and efficient when writing multi-process logs? How to ensure concurrency is safe and efficient when writing multi-process logs? Apr 02, 2025 pm 03:51 PM

Efficiently handle concurrency security issues in multi-process log writing. Multiple processes write the same log file at the same time. How to ensure concurrency is safe and efficient? This is a...

See all articles