Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load file metadata with go

Tags:

metadata

go

Does anyone know of a way to read the metadata and or properties of a file using the go language?

like image 219
a sandwhich Avatar asked Jan 16 '11 01:01

a sandwhich


2 Answers

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())
}
like image 153
peterSO Avatar answered Oct 17 '22 11:10

peterSO


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())
}
like image 32
Viseshini Reddy Avatar answered Oct 17 '22 13:10

Viseshini Reddy