Go 시작하기: Go run and go build

작성자

카테고리:

← 피드로
DEV Community · Sahal Mahfudh · 2026-09-23 개발(SW)

Go is a compiled language. This means your human-readable code must be translated into machine code before the computer can execute it.

When you start your journey with Go, the two most important commands you will use daily are go run and go build. While they might seem similar at first glance, they serve entirely different purposes in the software development lifecycle.

Here is a breakdown of how and when to use them.

Creating a Sample Project

Before testing these commands, you need to set up a basic Go project. Open your terminal, create a new directory for your project, and initialize a Go module:

➔ mkdir golang-project
➔ cd golang-project
➔ go mod init golang-project

Enter fullscreen mode Exit fullscreen mode

Next, create a file named main.go inside that folder and add the following code:

package main

import "fmt"

func main() {
    fmt.Println("go is running")
}

Enter fullscreen mode Exit fullscreen mode

1. go run

The go run command is built for quick testing while you write code. When you execute go run main.go, Go works behind the scenes to:

  • Compile your code into a temporary file.
  • Run the program immediately.
  • Delete the temporary file as soon as the program finishes.

How to use:

➔ go run main.go

Enter fullscreen mode Exit fullscreen mode

Output:

go is running

Enter fullscreen mode Exit fullscreen mode

When to use go run:

  • Active Development: When you are writing code and want to quickly test if your logic works.
  • Testing Scripts: When you are using Go to write quick, script-like tools where you don’t need a permanent executable file cluttering up your directory.

Note: Because go run has to compile the code every single time you execute it, it is not meant for production environments.

2. go build

Use go build when your application is finished and ready to deploy. When you execute this command, Go will:

  • Compile your code into a permanent, standalone executable file.
  • Bundle everything your program needs into that single file.
  • Allow you to run the program on other computers without needing to install Go.

How to use:

➔ go build

Enter fullscreen mode Exit fullscreen mode

After running this command, you will see a new executable file in your directory. On Linux or macOS, it will be named golang-project, and on Windows, it will be golang-project.exe.

➔ ls
go.mod   golang-project   main.go

Enter fullscreen mode Exit fullscreen mode

Changing the Executable Name:
By default, the binary or executable file name matches your project name. To change the name of the executable file, use the -o flag:

➔ go build -o my-custom-app
➔ ls
go.mod         golang-project main.go        my-custom-app

Enter fullscreen mode Exit fullscreen mode

Running go build -o my-custom-app generates the my-custom-app file directly in your project directory.

How to run the built binary:

# On Linux / macOS
➔ ./golang-project
go is running

# On Windows
golang-project.exe

Enter fullscreen mode Exit fullscreen mode

When to use go build:

  • Production Deployment: When you are ready to ship your application to a server or to your users.
  • Performance: Because the code is already compiled, running the binary is extremely fast.

Cross-Compilation with go build
Go has a great feature called cross-compilation, which lets you build an app for a completely different operating system right from your current computer.

Just change the GOOS (Operating System) and GOARCH (Architecture) variables when building.

Here is the complete list of simple commands for the major operating systems:

  • Building for Windows
GOOS=windows GOARCH=amd64 go build -o my-app.exe

Enter fullscreen mode Exit fullscreen mode

  • Building for Linux
GOOS=linux GOARCH=amd64 go build -o my-app-linux

Enter fullscreen mode Exit fullscreen mode

  • Building for macOS (Apple Silicon)
GOOS=darwin GOARCH=arm64 go build -o my-app-mac-m1

Enter fullscreen mode Exit fullscreen mode

  • Building for macOS (Older Intel Macs)
GOOS=darwin GOARCH=amd64 go build -o my-app-mac-intel

Enter fullscreen mode Exit fullscreen mode

Summary Comparison

Feature go run go build Primary Use Local development and testing Production deployment and distribution Creates a File? No (uses a hidden temporary file) Yes (creates a permanent binary executable) Execution Compiles and runs automatically Only compiles; you must run the file manually Speed Slower (compiles every time) Extremely fast (already compiled)

By understanding the difference between these two commands, you will be well on your way to developing and deploying efficient Go applications.

Reference:

원문에서 계속 ↗