Welcome To Golang By Example

Menu
  • Home
  • Blog
  • Contact Us
  • Support this website
Menu

Create a directory or folder in Go (Golang)

Posted on April 16, 2020April 16, 2020 by admin

Table of Contents

  • Overview
  • Code

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

  • The first parameter is the named directory. If the named directory is a fully qualified path it will create a directory at that path. If not it will create a directory with respect to current working directory
  • The second parameter specifies the permission bits. The folder or directory is created with this permission bits

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

  • create
  • directory
  • folder
  • go
  • golang
  • Follow @golangbyexample

    Popular Articles

    Golang Comprehensive Tutorial Series

    All Design Patterns in Go (Golang)

    Slice in golang

    Variables in Go (Golang) – Complete Guide

    OOP: Inheritance in GOLANG complete guide

    Using Context Package in GO (Golang) – Complete Guide

    All data types in Golang with examples

    Understanding time and date in Go (Golang) – Complete Guide

    ©2025 Welcome To Golang By Example | Design: Newspaperly WordPress Theme