geektutu/7days-golang · error
invalid magic number
Error message
invalid magic number
What it means
The bolt database file header does not start with the expected magic number constant, so meta.validate() in gee-bolt/day1-pages rejects the page as not a valid database file. The magic number is a fixed marker written when the DB is initialized; mismatch means the buffer is not a bolt file or is corrupted.
Source
Thrown at gee-bolt/day1-pages/meta.go:27
// Represent a marker value to indicate that a file is a gee-bolt DB
const magic uint32 = 0xED0CDAED
type meta struct {
magic uint32
pageSize uint32
pgid uint64
checksum uint64
}
func (m *meta) sum64() uint64 {
var h = fnv.New64a()
_, _ = h.Write((*[unsafe.Offsetof(meta{}.checksum)]byte)(unsafe.Pointer(m))[:])
return h.Sum64()
}
func (m *meta) validate() error {
if m.magic != magic {
return errors.New("invalid magic number")
}
if m.checksum != m.sum64() {
return errors.New("invalid checksum")
}
return nil
}
View on GitHub (pinned to cf36443821)
Solutions
- Verify the file path points to a file previously created by this bolt implementation
- Delete or archive the corrupt/foreign file and re-create the database from scratch
- Check you are reading the meta page at the correct offset (page 0 for the first meta)
- Compare the file's first bytes against the expected magic constant with a hex dump to confirm corruption
Example fix
// before
f, _ := os.OpenFile("notes.txt", os.O_RDWR, 0600)
db := bolt.Open(f) // -> invalid magic number
// after
f, _ := os.OpenFile("notes.bolt", os.O_RDWR|os.O_CREATE, 0600)
db := bolt.Open(f) // freshly created file has magic written Defensive patterns
Strategy: validation
Validate before calling
func looksLikeBoltFile(path string) bool {
b, err := os.ReadFile(path)
if err != nil || len(b) < 4 { return false }
return bytes.Equal(b[:4], magicBytes()) // compare first bytes to magic
} Type guard
func isValidBoltMeta(m *meta) bool { return m != nil && m.magic == magic } Prevention
- Always create DB files via the library's open/create path, never os.Create alone
- Validate file existence and format before opening in tooling
- Hex-dump first bytes when diagnosing file issues
- Keep DB files out of editors/sync tools that may rewrite them
When it happens
Trigger: Opening a file that is not a bolt database, reading an empty/zero-filled page, byte-swapped or truncated file, or reading a buffer from the wrong offset so the magic field lands on garbage.
Common situations: Pointing the DB at a log/text file or a different format's file; a crashed first write left an all-zero file; manually copying/editing the file; opening a file produced by an incompatible version.
Related errors
AI-assisted analysis of geektutu/7days-golang@cf36443821 (2026-09-03).
Data as JSON: /api/errors/9b01f51211a88fb6.
Report an issue: GitHub.