Documentation ¶
Overview ¶
Package mmap provides a way to memory-map a file.
Index ¶
- type File
- func (f *File) At(i int) byte
- func (f *File) Close() error
- func (f *File) Len() int
- func (f *File) Read(p []byte) (int, error)
- func (f *File) ReadAt(p []byte, off int64) (int, error)
- func (f *File) ReadByte() (byte, error)
- func (f *File) Seek(offset int64, whence int) (int64, error)
- func (f *File) Stat() (os.FileInfo, error)
- func (f *File) Sync() error
- func (f *File) Write(p []byte) (int, error)
- func (f *File) WriteAt(p []byte, off int64) (int, error)
- func (f *File) WriteByte(c byte) error
- type Flag
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type File ¶
type File struct {
// contains filtered or unexported fields
}
File reads/writes a memory-mapped file.
func Open ¶
Open memory-maps the named file for reading.
Example ¶
package main import ( "fmt" "log" "github.com/go-mmap/mmap" ) func main() { f, err := mmap.Open("example_mmap_test.go") if err != nil { log.Fatalf("could not mmap file: %+v", err) } defer f.Close() buf := make([]byte, 32) _, err = f.Read(buf) if err != nil { log.Fatalf("could not read into buffer: %+v", err) } fmt.Printf("%s\n", buf[:12]) }
Output: // Copyright
func OpenFile ¶
OpenFile memory-maps the named file for reading/writing, depending on the flag value.
Example (Read) ¶
package main import ( "fmt" "log" "github.com/go-mmap/mmap" ) func main() { f, err := mmap.OpenFile("example_mmap_test.go", mmap.Read) if err != nil { log.Fatalf("could not mmap file: %+v", err) } defer f.Close() buf := make([]byte, 32) _, err = f.ReadAt(buf, 0) if err != nil { log.Fatalf("could not read into buffer: %+v", err) } fmt.Printf("%s\n", buf[:12]) }
Output: // Copyright
Example (Readwrite) ¶
package main import ( "fmt" "io/ioutil" "log" "os" "github.com/go-mmap/mmap" ) func main() { f, err := ioutil.TempFile("", "mmap-") if err != nil { log.Fatalf("could not create tmp file: %+v", err) } defer f.Close() defer os.Remove(f.Name()) _, err = f.Write([]byte("hello world!")) if err != nil { log.Fatalf("could not write data: %+v", err) } err = f.Close() if err != nil { log.Fatalf("could not close file: %+v", err) } raw, err := ioutil.ReadFile(f.Name()) if err != nil { log.Fatalf("could not read back data: %+v", err) } fmt.Printf("%s\n", raw) rw, err := mmap.OpenFile(f.Name(), mmap.Read|mmap.Write) if err != nil { log.Fatalf("could not open mmap file: %+v", err) } defer rw.Close() _, err = rw.Write([]byte("bye!")) if err != nil { log.Fatalf("could not write to mmap file: %+v", err) } raw, err = ioutil.ReadFile(f.Name()) if err != nil { log.Fatalf("could not read back data: %+v", err) } fmt.Printf("%s\n", raw) }
Output: hello world! bye!o world!
func (*File) Stat ¶
Stat returns the FileInfo structure describing file. If there is an error, it will be of type *os.PathError.
Click to show internal directories.
Click to hide internal directories.