开发者

Load file metadata with go

Does anyone know of a way to read the metadata and o开发者_运维技巧r properties of a file using the go language?


package main

import (
    "fmt"
    "os"
)

func main() {
    fi, err := os.Stat("filename")
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Println(fi.Name(), fi.Size())
}


Use below code, please change your path at place "filename or entire path".

package main

import (
"fmt"
"os"
)

func main() {
    //The file has to be opened first
    f, err := os.Open("filename or entire path")
    // The file descriptor (File*) has to be used to get metadata
    fi, err := f.Stat()
    // The file can be closed
    f.Close()
    if err != nil {
        fmt.Println(err)
        return
    }
    // fi is a fileInfo interface returned by Stat
    fmt.Println(fi.Name(), fi.Size())
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜