Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Go 磁盘使用信息_Go_Archlinux - Fatal编程技术网

Go 磁盘使用信息

Go 磁盘使用信息,go,archlinux,Go,Archlinux,我首先编写了一个shbash()脚本,现在在Go中编写了一个类似的脚本。Go程序显示我从服务器目录装载错误 我使用: 内核:5.3.11-1-MANJARO x86_64位:64编译器:gcc v:9.2.0桌面:Xfce 4.14.1 tk:Gtk 3.24.12 信息:xfce4面板wm:xfwm4 dm:LightDM 1.30.0发行版:Manjaro Linux 以下是围棋程序: //pacman -S go //go get -u golang.org/x/sys/unix pac

我首先编写了一个shbash()脚本,现在在Go中编写了一个类似的脚本。Go程序显示我从服务器目录装载错误

我使用:

内核:5.3.11-1-MANJARO x86_64位:64编译器:gcc v:9.2.0桌面:Xfce 4.14.1 tk:Gtk 3.24.12 信息:xfce4面板wm:xfwm4 dm:LightDM 1.30.0发行版:Manjaro Linux

以下是围棋程序:

//pacman -S go
//go get -u golang.org/x/sys/unix

package main

import (
    "fmt"
    syscall "golang.org/x/sys/unix"
)

type DiskStatus struct {
    All   uint64 `json:"all"`
    Used  uint64 `json:"used"`
    Free  uint64 `json:"free"`
    Avail uint64 `json:"avail"`
}

// disk usage of path/disk
func DiskUsage(path string) (disk DiskStatus) {
    fs := syscall.Statfs_t{}
    err := syscall.Statfs(path, &fs)
    if err != nil {
        return
    }
    disk.All = fs.Blocks * uint64(fs.Bsize)
    disk.Avail = fs.Bavail * uint64(fs.Bsize)
    disk.Free = fs.Bfree * uint64(fs.Bsize)
    disk.Used = disk.All - disk.Free
    return
}

const (
    B  = 1
    KB = 1024 * B
    MB = 1024 * KB
    GB = 1024 * MB
)

func main() {
    path := "/"
    funcName("/mnt/ix4")
    funcName("/mnt/ix4300d")
    funcName(path)
    //funcName("192.168.178.42/share")
}

func funcName(path string) {
    disk := DiskUsage(path)
    fmt.Println("")
    fmt.Println(path, ":")
    fmt.Printf("All: %.2f GB\n", float64(disk.All)/float64(GB))
    fmt.Printf("Avail: %.2f GB\n", float64(disk.Avail)/float64(GB))
    fmt.Printf("Used: %.2f GB\n", float64(disk.Used)/float64(GB))
}
/mnt/ix4300d的正确值 是

Golang版本为我提供了/mnt/ix4300d 是


Go给出相同的值​​作为我的系统分区(“/”)。

你确定
syscall.Statfs(path,&fs)
没有返回错误吗?@zerkms错误从未发生在我身上。(与显式错误相同)好,这只是一个一般性的建议,不要编写忽略错误的代码。在我的机器上,工作正常:/boot/efi:All:0.50 GB Avail:0.49 GB Used:0.01 GB/:All:467.02 GB Avail:305.26 GB Used:137.97GB@MaximPanfilov这里它是通过
mount-tfs装载的afp://ix4-300d.local:share /mnt/ix4300d
。它是一个旧的NAS(ix4-200d)
5216716992 = blocks_avail
5216 GB free
9%used 192.168.178.42:/share
/mnt/ix4300d :
All: 862.34 GB
Avail: 180.74 GB
Used: 637.73 GB