Go code to monitor RAM memory
go run main.go
Let's break down the code step by step:
package main
import (
"fmt"
"time"
"github.com/shirou/gopsutil/v3/mem"
)
func main() {
for {
memInfo, err := mem.VirtualMemory()
if err != nil {
fmt.Println("Error retrieving memory information:", err)
return
}
fmt.Printf("Memory Usage:\n")
fmt.Printf(" Total: %v MB\n", memInfo.Total>>20)
fmt.Printf(" Available: %v MB\n", memInfo.Available>>20)
fmt.Printf(" Used: %v MB\n", memInfo.Used>>20)
time.Sleep(1 * time.Second)
}
}
Now, let's break down the code:
-
package main
: Indicates that this file belongs to the main package. In Go, an executable program starts with themain
package. -
Imports:
fmt
: Provides formatting functions similar toprintf
in C.time
: Provides functions for measuring and manipulating time.github.com/shirou/gopsutil/v3/mem
: An external library that provides information about system memory usage.
-
func main()
: The main function that gets executed when the program is started. -
for {...}
: An infinite loop. -
memInfo, err := mem.VirtualMemory()
: Retrieves information about memory usage using thegopsutil
library.mem.VirtualMemory()
returns a*mem.VirtualMemoryInfo
object that contains information about the system's virtual memory. -
if err != nil {...}
: Checks for an error when retrieving memory information. If an error occurs, it prints an error message and exits the program. -
Printing Memory Information:
fmt.Printf("Memory Usage:\n")
: Prints a header.fmt.Printf(" Total: %v MB\n", memInfo.Total>>20)
: Prints the total amount of memory in megabytes.fmt.Printf(" Available: %v MB\n", memInfo.Available>>20)
: Prints the amount of available memory in megabytes.fmt.Printf(" Used: %v MB\n", memInfo.Used>>20)
: Prints the amount of used memory in megabytes.
-
time.Sleep(1 * time.Second)
: Makes the program wait for 1 second before continuing to the next iteration of the loop. This sets the monitoring update frequency.
This program essentially retrieves and displays information about system memory usage in an infinite loop with a 1-second update frequency. The output format is quite simple, but you can customize it as per your needs. Furthermore, this is a starting point; you can expand and enhance the program based on what you want to achieve with it.