Documentation
¶
Overview ¶
Package mmap memory-maps files, cgo-free. The mapped region is a []byte you read and write directly; the OS writes changes back to the file.
f, _ := os.OpenFile("data.bin", os.O_CREATE|os.O_RDWR, 0o644)
f.Truncate(4096) // mmap can't map an empty file
m, _ := mmap.Map(f)
copy(m, []byte("hello"))
m.Unmap()
On Unix this is a thin wrapper over syscall.Mmap (no purego needed); on Windows it binds the file-mapping API through purego.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrUnsupported = errors.New("mmap: not supported on this platform")
ErrUnsupported is returned by Map on a platform with no backend.
Functions ¶
This section is empty.
Types ¶
type MMap ¶
type MMap []byte
MMap is a memory-mapped region. It aliases the file's bytes: writing to the slice writes the file. Don't append to it or reslice past its length, and don't use it after Unmap.
func Map ¶
Map maps the whole of f into memory for reading and writing (shared, so writes reach the file). f must be opened read-write and already sized, e.g. with Truncate; mapping an empty file fails. Unmap the result when done.
yagni: whole-file, read-write, shared. Add an Options{Offset, Length, ReadOnly} only when a real case needs a partial or read-only mapping.