在 Go 中,os.File 類型提供了一種簡單的方法來檢索由 File 指標處理的檔案的長度。
要確定檔案的長度,您可以利用作業系統提供的Stat 函數package:
fi, err := f.Stat()
fmt.Printf("The file is %d bytes long", fi.Size())
為了說明擷取過程,請考慮以下程式碼片段:
package main import ( "fmt" "os" ) func main() { f, err := os.Open("my_file.txt") if err != nil { fmt.Println("Could not open file:", err) return } fi, err := f.Stat() if err != nil { fmt.Println("Could not obtain file info:", err) return } fmt.Printf("The file is %d bytes long", fi.Size()) }
透過執行此程式碼,您可以擷取並顯示指定檔案「my_file.txt」的長度。
免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。
Copyright© 2022 湘ICP备2022001581号-3