szaydel
7/25/2016 - 12:20 PM

Golang RemoveAll Test

Golang RemoveAll Test

package main

import (
  "fmt"
  "os"
)

const (
  LOOPS = 10000
)

func main() {
  var f *os.File
  var path string = "/tmp/tdir"
  var args []string = os.Args
   if len(args) > 1 {
     path = args[1]
   }
   err := os.Mkdir(path, 0700)
   if err != nil {
     if os.IsExist(err) {
       fmt.Printf("Directory %s exists.\n", path)
     } else {
       fmt.Printf("Failed to create directory: %v\n", err)
       os.Exit(1)
     }
   }
   f, err = os.Create(fmt.Sprintf("%s/%s", path, "testfile.txt"))
   for i := 0; i < LOOPS; i++ {
    n, err := f.WriteString("This is junk data to fill file with something!\n")
    if n == 0 {
      fmt.Printf("Prematurely terminated loop: %v\n", err)
      break
    }
   }
  // At this point we should expect that each time we run this, some number
  // of KBs will be added to used space of the filesystem.
  err = os.RemoveAll(path)
  if err != nil {
    fmt.Printf("os.RemoveAll(path): %v\n", err)
  }
}