Welcome To Golang By Example

Create a directory or folder in Go (Golang)

Table of Contents

Overview

os.Mkdir() function can be used to create a directory or folder in go.

Below is the signature of the function.

func Mkdir(name string, perm FileMode)

It takes in two parameters

Code

package main

import (
    "log"
    "os"
)

func main() {
    //Create a folder/directory at a full qualified path
    err := os.Mkdir("/Users/temp", 0755)
    if err != nil {
        log.Fatal(err)
    }

    //Create a folder/directory at a full qualified path
    err = os.Mkdir("temp", 0755)
    if err != nil {
        log.Fatal(err)
    }
}

Output

It will create a directory temp at location /Users location and at the current working directory location