create Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/create/ Thu, 16 Apr 2020 20:29:13 +0000 en-US hourly 1 https://wordpress.org/?v=6.8.1 https://i0.wp.com/golangbyexamples.com/wp-content/uploads/2021/05/cropped-go_border-1.png?fit=32%2C32&ssl=1 create Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/create/ 32 32 159787465 Create a directory or folder in Go (Golang) https://vikasboss.github.io/create-directory-folder-golang/ https://vikasboss.github.io/create-directory-folder-golang/#respond Thu, 16 Apr 2020 18:31:08 +0000 https://vikasboss.github.io/?p=2019 Overview os.Mkdir() function can be used to create a directory or folder in go. Below is the signature of the function. It takes in two parameters The first parameter is the named...

The post Create a directory or folder in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
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

The post Create a directory or folder in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/create-directory-folder-golang/feed/ 0 2019