//go:build windows package logic import ( "math" "os" "golang.org/x/sys/windows" "easygoadmin/api/v1" ) // diskUsage 读取工作目录所在磁盘分区容量(Windows) func diskUsage() []v1.ServerDiskInfo { wd, err := os.Getwd() if err != nil || len(wd) < 3 { return nil } root := wd[:3] // 形如 "E:\" handle, err := windows.UTF16PtrFromString(root) if err != nil { return nil } var freeBytes, totalBytes, totalFreeBytes uint64 if err = windows.GetDiskFreeSpaceEx(handle, &freeBytes, &totalBytes, &totalFreeBytes); err != nil { return nil } total := float64(totalBytes) / 1073741824 free := float64(freeBytes) / 1073741824 usedPercent := 0.0 if total > 0 { usedPercent = math.Round((total-free)/total*10000) / 100 } return []v1.ServerDiskInfo{{ Path: root, TotalGB: math.Round(total*100) / 100, FreeGB: math.Round(free*100) / 100, UsedPercent: usedPercent, }} }