On Mon, 11 Jul 2016 00:07:11 -0700 (PDT)
sphilip...@gmail.com wrote:

> I can allocated memory from OS using syscall.Mmap(-1...) on Unix and 
> VirtualAlloc on Windows. But may be there is standard and compatible
> way to do this?

There are none, and I'm pretty sure there simply can't be any: this
stuff is pretty different between different OSes.  Also note that
you're actually confusing things here a bit: Unix's mmap() is actually
for memory-mapping files, and creating an anonymous region -- not
backed by a file -- is rather a special case.
Conversely, VirtualAlloc() is one of several ways Win32 API offers to
allocating memory.  A Win32 API's cousin to mmap() is MapViewOfFile(),
and it's for files.

On the other hand, if your intent was more about "how do I handle
allocating memory using OS-specific means in a way _my_ code is
portable without fuss" then the answer is: use build tags and
platform-specific source code files.  Here's the good introductory
material to this stuff [1].  Basically, you create a set of files named
like malloc_{GOOS}.go (like malloc_linux.go, malloc_windows.go etc)
and have them declare the same package and export the same API.
The Go building toolchain will pick only the file matching the target
GOOS it build for.

1. 
http://dave.cheney.net/2013/10/12/how-to-use-conditional-compilation-with-the-go-build-tool

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to