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.
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.
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 emptyIf 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 emptyIf 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> go build .\demo.go
D:\code> .\demo.exe
Hello World!</pre>
where
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
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 ./
.
.\go buildcan be omitted in the go build command here, and it will not affect compilation.
In addition, the
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.
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 run命令的语法格式如下: 其中 使用 可以看到第 1 行的 推荐学习:Golang教程go build
命令外,Go语言还为我们提供了go run
命令,go run
命令将编译和执行指令合二为一,会在编译之后立即执行Go语言程序,但是不会生成可执行文件。go run fileName
fileName
为所需要的参数,参数必须是同一 main 包下的所有源文件名,并且不能为空。go run
命令对我们上一节编写的程序进行编译,运行结果如下所示:D:\code> go run demo.go
Hello World!
go run
命令执行后,直接在第 2 行输出了程序的运行结果。
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!

You should care about the "strings" package in Go because it provides tools for handling text data, splicing from basic strings to advanced regular expression matching. 1) The "strings" package provides efficient string operations, such as Join functions used to splice strings to avoid performance problems. 2) It contains advanced functions, such as the ContainsAny function, to check whether a string contains a specific character set. 3) The Replace function is used to replace substrings in a string, and attention should be paid to the replacement order and case sensitivity. 4) The Split function can split strings according to the separator and is often used for regular expression processing. 5) Performance needs to be considered when using, such as

The"encoding/binary"packageinGoisessentialforhandlingbinarydata,offeringtoolsforreadingandwritingbinarydataefficiently.1)Itsupportsbothlittle-endianandbig-endianbyteorders,crucialforcross-systemcompatibility.2)Thepackageallowsworkingwithcus

Mastering the bytes package in Go can help improve the efficiency and elegance of your code. 1) The bytes package is crucial for parsing binary data, processing network protocols, and memory management. 2) Use bytes.Buffer to gradually build byte slices. 3) The bytes package provides the functions of searching, replacing and segmenting byte slices. 4) The bytes.Reader type is suitable for reading data from byte slices, especially in I/O operations. 5) The bytes package works in collaboration with Go's garbage collector, improving the efficiency of big data processing.

You can use the "strings" package in Go to manipulate strings. 1) Use strings.TrimSpace to remove whitespace characters at both ends of the string. 2) Use strings.Split to split the string into slices according to the specified delimiter. 3) Merge string slices into one string through strings.Join. 4) Use strings.Contains to check whether the string contains a specific substring. 5) Use strings.ReplaceAll to perform global replacement. Pay attention to performance and potential pitfalls when using it.

ThebytespackageinGoishighlyeffectiveforbyteslicemanipulation,offeringfunctionsforsearching,splitting,joining,andbuffering.1)Usebytes.Containstosearchforbytesequences.2)bytes.Splithelpsbreakdownbyteslicesusingdelimiters.3)bytes.Joinreconstructsbytesli

ThealternativestoGo'sbytespackageincludethestringspackage,bufiopackage,andcustomstructs.1)Thestringspackagecanbeusedforbytemanipulationbyconvertingbytestostringsandback.2)Thebufiopackageisidealforhandlinglargestreamsofbytedataefficiently.3)Customstru

The"bytes"packageinGoisessentialforefficientlymanipulatingbyteslices,crucialforbinarydata,networkprotocols,andfileI/O.ItoffersfunctionslikeIndexforsearching,Bufferforhandlinglargedatasets,Readerforsimulatingstreamreading,andJoinforefficient

Go'sstringspackageiscrucialforefficientstringmanipulation,offeringtoolslikestrings.Split(),strings.Join(),strings.ReplaceAll(),andstrings.Contains().1)strings.Split()dividesastringintosubstrings;2)strings.Join()combinesslicesintoastring;3)strings.Rep


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
