SoFunction
Updated on 2025-03-02

The difference between Go Run, Go Build, Go Install

During the development of Go language,go rungo buildandgo installare three commonly used commands. Although they are all used to handle Go code, their functions and usage scenarios are very different. This article will explore the differences between these three commands in depth, and help you fully understand their working principles and practical applications through detailed code examples and technical explanations.

1. Pre-knowledge: Basics of Go Language

Before delving into these three commands, let's briefly review the basics of Go.

  • Go language:Go (also known as Golang) is a statically typed, compiled language developed by Google. It is known for its simplicity, efficiency and concurrent support.
  • Go module: The Go module is a dependency management mechanism introduced by Go 1.11, which is used to manage project dependencies.
  • Go workspace: The Go workspace is a directory structure that stores Go code and compile products. By default, the Go workspace is located inGOPATHThe path specified by the environment variable.

2. Go Run: Run Go code quickly

go runCommands are used to quickly compile and run Go programs. It is suitable for rapid iteration and testing during the development phase without the need to generate an executable file.

Features:

  • Compile and rungo runThe Go code will be compiled first, and the generated executable file will be run immediately.
  • Temporary documents: The compiled executable file is usually stored in a temporary directory and will be deleted after the run is completed.
  • Applicable scenarios: Suitable for rapid testing and debugging without the need to generate the final executable.

Example:Suppose you have a simple Go program

// 
package main

import "fmt"

func main() {
    ("Hello, Go!")
}

You can usego runCommands to run it quickly:

go run 

Output:

Hello, Go!

3. Go Build: Generate executable files

go buildCommands are used to compile Go code and generate executable files. The generated executable file can be run independently without the need for a Go compiler.

Features:

  • Generate executable filesgo buildThe Go code will be compiled into an executable file and stored in the current directory or in the specified directory.
  • Cross-platform compilation: Can be setGOOSandGOARCHEnvironment variables, generate executable files for different operating systems and architectures.
  • Applicable scenarios: Suitable for generating the final executable for deployment and distribution.

Example:Continue to use the aboveFiles you can usego buildGenerate executable file:

go build -o myapp 

The generated executable filemyappIt can be run directly from the command line:

./myapp

Output:

Hello, Go!

4. Go Install: Install the executable file to GOPATH

go installCommands are used to compile and install Go code toGOPATHofbinin the directory. The installed executable file can be run directly anywhere on the system.

Features:

  • Install to GOPATHgo installThe generated executable file will be installed toGOPATH/binin the directory.
  • Globally available: The installed executable file can run directly anywhere on the system without specifying a path.
  • Applicable scenarios: Executable files for installation of command line tools and global use.

Example:Suppose you have a command line toolmytool, you can usego installInstall it toGOPATH/binIn the directory:

go install mytool

After installation, you can run it directly from the command linemytool

mytool

5. Differences and application scenarios

Order Function Applicable scenarios Generate file location
go run Compile and run Quick testing and debugging Temporary directory (delete after running)
go build Generate executable files Generate the final executable file for deployment Current directory or specified directory
go install Install to GOPATH/bin Install command line tools and global executable files GOPATH/binTable of contents

6. Practical application examples

6.1 Quick Testing and Debugging

During the development phase, you may need to modify and test the code frequently. usego runIt can quickly verify the correctness of the code:

go run 

6.2 Generate executable files

When you need to generate the final executable file, usego build

go build -o myapp 

GeneratedmyappFiles can be distributed to others for use.

6.3 Installing command line tools

Suppose you have developed a command line toolmytool, you can usego installInstall it into the system:

go install mytool

After installation, you can run it directly anywheremytool

7. Summary

go rungo buildandgo installThey are three commonly used commands in Go language development. They each have different functions and applicable scenarios. Understanding their differences and application scenarios can help you develop and deploy Go programs more efficiently.

This is the end of this article about the differences between Go Run, Go Build, Go Install. For more related contents of Go Run, Go Build, Go Install, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!