By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
So far you have run Penny with go run .. That command compiles your code into a temporary program, runs it, and throws the program away. When you are ready to share Penny, you want a real file: one binary that runs on a computer that does not have Go installed and does not have your source code.
go build produces exactly that. The output is a single native executable containing your code and everything it needs from the standard library.
Create a folder called build-demo, put this file in it, and run go mod init build-demo once. The program is small on purpose; the point of this lesson is the build, not the code.
package main
import "fmt"
func main() {
fmt.Println("Penny v1.0")
}The -o flag names the output file. On macOS and Linux the binary has no extension; on Windows it ends in .exe.
# macOS / Linux
go build -o penny .
# Windows PowerShell
go build -o penny.exe .
# Run the binary (not go run). No Go toolchain is involved now.
./penny
# Windows: .\penny.exeLook at the file that appeared. On macOS or Linux it is a few megabytes; on Windows it is a .exe. You can copy that single file to another machine, and it will run there. That is the practical meaning of Go's compiled binaries.
Your binary only runs on the operating system and processor it was built for. GOOS (the operating system) and GOARCH (the processor) choose the target. The build happens on your machine; the result runs on the target.
| You want to run on | GOOS | GOARCH |
|---|---|---|
| Windows 64-bit | windows | amd64 |
| macOS on Apple Silicon | darwin | arm64 |
| macOS on Intel | darwin | amd64 |
| Linux 64-bit | linux | amd64 |
# macOS / Linux: set the variables for one command
GOOS=windows GOARCH=amd64 go build -o penny-windows.exe .
# Windows PowerShell: set them, build, then optionally clear them
$env:GOOS = "linux"
$env:GOARCH = "amd64"
go build -o penny-linux .
Remove-Item Env:GOOS
Remove-Item Env:GOARCHBefore you hand a binary to anyone, run the two commands you have been using all along. They catch the mistakes that a successful build does not.
gofmt -l .
go vet ./...
go test ./...
go build -o penny .A binary without instructions is hard to trust. Add a short README.md next to your code that answers four questions: what Penny does, how to run it, where it stores data, and how to run the tests. That is enough for a friend, a teacher or a future you.
penny.json in the folder you run it from.go test ./....Build the demo binary. Copy it to another folder and run it from there - it should still print Penny v1.0. Delete the binary. Build it again with a different name. Then build the Windows version from the table and confirm the new file appears, even if you cannot run it on your computer.
Key takeaways
go build produces a single native binary; go run does not keep one.
The binary runs without Go being installed on the target machine.
GOOS and GOARCH choose the target operating system and processor.
gofmt -l, go vet, go test and a README are the pre-flight check for sharing.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.