Build and launch a Go executable
Create a reusable executable
package main
import "fmt"
func main() { fmt.Println("Built program")}Unlike go run, go build -o writes the compiled program to the filename you choose. Run these commands in the directory containing main.go.
Both command sequences should display Built program on the matching operating system. ./demo (or .\demo.exe) explicitly runs a file in the current directory. The Linux commands were tested; the other platform commands are instructional.
Rebuild after editing
If you change the source message to New build, the old executable will still print Built program. Re-run the build command, then launch the resulting binary. Do not confuse a successful launch of an old executable with a successful build of your changes.
Try it locally
Build a binary named demo (demo.exe on Windows), change the message to New build, rebuild and launch it.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Getting Started 07: Build and launch a Go executable
Check the lesson objective before moving on.
Which command writes a named Linux executable?
After editing main.go, what does the previously built executable contain?