folder Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/folder/ Fri, 17 Apr 2020 17:45:31 +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 folder Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/folder/ 32 32 159787465 Change folder/directory permissions in Go (Golang) https://vikasboss.github.io/change-folder-directory-permissions-go/ https://vikasboss.github.io/change-folder-directory-permissions-go/#respond Fri, 17 Apr 2020 17:45:18 +0000 https://vikasboss.github.io/?p=2036 os.Chmod() function can be used to change the permissions of an existing folder or directory. Below is the signature of the function Code Output:

The post Change folder/directory permissions in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
os.Chmod() function can be used to change the permissions of an existing folder or directory. Below is the signature of the function

func Chmod(name string, mode FileMode) error

Code

package main

import (
    "fmt"
    "log"
    "os"
)

func main() {
    err := os.Mkdir("new", 0755)
    if err != nil {
        log.Fatal(err)
    }
    stats, err := os.Stat("new")
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("Permission Folder Before: %s\n", stats.Mode())
    err = os.Chmod("new", 0700)
    if err != nil {
        log.Fatal(err)
    }
    stats, err = os.Stat("new")
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("Permission Folder After:  %s\n", stats.Mode())
}

Output:

Permission Folder Before: drwxr-xr-x
Permission Folder After:  drwx------

The post Change folder/directory permissions in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/change-folder-directory-permissions-go/feed/ 0 2036
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
Rename file or folder in Go (Golang) https://vikasboss.github.io/rename-file-folder-golang/ https://vikasboss.github.io/rename-file-folder-golang/#respond Wed, 15 Apr 2020 21:15:15 +0000 https://vikasboss.github.io/?p=2004 Overview os.Rename() function can be used to rename a file or folder. Below is the signature of the function. old and new can be fully qualified as well. If the old and...

The post Rename file or folder in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
Overview

os.Rename() function can be used to rename a file or folder. Below is the signature of the function.

func Rename(old, new string) error


old and new can be fully qualified as well. If the old and new path doesn’t lie in the same directory then os.Rename() function behaves the same as moving a file or folder.

Code

Renaming a file

Below is code for renaming a file

package main

import (
    "log"
    "os"
)

func main() {
    //Create a file
    file, err := os.Create("temp.txt")
    if err != nil {
        log.Fatal(err)
    }
    defer file.Close()

    //Change permission so that it can be moved
    err = os.Chmod("temp.txt", 0777)
    if err != nil {
        log.Println(err)
    }


    err = os.Rename("temp.txt", "newTemp.txt")
    if err != nil {
        log.Fatal(err)
    }
}

Output

First, it will create a file named temp.txt in the current working directory. Then it will rename it to newTemp.txt

Renaming a folder

Below is code for renaming a folder

package main

import (
    "log"
    "os"
)

func main() {
    //Create a directory
    err := os.Mkdir("temp", 0755)
    if err != nil {
        log.Fatal(err)
    }
    err = os.Rename("temp", "newTemp")
    if err != nil {
        log.Fatal(err)
    }
}

Output:

First it will create a folder named temp in current working directory. Then it will rename it to newTemp

The post Rename file or folder in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/rename-file-folder-golang/feed/ 0 2004