Getting hertz of linux system using GO

According to man pages on my Debian system, the use of CLK_TCK is now obsolete, although it still works. I can run the getconf command and get the correct answer:

$ getconf CLK_TCK
100

(100 ticks per second is normal for modern Linux systems.)

Here is a non-obsolete way to get the ticks per second from a Linux system using Go (cgo, actually), with the ticks per second going into the variable “hz”:

package main

/*
#include <unistd.h>

long sysconf(int name);

static long gethz(void)
{
        return sysconf(_SC_CLK_TCK);
}
*/
import "C"
import "fmt"

func main() {
        hz := C.gethz()
        fmt.Printf("%v clock ticks per second\n",hz)
}

It’s simply a wrapper around a C program to call the sysconf() C library function. See the sysconf(3) manual page and cgo documentation for details about how I created this.

That was more complicated than it needed to be, but it shows what’s going on better than this more concise way:

package main

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

func main() {
        hz := C.sysconf(C._SC_CLK_TCK)
        fmt.Printf("%v clock ticks per second\n",hz)
}
1 Like