What is the command to execute go language?

青灯夜游
Release: 2022-12-16 19:17:54
Original
4918 people have browsed it

The command to execute the go language is "go run". The "go run" command combines compilation and execution instructions into one, and 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; syntax "go run fileName", fileName The parameters must be the names of all source files under the same main package and cannot be empty.

What is the command to execute go language?

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 and will not automatically execute the file;

  • go The run 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.

go build command

The go build command is used to start compilation. It can combine Go language programs with related dependencies Compile it 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.

Note: The go build command will only return information if an execution error occurs. If the execution is successful, no information will be returned, but a main package file will be generated in the current directory. The .exe executable file with the same name is shown in the figure below.

What is the command to execute go language?

go run command

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

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

go run fileName
Copy after login

where fileName is the required parameter. The parameter must be the name of all source files under the same main package and cannot be empty.

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

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

You can see the go run command on line 1 After execution, the running result of the program is output directly on line 2.

【Related recommendations: Go video tutorial, Programming teaching

The above is the detailed content of What is the command to execute go language?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template