Home  >  Article  >  Backend Development  >  How to use the os.Stat function in golang to obtain file information

How to use the os.Stat function in golang to obtain file information

王林
王林Original
2023-11-18 14:02:201192browse

How to use the os.Stat function in golang to obtain file information

How to use the os.Stat function in golang to obtain file information requires specific code examples

As a modern programming language, Golang (also known as Go ) provides developers with a rich standard library, which contains a wealth of file and directory operation functions to meet most programming needs.

Among them, the os package is one of the standard libraries in Golang used to operate operating system functions. This package has many useful functions and methods, one of which is the Stat function. Through this function, developers can obtain detailed information of a specified file.

Below, we will introduce how to use the os.Stat function to obtain file information and give a specific code example.

package main

import (
    "fmt"
    "log"
    "os"
)

func main() {
    filename := "test.txt"
    
    // 调用Stat函数获取文件信息
    fileInfo, err := os.Stat(filename)
    if err != nil {
        log.Fatal(err)
    }
    
    // 输出文件信息
    fmt.Println("文件名:", fileInfo.Name())
    fmt.Println("权限:", fileInfo.Mode())
    fmt.Println("大小:", fileInfo.Size(), "bytes")
    fmt.Println("最后访问时间:", fileInfo.ModTime())
    fmt.Println("是否为目录:", fileInfo.IsDir())
    fmt.Println("所属设备:", fileInfo.Sys())
}

In the above example, we first define a variable filename, which is used to store the file name of the information to be obtained.

Then, we call the os.Stat function and pass in the file name to be opened as an input parameter. This function will return a structure of type FileInfo and an error of type error, which we receive through fileInfo, err := os.Stat(filename) The result returned.

If the os.Stat function call is successful, then fileInfo will contain various information about the file, such as file name, permissions, size, last access time, etc. . We can obtain this information by calling the relevant methods of the fileInfo structure, such as fileInfo.Name() for obtaining the file name, fileInfo.Mode() Used to obtain file permissions, etc.

Finally, we print out each information to view the results.

It should be noted that if the os.Stat function call fails, then err will be assigned a non-nil value. At this time You can print the error message through log.Fatal(err) and exit the program.

Through this simple example, we successfully used the os.Stat function to obtain information about a specific file. This function is very convenient and can help us obtain necessary file details when performing file operations. Of course, Golang's os package also provides more powerful file and directory operation functions, and developers can further explore and learn according to their needs.

The above is the detailed content of How to use the os.Stat function in golang to obtain file information. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn