×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

How to get the system information using syscall in Golang?

Here, we will learn to get the system information using syscall in Golang.
Submitted by IncludeHelp, on November 13, 2021 [Last updated : March 05, 2023]

Getting the system information using syscall in Golang

In the Go programming language, to get the system information using syscall – we use the Sysinfo() function of the syscall package. The Sysinfo() function is used to get the system information. It returns (assigns) the following information in the Sysinfo_t structure and Sysinfo_t contains the following fields:

type Sysinfo_t struct {
    Uptime    int64
    Loads     [3]uint64
    Totalram  uint64
    Freeram   uint64
    Sharedram uint64
    Bufferram uint64
    Totalswap uint64
    Freeswap  uint64
    Procs     uint16
    Pad       uint16
    Pad_cgo_0 [4]byte
    Totalhigh uint64
    Freehigh  uint64
    Unit      uint32
    X_f       [0]byte
    Pad_cgo_1 [4]byte
}

Reference: Sysinfo_t

Syntax

func Sysinfo(info *Sysinfo_t) (err error)

Consider the below example demonstrating how to get the system information using syscall in Golang?

Golang code to get the system information using syscall

package main

import (
	"fmt"
	"syscall"
)

func main() {
	// Creating an object of Sysinfo_t
	sysinfo := syscall.Sysinfo_t{}

	// Calling the function Sysinfo()
	// to get the system information
	err := syscall.Sysinfo(&sysinfo)

	if err != nil {
		fmt.Println("Error:", err)
	}

	// Printing the structure value
	fmt.Println("sysinfo:", sysinfo)

	// Printing the fields values
	fmt.Println("Uptime:", sysinfo.Uptime)
	fmt.Println("Loads:", sysinfo.Loads)
	fmt.Println("Totalram:", sysinfo.Totalram)
	fmt.Println("Freeram:", sysinfo.Freeram)
	fmt.Println("Sharedram:", sysinfo.Sharedram)
	fmt.Println("Bufferram:", sysinfo.Bufferram)
	fmt.Println("Totalswap:", sysinfo.Totalswap)
	fmt.Println("Freeswap:", sysinfo.Freeswap)
	fmt.Println("Procs:", sysinfo.Procs)
	fmt.Println("Pad:", sysinfo.Pad)
	fmt.Println("Pad_cgo_0:", sysinfo.Pad_cgo_0)
	fmt.Println("Totalhigh:", sysinfo.Totalhigh)
	fmt.Println("Freehigh:", sysinfo.Freehigh)
	fmt.Println("Unit:", sysinfo.Unit)
	fmt.Println("X_f:", sysinfo.X_f)
	fmt.Println("Pad_cgo_1:", sysinfo.Pad_cgo_1)
}

Output

sysinfo: {8 [0 0 0] 104857600 74522624 0 0 0 0 15 0 [0 0 0 0] 0 0 1 [] [0 0 0 0]}
Uptime: 8
Loads: [0 0 0]
Totalram: 104857600
Freeram: 74522624
Sharedram: 0
Bufferram: 0
Totalswap: 0
Freeswap: 0
Procs: 15
Pad: 0
Pad_cgo_0: [0 0 0 0]
Totalhigh: 0
Freehigh: 0
Unit: 1
X_f: []
Pad_cgo_1: [0 0 0 0]

Golang syscall Package Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.