[Golang] Create Directory If Not Exist


Create a directory if it does not exist. Otherwise do nothing.

import "os"

func CreateDirIfNotExist(dir string) {
      if _, err := os.Stat(dir); os.IsNotExist(err) {
              err = os.MkdirAll(dir, 0755)
              if err != nil {
                      panic(err)
              }
      }
}

os.MkdirAll is similar to mkdir -p in shell command, which also creates parent directory if not exists.

Tested on: Go 1.8, Ubuntu Linux 16.10


References:

[1]
[2][Makefile] Create Directory If Not Exist