Project Organization and Style
Project Layout
A small Go project often separates commands, packages, and documentation without needing a heavy framework.
layout
Project layout should make the entry point and reusable code easy to find.
Project Layout
project_layout.go
Replay: real traced execution (multi-file project)
package main
import "fmt"
func main() {
var includeCmd = true
entries := []string{"README.md", "go.mod", "pkg/report"}
if includeCmd {
entries = append(entries, "cmd/report")
}
commandDirs := 0
for _, entry := range entries {
if len(entry) >= 4 && entry[:4] == "cmd/" {
commandDirs++
}
fmt.Println("entry=", entry)
}
fmt.Println("entry_count=", len(entries))
fmt.Println("command_dirs=", commandDirs)
}
package main
import "fmt"
func main() {
var includeCmd = false
entries := []string{"README.md", "go.mod", "pkg/report"}
if includeCmd {
entries = append(entries, "cmd/report")
}
commandDirs := 0
for _, entry := range entries {
if len(entry) >= 4 && entry[:4] == "cmd/" {
commandDirs++
}
fmt.Println("entry=", entry)
}
fmt.Println("entry_count=", len(entries))
fmt.Println("command_dirs=", commandDirs)
}
includeCmd ← true, entries ← []string{"README.md", "go.mod", "pkg/report"}
5func main() {6 var includeCmd→ true = true //@includeCmd=false7 entries→ []string{"README.md", "go.mod", "pkg/report"} := []string{"README.md", "go.mod", "pkg/report"}8 if includeCmd {entries ← []string{"README.md", "go.mod", "pkg/report", "cmd/report"}
7entries := []string{"README.md", "go.mod", "pkg/report"}8if includeCmdtrue {9 entries→ []string{"README.md", "go.mod", "pkg/report", "cmd/report"} = append(entries, "cmd/report")10}commandDirs ← 0
12commandDirs→ 0 := 013for _, entry := range entries {for _, entry := range entries
pass 1 of 412commandDirs := 013for _, entry"README.md" := range entries[]string{"README.md", "go.mod", "pkg/report", "cmd/report"} {14 if len(entry) >= 4 && entry[:4] == "cmd/" {15 commandDirs++16 }17 fmt.Println("entry=", entry"README.md")18}outputentry= README.mdAll 4 passes — pass 1 is the card above pass entrycommandDirs1 "README.md" — 2 "go.mod" — 3 "pkg/report" — 4 "cmd/report" 0 → 1 commandDirs ← 1
13for _, entry := range entries {14 if len(entry"cmd/report") >= 4 && entry[:4] == "cmd/" {15 commandDirs→ 1++16 }fmt.Println("entry=", entry)
16 }17 fmt.Println("entry=", entry"cmd/report")18}outputentry= cmd/reportfmt.Println("entry_count=", len(entries))
20 fmt.Println("entry_count=", len(entries[]string{"README.md", "go.mod", "pkg/report", "cmd/report"}))21 fmt.Println("command_dirs=", commandDirs1)22}outputentry_count= 4 command_dirs= 1
includeCmd ← false, entries ← []string{"README.md", "go.mod", "pkg/report"}
5func main() {6 var includeCmd→ false = false7 entries→ []string{"README.md", "go.mod", "pkg/report"} := []string{"README.md", "go.mod", "pkg/report"}8 if includeCmd {9 entries = append(entries, "cmd/report")10 }1112 commandDirs→ 0 := 013 for _, entry := range entries {for _, entry := range entries
pass 1 of 312commandDirs := 013for _, entry"README.md" := range entries[]string{"README.md", "go.mod", "pkg/report"} {14 if len(entry) >= 4 && entry[:4] == "cmd/" {15 commandDirs++16 }17 fmt.Println("entry=", entry"README.md")18}outputentry= README.mdAll 3 passes — pass 1 is the card above pass entry1 "README.md" 2 "go.mod" 3 "pkg/report" fmt.Println("entry_count=", len(entries))
20 fmt.Println("entry_count=", len(entries[]string{"README.md", "go.mod", "pkg/report"}))21 fmt.Println("command_dirs=", commandDirs0)22}outputentry_count= 3 command_dirs= 0