Go programs need to be turned into machine code before they can run. This process is called compilation. Understanding how Go compiles and runs helps you write and run your programs smoothly.
Go compilation and execution flow
go build [options] [packages] go run [options] [files]
go build compiles your code into an executable file.
go run compiles and runs your code in one step without creating a separate executable file.
main.go into an executable file named main (or main.exe on Windows).go build main.go
main.go immediately without saving an executable file.go run main.go
go build ./...
This simple Go program prints a message. You can compile it with go build and run the executable, or run it directly with go run.
package main import "fmt" func main() { fmt.Println("Hello, Go compilation and execution!") }
When you use go run, Go compiles your code to a temporary executable, runs it, then deletes the executable.
go build creates a permanent executable you can run anytime without recompiling.
Compilation checks your code for errors before creating the executable.
Go code must be compiled before it runs.
go build creates an executable file.
go run compiles and runs code in one step without saving an executable.
Practice
go build command do in Go programming?Solution
Step 1: Understand the purpose of
Thego buildgo buildcommand compiles Go source code into an executable binary file.Step 2: Differentiate from other commands
Unlikego run,go builddoes not run the program immediately but creates a file you can run later.Final Answer:
It compiles the code and creates an executable file. -> Option AQuick Check:
go buildcreates executable [OK]
- Confusing
go buildwithgo run - Thinking
go buildruns the program immediately - Believing
go buildonly checks syntax
Solution
Step 1: Identify the command that compiles and runs
go runcompiles the code and runs it immediately without creating an executable file.Step 2: Check other options
go buildonly compiles;go executeandgo compileare not valid Go commands.Final Answer:
go run main.go -> Option CQuick Check:
go runcompiles and runs [OK]
go run to compile and run instantly [OK]- Using
go buildto run directly - Typing invalid commands like
go execute - Confusing compile-only and run commands
go run main.go on a valid Go source file?Solution
Step 1: Understand
go runbehaviorgo runcompiles the source code in memory and runs it immediately without saving an executable file.Step 2: Eliminate incorrect options
It compiles the code, creates an executable file, and runs it. is wrong because no executable file is saved. It only checks the code for errors but does not run it. is wrong because it runs the program. It deletes the source file after running. is incorrect as source files are never deleted.Final Answer:
It compiles the code and runs it without creating an executable file. -> Option DQuick Check:
go runcompiles & runs, no file saved [OK]
go run to skip saving executable [OK]- Assuming
go runsaves an executable - Thinking
go runonly checks syntax - Believing source files get deleted
go build on your Go program, but no executable file appears. What is the most likely reason?Solution
Step 1: Understand
If the source code has syntax errors,go buildbehavior on errorsgo buildwill fail and not create an executable file.Step 2: Check other options
go builddoes not run the program, executables are created in the current directory by default, and Go does not delete executables automatically.Final Answer:
The source file has syntax errors preventing compilation. -> Option BQuick Check:
Syntax errors block build, no executable made [OK]
- Expecting
go buildto run the program - Looking in wrong directories without checking errors
- Assuming Go deletes executables automatically
Solution
Step 1: Identify the command for quick testing
go runcompiles and runs the program immediately without creating an executable file, perfect for quick tests.Step 2: Understand why other commands don't fit
go buildcreates an executable but does not run it.go installcompiles and installs packages, not for quick runs.go testruns tests, not general programs.Final Answer:
Usego runbecause it compiles and runs the program without saving an executable. -> Option AQuick Check:
Quick test =go run[OK]
go run for quick compile-and-run tests [OK]- Using
go buildexpecting immediate run - Confusing
go installwith running code - Trying
go testfor normal program execution
Ready to prove it?
Pass the test and this pattern moves unknown
