Go - First Program
Beginner 10/10
Teacher 10/10
Architect 10/10
Minimal Program
- Create a folder and module:
mkdir hello-go && cd hello-go go mod init example.com/hello
- Create
main.go
:package main import "fmt" func main(){ fmt.Println("First Go Program") }
- Run it:
go run . # Expected output: # First Go Program
Windows tip: The commands above also work in PowerShell. Use
mkdir
and cd
the same way. Run go
commands from a new PowerShell window after installing Go.
Run vs Build
go run .
compiles to a temp binary and runs it.go build
creates a binary in the current directory.
Common errors
go: no go files listed
: you’re not in the folder withmain.go
or the file is named differently.package main
missing: executable programs must usepackage main
and definefunc main()
.
Practice
- Print your name and today’s date on separate lines.
- Change the message and rebuild with
go build
, then run the binary.
Try:
- Modify the message; re-run quickly with
go run .
. - Build the binary and run it directly.