Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

mmap go

By: a guest on Feb 6th, 2012  |  syntax: Go  |  size: 0.48 KB  |  hits: 229  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. package main
  2.  
  3. import (
  4.         "fmt"
  5.         "os"
  6.         "syscall"
  7.         "unsafe"
  8.         )
  9.  
  10. func main() {
  11.         n := 1000
  12.         t := int(unsafe.Sizeof(0)) * n
  13.  
  14.         map_file, _ := os.Create("/tmp/test.dat")
  15.         _, _ = map_file.Seek(int64(t - 1), 0)
  16.         map_file.Write([]byte(" "))
  17.  
  18.         mmap, _ := syscall.Mmap(map_file.Fd(), 0, int(t), syscall.PROT_READ | syscall.PROT_WRITE, syscall.MAP_SHARED)
  19.         map_array := (*[1000]int)(unsafe.Pointer(&mmap))
  20.  
  21.         for i:=0; i < n; i++ {
  22.                 map_array[i] = i*i
  23.         }
  24.  
  25.         fmt.Println(*map_array)
  26. }