首頁 > 後端開發 > Golang > 如何使用 Go 在 Windows 中取得檔案的建立日期?

如何使用 Go 在 Windows 中取得檔案的建立日期?

Susan Sarandon
發布: 2024-11-29 19:26:11
原創
247 人瀏覽過

How to Get a File's Creation Date in Windows Using Go?

使用Go 在Windows 中擷取檔案建立日期

問題:如何存取檔案的建立日期在Windows中使用Go程式

答案: 要使用Go 檢索Windows 中檔案的建立日期,您可以利用 os.File 的 Sys 方法來取得系統特定的資料結構,具體來說,適用於 Windows 系統的 Win32FileAttributeData。 Win32FileAttributeData 結構包含 CreationTime 等字段,它表示檔案的建立時間戳記。

示範此方法:

import (
    "os"
    "syscall"
    "time"
)

func GetFileCreationTime(path string) (time.Time, error) {
    fileInfo, err := os.Stat(path)
    if err != nil {
        return time.Time{}, err
    }

    // Verify that the file information is compatible with Windows
    sysFileInfo, isWindows := fileInfo.Sys().(*syscall.Win32FileAttributeData)
    if !isWindows {
        return time.Time{}, nil
    }

    // Convert the nanosecond timestamp to a `time.Time` object
    return time.Unix(0, sysFileInfo.CreationTime.Nanoseconds()), nil
}
登入後複製

範例用法:

creationTime, err := GetFileCreationTime("./myfile.txt")
if err != nil {
    // Handle file opening error
}
fmt.Println("File creation time:", creationTime)
登入後複製

利用此方法,您可以使用以下指令輕鬆擷取Windows 系統中檔案的建立日期Go 程式語言。

以上是如何使用 Go 在 Windows 中取得檔案的建立日期?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板