Go Standards
Project Conventions
This is a single-file Go HTTP server. All code lives in main.go.
Error Handling
- •Return early on errors with descriptive messages
- •Use
fmt.Errorffor wrapped errors with context - •HTTP handlers: use
http.Error()for client errors, log server errors
go
// Good
if err != nil {
return fmt.Errorf("failed to read file %s: %w", path, err)
}
// Bad
if err != nil {
return err
}
HTTP Handlers
- •Log all requests with timestamp:
fmt.Printf("[%s] %s %s\n", time.Now().Format("15:04:05"), r.Method, r.URL.String()) - •Clean paths with
filepath.Clean()before use - •Set appropriate
Content-Typeheaders - •Return early for special endpoints (asset, mtime)
String Building
- •Use
strings.Builderfor building HTML/text output - •Use
fmt.Sprintffor simple formatting - •Escape user content with
html.EscapeString()
Regex Patterns
- •Compile regexes once at package level for reuse, or inline for clarity in small functions
- •Use
FindStringSubmatchto extract groups - •Use
ReplaceAllStringFuncfor complex replacements
Code Organization
Functions should follow this order:
- •Package-level variables (constants, maps, structs)
- •Helper functions (slugify, replaceEmojis, etc.)
- •
main() - •HTTP handler
- •Render functions by type (renderFile, renderMarkdown, renderJSON, etc.)
- •HTML template builder
Adding New File Format Support
- •Add case in
renderFile()switch statement - •Create
renderXXX(content string) stringfunction - •Add CSS styles in
buildHTML()if needed - •Add format to CLAUDE.md supported formats table
Adding New Markdown Features
Location: renderMarkdown() function (~350 lines)
- •Block elements: Add regex check in main loop before paragraph fallback
- •Inline elements: Add to
processInline()closure - •Preserve order: math/code first (protect from other processing)