Project Organization and Style
Project Layout
A small Go project often separates commands, packages, and documentation without needing a heavy framework.
Project Layout
project_layout.go
package main
import "fmt"
func main() {
var includeCmd =
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 =
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)
}
layout
Project layout should make the entry point and reusable code easy to find.