Skip to content
Qxcl edited this page Oct 9, 2016 · 1 revision

Create a slice containing the usage stats for each partition on your system:

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)
  }
}
Clone this wiki locally