跳转至

os.Stat

查看文件属性


示例代码

package main

import (
    "fmt"
    "log/slog"
    "os"
)

func main() {
    fileName := "/tmp/u.log"
    fileInfo, err := os.Stat(fileName)

    if err != nil {
        // 如果是文件不存在
        // os.IsNotExist 函数会返回 true
        if os.IsNotExist(err) {
            slog.Info(fmt.Sprintf("file '%s' not exists\n", fileName))
        }
        slog.Error(fmt.Sprintf("err : %s", err.Error()))
    } else {
        // 能执行到这是说明文件必然存在
        fmt.Printf("file name : %s\n", fileInfo.Name())
        fmt.Printf("file size : %d byte(s) \n", fileInfo.Size())
    }
}