Skip to content

Instantly share code, notes, and snippets.

@caelifer
Last active October 27, 2016 12:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caelifer/b82e8429e464de39edc50cc21df3123c to your computer and use it in GitHub Desktop.
Save caelifer/b82e8429e464de39edc50cc21df3123c to your computer and use it in GitHub Desktop.
Getting page size on POSIX system.
// +build !windows
package main
import (
"fmt"
"log"
)
/*
#include <errno.h>
#include <string.h>
#include <unistd.h>
int getErrno(void) {
return errno;
}
*/
import "C"
func main() {
sz, err := getPageSize()
if err != nil {
log.Fatal(err)
}
fmt.Printf("Page size: %d\n", sz)
}
func getPageSize() (int, error) {
sz := int(C.sysconf(C._SC_PAGESIZE))
errno := C.getErrno()
if errno == C.EINVAL {
return -1, fmt.Errorf("C.sysconf: %s", C.GoString(C.strerror(errno)))
}
return sz, nil
}
@caelifer
Copy link
Author

caelifer commented Oct 27, 2016

This program does not work on Windows. On RHEL 6 Linux and macOS Sierra it outputs:

$ go run page_size.go
Page size: 4096

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment