Skip to content

Instantly share code, notes, and snippets.

@makeittotop
Last active August 29, 2015 14:17
Show Gist options
  • Select an option

  • Save makeittotop/a0428e96cbad908079d1 to your computer and use it in GitHub Desktop.

Select an option

Save makeittotop/a0428e96cbad908079d1 to your computer and use it in GitHub Desktop.
Go script to calculate free space on a machine
package main
import (
"syscall"
"os"
"fmt"
"math"
)
func main() {
var stat syscall.Statfs_t
wd, err := os.Getwd()
if err != nil {
panic(err)
}
syscall.Statfs(wd, &stat)
// Available blocks * size per block = available space in bytes / 1024 ^ 3 to convert into gb.
//fmt.Printf("Free: %d GB\n", stat.Bavail * uint64(stat.Bsize) / (1024 * 1024 * 1024))
fmt.Printf("Free: %f GB\n", float64(stat.Bavail * uint64(stat.Bsize)) / (math.Pow(1024, 3)))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment