https://github.com/hslam/mmap
Package mmap provides a way to memory-map a file.
https://github.com/hslam/mmap
darwin go golang linux mmap msync munmap unix windows
Last synced: about 1 year ago
JSON representation
Package mmap provides a way to memory-map a file.
- Host: GitHub
- URL: https://github.com/hslam/mmap
- Owner: hslam
- License: mit
- Created: 2020-08-01T15:50:32.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2021-05-21T13:57:49.000Z (about 5 years ago)
- Last Synced: 2025-03-30T11:32:41.681Z (about 1 year ago)
- Topics: darwin, go, golang, linux, mmap, msync, munmap, unix, windows
- Language: Go
- Homepage:
- Size: 35.2 KB
- Stars: 2
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# mmap
[](https://pkg.go.dev/github.com/hslam/mmap)
[](https://github.com/hslam/mmap/actions)
[](https://codecov.io/gh/hslam/mmap)
[](https://goreportcard.com/report/github.com/hslam/mmap)
[](https://github.com/hslam/mmap/blob/master/LICENSE)
Package mmap provides a way to memory-map a file.
## Get started
### Install
```
go get github.com/hslam/mmap
```
### Import
```
import "github.com/hslam/mmap"
```
### Usage
#### Example
```go
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]))
}
}
```
### Output
```
Hello world
```
### License
This package is licensed under a MIT license (Copyright (c) 2020 Meng Huang)
### Author
mmap was written by Meng Huang.