Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get total memory/RAM in GO?

Tags:

go

How can I get the total amount of memory/RAM attached to a system in Go? I want to use native code only if possible. I have found a library that wraps linux sysinfo command. Is there a more elegant way?

like image 646
Usman Ismail Avatar asked May 04 '15 01:05

Usman Ismail


1 Answers

cgo & linux solution

package main

// #include <unistd.h>
import "C"

func main() {
    println(C.sysconf(C._SC_PHYS_PAGES)*C.sysconf(C._SC_PAGE_SIZE), " bytes")
}
like image 191
user5269986 Avatar answered Sep 23 '22 06:09

user5269986