### Get Virtual Memory Info in Go Source: https://github.com/shirou/gopsutil/blob/master/README.md Demonstrates how to retrieve and print virtual memory statistics using the gopsutil/mem package. The output can be a formatted string or a JSON representation. ```go package main import ( "fmt" "github.com/shirou/gopsutil/v4/mem" ) func main() { v, _ := mem.VirtualMemory() // almost every return value is a struct fmt.Printf("Total: %v, Free:%v, UsedPercent:%f%%\n", v.Total, v.Free, v.UsedPercent) // convert to JSON. String() is also implemented fmt.Println(v) } ``` -------------------------------- ### Get and Print Disk Usage Statistics Source: https://github.com/shirou/gopsutil/wiki/Disk-Usage-Example This Go snippet retrieves disk partition information and then iterates through each partition to get and print its usage statistics. Ensure the gopsutil/disk package is imported. The check function handles errors by panicking. ```go package main import ( "github.com/shirou/gopsutil/disk" "fmt" "strconv" ) func main() { parts, err := disk.Partitions(false) check(err) var usage []*disk.UsageStat for _, part := range parts { u, err := disk.Usage(part.Mountpoint) check(err) usage = append(usage, u) printUsage(u) } } func printUsage(u *disk.UsageStat) { fmt.Println(u.Path + "\t" + strconv.FormatFloat(u.UsedPercent, 'f', 2, 64) + "% full.") fmt.Println("Total: " + strconv.FormatUint(u.Total/1024/1024/1024, 10) + " GiB") fmt.Println("Free: " + strconv.FormatUint(u.Free /1024/1024/1024, 10) + " GiB") fmt.Println("Used: " + strconv.FormatUint(u.Used /1024/1024/1024, 10) + " GiB") } func check(err error) { if err != nil { panic(err) } } ``` -------------------------------- ### Set Custom System Directory with Context in Go Source: https://github.com/shirou/gopsutil/blob/master/README.md Shows how to use a context to specify alternative locations for system directories, such as /proc, overriding environment variables and default paths. This feature requires gopsutil v3.23.6 or later. ```go ctx := context.WithValue(context.Background(), common.EnvKey, common.EnvMap{common.HostProcEnvKey: "/myproc"}, ) v, err := mem.VirtualMemoryWithContext(ctx) ``` -------------------------------- ### Access Platform-Specific Memory Info in Go Source: https://github.com/shirou/gopsutil/blob/master/README.md Illustrates how to use the `Ex` struct to access platform-specific memory details, such as `VirtualAvail` and `VirtualTotal` on Windows. This requires gopsutil v4.24.5 or later. ```go ex := NewExWindows() v, err := ex.VirtualMemory() if err != nil { panic(err) } fmt.Println(v.VirtualAvail) fmt.Println(v.VirtualTotal) // Output: // 140731958648832 // 140737488224256 ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.