A portable I/O implement base on mmap for golang.
Use it like bufio
in golang.
NewReader(filePath string)
returns a reader that uses mmap io to read fileRead(p []byte) (n int, err error)
read the file data into the sliceReadAll()(b []byte, err error)
read whole file and format to stringReadLine() (b []byte, err error)
returns one row of the file dataLineNumber() int
get current line numberClose() error
unmap file and release resources
Create a reader from a filepath
reader, err := NewReader(testFile)
if err != nil{
return
}
defer reader.Close()
Read data into slice
b := make([]byte, 20)
if n, err := reader.Read(b); err == nil {
fmt.Printf("read " + strconv.Itoa(n) + " bytes data from file" + " : " + string(b))
}
The output of the above code is as follows
Welcome to mmap io !
You can use ReadAll()
to read it at once
if reader, err := NewReader(testFile); err == nil {
defer reader.Close()
if b, err := reader.ReadAll(); err == nil {
fmt.Printf(string(b))
}
}
The output of the above code is as follows
Welcome to mmap io ! by Yuchao Huang @misterchaos
_ _ _ _
| |__ ___| | | ___ _ __ ___ _ __ ___ __ _ _ __ (_) ___
| '_ \ / _ \ | |/ _ \ | '_ ` _ \| '_ ` _ \ / _` | '_ \| |/ _ \
| | | | __/ | | (_) | | | | | | | | | | | | (_| | |_) | | (_) |
|_| |_|\___|_|_|\___/ |_| |_| |_|_| |_| |_|\__,_| .__/|_|\___/
|_|
- Add file writing and locking support
- Implements
io.Reader
interface to be compatible withbufio
- Implements a mapping of the specified region
The code is open source using GPL3 protocol. If you need to use the code, please follow the relevant provisions of CPL3 protocol.
- Yuchao Huang @misterchaos - Original Author