Go - First Program

Beginner 10/10 Teacher 10/10 Architect 10/10

Minimal Program

  1. Create a folder and module:
    mkdir hello-go && cd hello-go
    go mod init example.com/hello
    
  2. Create main.go:
    package main
    import "fmt"
    func main(){
        fmt.Println("First Go Program")
    }
    
  3. 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 with main.go or the file is named differently.
  • package main missing: executable programs must use package main and define func 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:

  1. Modify the message; re-run quickly with go run ..
  2. Build the binary and run it directly.