To find the size of a directory in Go, we can use the filepath.Walk
function to walk through all the files and directories within a given directory. While walking through the directory, we can use the os.Stat
function to get information like file size, mode and modification time. By adding up the file sizes, we can determine the size of the entire directory.
main.go459 chars25 lines
In this example, we create a size
variable to hold the total size of the directory. We then use filepath.Walk
to walk through all the files in the directory, ignoring directories themselves. The Size
method on a os.FileInfo
interface returns the size of the file in bytes. We can then add up the sizes of all the files to get the total size of the directory. Finally, we print out the size of the directory in bytes.
gistlibby LogSnag