Package mmap provides a way to memory-map a file.
go get github.com/hslam/mmap
import "github.com/hslam/mmap"
package main
import (
"fmt"
"github.com/hslam/mmap"
"os"
)
func main() {
name := "mmap"
file, err := os.Create(name)
if err != nil {
panic(err)
}
defer os.Remove(name)
defer file.Close()
str := "Hello world"
file.Truncate(int64(len(str)))
b, err := mmap.Open(int(file.Fd()), 0, len(str), mmap.READ|mmap.WRITE)
if err != nil {
panic(err)
}
defer mmap.Munmap(b)
copy(b, []byte(str))
mmap.Msync(b)
buf := make([]byte, len(str))
if n, err := file.Read(buf); err != nil {
fmt.Println(err)
} else {
fmt.Println(string(buf[:n]))
}
}
Hello world
This package is licensed under a MIT license (Copyright (c) 2020 Meng Huang)
mmap was written by Meng Huang.