1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
package mmap_test
import (
"fmt"
"log"
"os"
"github.com/edsrzf/mmap-go"
)
func ExampleMapRegion() {
m, err := mmap.MapRegion(nil, 100, mmap.RDWR, mmap.ANON, 0)
if err != nil {
log.Fatal(err)
}
// m acts as a writable slice of bytes that is not managed by the Go runtime.
fmt.Println(len(m))
// Because the region is not managed by the Go runtime, the Unmap method should
// be called when finished with it to avoid leaking memory.
if err := m.Unmap(); err != nil {
log.Fatal(err)
}
// Output: 100
}
func ExampleMap() {
f, err := os.OpenFile("notes.txt", os.O_RDWR|os.O_CREATE, 0755)
if err != nil {
log.Fatal(err)
}
_, err = f.WriteString("Hello, world")
if err != nil {
log.Fatal(err)
}
// The file must be closed, even after calling Unmap.
defer f.Close()
m, err := mmap.Map(f, mmap.RDWR, 0)
if err != nil {
log.Fatal(err)
}
// m acts as a writable slice of bytes that is a view into the open file, notes.txt.
// It is sized to the file contents automatically.
fmt.Println(string(m))
// The Unmap method should be called when finished with it to avoid leaking memory
// and to ensure that writes are flushed to disk.
if err := m.Unmap(); err != nil {
log.Fatal(err)
}
// Hello, world
}
|