golang:如何检查写入是否因磁盘不足而失败 space

golang: how to check if a write fails due to insufficient disk space

如果 go 程序 return 错误 Write(),它 return 是这样的:

write failed(*fs.PathError): write mnt/test.dat: no space left on device

除了 'no space left on device' 的字符串匹配之外,是否还有 PathError 是由于磁盘不足 space?

注意:我正在使用 Linux,关心它在 Windows 上的工作方式。

Is there anyway other than string matching for 'no space left on device', to know that a PathError is due to insufficient disk space?

没有

是的,它可能对您编译的 OS 敏感,但您可以检查错误是否包含 syscall.ENOSPC

_, err := file.Write(stuff)
if err != nil {
    if errors.Is(err, syscall.ENOSPC) {
        // There was no space left on the device
        return "get more space"
    }
    // something else is wrong
}