Getting file information
If you need to discover basic information about the accessed file, Go's standard library provides a way on how you can do this. This recipe shows how you can access this information.
How to do it...
- Open the console and create the folder
chapter06/recipe01
. - Navigate to the directory.
- Create the sample
test.file
with the contentThis is test file
. - Create the
fileinfo.go
file with the following content:
package main import ( "fmt" "os" ) func main() { f, err := os.Open("test.file") if err != nil { panic(err) } fi, err := f.Stat() if err != nil { panic(err) } fmt.Printf("File name: %v\n", fi.Name()) fmt.Printf("Is Directory: %t\n", fi.IsDir()) fmt.Printf("Size: %d\n", fi.Size()) fmt.Printf("Mode: %v\n", fi.Mode()) }
- Execute the code by running
go run fileinfo.go
in the main Terminal. - You...