How to Create Nested Directories in Go


Using os.MkdirAll Function

The os.MkdirAll() function creates a directory named path, along with any necessary parents, and returns nil, or else returns an error.

See the following example:

package main

import (

func main() {

  if err := os.MkdirAll("path/subdir", 0750); err != nil && !os.IsExist(err) {
    // panic()

  // create file

If path is already a directory, MkdirAll does nothing and returns nil.

Related Tags