How npcnixel/genanki-go works
Main components
• API / Fluent-Builder Layer
– Models: models.go defines Model and Field structures and methods to customize CSS and fields.
– Decks & Notes: models.go and package.go include Deck and Note builders with method chaining (AddNote, SetCSS, etc.).
• Media Manager
– media.go: responsible for collecting media (images, audio, video) into memory until packaging.
• Database Writer
– database.go: encapsulates creation of an in-memory or on-disk SQLite DB, schema creation (tables for notes, cards, models, decks), and insertion of all records.
• Package Assembler
– package.go: orchestrates the above components: takes decks, models, media; calls the database writer; and uses Go’s zip utilities to bundle the SQLite DB and media into an .apkg file.
• File System / I/O
– Under the Package Assembler: writes the final zip to disk via WriteToFile.
• Examples / Tests (external)
– examples/: show how consumers use the API.
– tests/: validate correctness of chaining, deck/model/note creation, and end-to-end integration.
Relationships & Data Flow
1) User code (examples) calls API Layer → constructs Models, Decks, Notes
2) API Layer accumulates data in memory (builder objects)
3) User invokes NewPackage(...).AddModel(...).AddMedia(...).WriteToFile(...)
4) Package Assembler calls Database Writer → creates SQLite file (in memory or temp file)
5) Database Writer populates tables for decks, models, notes, cards
6) Package Assembler zips the DB file + media assets into an .apkg ZIP archive
7) File I/O flushes the ZIP to disk
Architectural Patterns & Design Principles
• Fluent/Method-Chaining Builder Pattern (Deck.AddNote(...).AddNote(...)…)
• Separation of Concerns: builders (API) vs persistence (database.go) vs packaging (package.go) vs media (media.go)
• Single Responsibility: each Go file/module owns one area
• Optional Debug Logging: toggled via environment variable or pkg.SetDebug(true), woven through the Database Writer
Technologies & Frameworks
• Go standard library – SQLite driver (via database/sql), archive/zip, os I/O
• No external services – everything runs in‐process
• Tests use Go’s testing package