Does anyone know of a way to read the metadata and or 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())
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With