On POSIX systems you can use sys.unix.Statfs
.
Example of printing free space in bytes of current working directory:
import "golang.org/x/sys/unix"
import "os"
var stat unix.Statfs_t
wd, err := os.Getwd()
unix.Statfs(wd, &stat)
// Available blocks * size per block = available space in bytes
fmt.Println(stat.Bavail * uint64(stat.Bsize))
For Windows you need to go the syscall route as well. Example (source, updated to match new sys/windows
package):
import "golang.org/x/sys/windows"
h := windows.MustLoadDLL("kernel32.dll")
c := h.MustFindProc("GetDiskFreeSpaceExW")
var freeBytes int64
_, _, err := c.Call(uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(wd))),
uintptr(unsafe.Pointer(&freeBytes)), nil, nil)
Feel free to write a package that provides the functionality cross-platform.
On how to implement something cross-platform, see the build tool help page.