Table of Contents
Compiles and Runs in One Step
Ideal for Development and Testing
Limitations
Uses the Go Toolchain Internationally
Home Backend Development Golang What does the go run command do?

What does the go run command do?

Aug 03, 2025 am 03:49 AM
go run命令

go run is a command used to quickly compile and execute Go programs. 1. It completes compilation and running in one step, generates temporary executable files and deletes them after the program is finished; 2. It is suitable for independent programs containing main functions, which are easy to develop and test; 3. It supports multi-file running, and can be executed through go run *.go or lists all files; 4. It automatically processes dependencies and uses the module system to parse external packages; 5. It is not suitable for libraries or packages, and does not generate persistent binary files. Therefore, it is suitable for quick testing during scripts, learning and frequent modifications. It is an efficient and concise way of running.

What does the go run command do?

The go run command compiles and executes a Go program in one step, without leaving behind a compiled binary file.

What does the go run command do?

Here's how it works and when to use it:

Compiles and Runs in One Step

When you run go run main.go , Go does the following:

What does the go run command do?
  • Compiles the source code (eg, main.go ) into a temporary executable.
  • Immediately runs that execute.
  • Deletes the temporary binary after execution.

This is convenient for quickly testing code during development.

Ideal for Development and Testing

You don't need to manually compile with go build and then run the binary. For example:

What does the go run command do?
 go run main.go

is faster than:

 go build main.go
./main

Especially when you're making frequent changes and just want to see output quickly.

Limitations

  • Only works with standalone programs (ie, package main with a main() function).
  • Not suitable for packages or libraries.
  • If your program has multiple source files in the same package, you can still run them:
     go run *.go

    or list them explicitly:

     go run main.go helper.go

    Uses the Go Toolchain Internationally

    go run handles dependencies automatically. If your code imports external packages, Go will resolve and include them (using the module system if go.mod exists).

    In short:
    go run is a fast, temporary way to execute Go code — great for scripts, learning, and development. It skips saving the binary, so it's clean and efficient for testing.

    Basically, it's "run this Go code now" without the extra steps.

    The above is the detailed content of What does the go run command do?. 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

Undress AI Tool

Undress AI Tool

Undress images for free

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.

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)

Hot Topics

PHP Tutorial
1510
276
What does the go run command do? What does the go run command do? Aug 03, 2025 am 03:49 AM

gorun is a command for quickly compiling and executing Go programs. 1. It completes compilation and running in one step, generates temporary executable files and deletes them after the program is finished; 2. It is suitable for independent programs containing main functions, which are easy to develop and test; 3. It supports multi-file operation, and can be executed through gorun*.go or lists all files; 4. It automatically processes dependencies and uses the module system to parse external packages; 5. It is not suitable for libraries or packages, and does not generate persistent binary files. Therefore, it is suitable for rapid testing during scripts, learning and frequent modifications. It is an efficient and concise way of running.

How to connect to a SQL database in Go? How to connect to a SQL database in Go? Aug 03, 2025 am 09:31 AM

To connect to SQL databases in Go, you need to use the database/sql package and a specific database driver. 1. Import database/sql packages and drivers (such as github.com/go-sql-driver/mysql), note that underscores before the drivers indicate that they are only used for initialization; 2. Use sql.Open("mysql","user:password@tcp(localhost:3306)/dbname") to create a database handle, and call db.Ping() to verify the connection; 3. Use db.Query() to execute query, and db.Exec() to execute

What are some best practices for logging in Go? What are some best practices for logging in Go? Aug 04, 2025 pm 04:48 PM

Using structured logging, adding context, controlling log levels, avoiding logging sensitive data, using consistent field names, correctly logging errors, taking into account performance, centrally monitoring logs and unifying configurations are best practices in Go to achieve efficient logging. First, structured logs in JSON format (such as using uber-go/zap or rs/zerolog) facilitate machine parsing and integrating ELK, Datadog and other tools; second, log traceability is enhanced by requesting context information such as ID and user ID, and can be injected through context.Context or HTTP middleware; third, use Debug, Info, Warn, Error levels reasonably, and operate through environment variables.

How to gracefully shut down a Go service? How to gracefully shut down a Go service? Aug 05, 2025 pm 08:21 PM

Usesignal.Notify()tolistenforSIGINT/SIGTERMandtriggershutdown;2.RuntheHTTPserverinagoroutineandblockuntilasignalisreceived;3.Callserver.Shutdown()withacontexttimeouttostopacceptingnewrequestsandallowin-flightonestocomplete;4.Propagatetheshutdownconte

How does a type implement an interface in Go? How does a type implement an interface in Go? Aug 03, 2025 pm 03:19 PM

InGo,atypeimplementsaninterfaceimplicitlybyprovidingallrequiredmethodswithoutexplicitdeclaration.1.Interfacesaresatisfiedautomaticallywhenatypehasmethodsmatchingtheinterface'ssignatureexactly.2.No"implements"keywordisneeded—ducktypingisused

How to parse XML data in Go How to parse XML data in Go Aug 05, 2025 pm 07:24 PM

Parsing XML data is very simple in Go, just use the built-in encoding/xml package. 1. Define a structure with xml tag to map XML elements and attributes, such as xml:"name" corresponding child elements, xml:"contact>email" handles nesting, xml:"id, attr" reads attributes; 2. Use xml.Unmarshal to parse XML strings into structures; 3. For files, use os.Open to open them and decode them through xml.NewDecoder, which is suitable for streaming processing of large files; 4. When processing duplicate elements, in the structure

How to get the current time in Go How to get the current time in Go Aug 06, 2025 am 11:28 AM

Usetime.Now()togetthecurrentlocaltimeasatime.Timeobject;2.FormatthetimeusingtheFormatmethodwithlayoutslike"2006-01-0215:04:05";3.GetUTCtimebycallingUTC()ontheresultoftime.Now();4.Extractcomponentslikeyear,month,dayusingmethodssuchasYear(),M

How to create and use custom error types in Go How to create and use custom error types in Go Aug 11, 2025 pm 11:08 PM

In Go, creating and using custom error types can improve the expressiveness and debugability of error handling. The answer is to create a custom error by defining a structure that implements the Error() method. For example, ValidationError contains Field and Message fields and returns formatted error information. The error can then be returned in the function, detecting specific error types through type assertions or errors.As to execute different logic. You can also add behavioral methods such as IsCritical to custom errors, which are suitable for scenarios that require structured data, differentiated processing, library export or API integration. In simple cases, errors.New, and predefined errors such as ErrNotFound can be used for comparable

See all articles