Help detailed information about a file i'm working on

Can someone help me doing this task. I have a problem solving this.
The program should return detailted information about a file in following format:

Information about file :

Size: X in bytes, KB, MB and GB

Is/Is not a directory

Is/Is not a regular file

Has Unix permission bits: -rwxrwxrwx

Is/Is not append only

Is/Is not a device file

Is/Is not a symbolic link

I think the docs on the os package concerning File, FileInfo, etc., cover everything you need: https://golang.org/pkg/os/#FileInfo

1 Like

Ok, here’s a quick solution, it’s just missing sizes other than bytes (I’ll leave that for you). Be sure to read it carefully and check the docs I linked before to understand what it’s doing. Also, replace “filename” accordingly:

package main

import (
	"fmt"
	"os"
)

func main() {

	file, err := os.Open("filename")
	if err != nil {
		fmt.Printf("couldn't open file: %s\n", err)
		os.Exit(1)
	}

	//Get stats
	stats, err := file.Stat()
	if err != nil {
		fmt.Printf("couldn't get stats: %s\n", err)
		os.Exit(1)
	}

	fmt.Printf("Size: %d bytes\n", stats.Size())
	fmt.Printf("Is dir: %t\n", stats.IsDir())
	fmt.Printf("Is regular: %t\n", stats.Mode().IsRegular())
	fmt.Printf("Unix permissions: %s\n", stats.Mode().String())
	fmt.Printf("Append only: %t\n", stats.Mode()&os.ModeAppend != 0)
	fmt.Printf("Is device: %t\n", stats.Mode()&os.ModeDevice != 0)
	fmt.Printf("Is symlink: %t\n", stats.Mode()&os.ModeSymlink != 0)

}
1 Like

Thank you! I appreciate it! :blush:

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.