See filepath.Walk (https://golang.org/pkg/path/filepath/#Walk)

If you wish to recurse with ioutil.ReadDir or File.Readdir 
(https://golang.org/pkg/os/#File.Readdir), you have to do it manually.

Nested structs -- it depends what you are doing.

I would simplify it to:

type Stat struct {
FileCount int64
DirCount  int64
Bytes     int64
}

When I want to keep per directory, then I would use:

type Info struct {
Path    string
Total   Stat
Current Stat
Dirs    []Info
}

type Stat struct {
FileCount int64
DirCount  int64
Bytes     int64
}

Btw. have you done the Go tour? (https://tour.golang.org/moretypes/1)

On Monday, 5 June 2017 08:38:59 UTC+3, Giovanni Francesco wrote:
>
> Hi there,
>
> I'm very new to Golang, and quite honestly haven't coded in a long time so 
> bear with me if this is an very basic question.
>
> I'm trying to build a basic backup program, which will take a path 
> (string) and then calculate the total "backup size" of the entire backup 
> (and all the child folders, etc).
>
> I found out by starting to write the basics of the program that 
> ioutil.ReadDir does not recursively go deeper into child folders so the 
> code I will show you is only calculating the size for any files in the root 
> only.
>
> I already have an idea on how to fix it but I have a much deeper question: 
> would using nested structs be a good idea?
>
> I'm basically defining the program like so:
>
> type Backup struct {
> Files
> Directories
> byteSize int64
> humanSize string
> }
>
> type Directories struct {
> name string
> containsBytes float64
> constainsFiles int64
> }
>
> type Files struct {
> parent Directories
> name string
> byteSize int64
> humanSize string
> }
>
> Do I need to make "Directories" and "Files" be a slice / map or something 
> for my purposes? I am a little confused here and I don't know how I could 
> make my struct (which holds multiple values) be an array/slice, how would 
> that work?
>
> What if I wanted to "search" for a file or folder later on my program? 
>
> My code that works for top-level folder calculation: 
> https://gist.github.com/TheLinuxGuy/2dd8ed958d14c491bf86e2f5f3d4688d
>
> Thanks!
> Giovanni
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to