directory Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/directory/ Mon, 04 Oct 2021 18:23:45 +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 directory Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/directory/ 32 32 159787465 Get Current User’s Home Directory in Go (Golang) https://vikasboss.github.io/get-current-user-home-directory-go/ https://vikasboss.github.io/get-current-user-home-directory-go/#respond Fri, 17 Apr 2020 20:31:59 +0000 https://vikasboss.github.io/?p=2049 Overview ‘os/user’ package can be used to get the current user home directory Let’s see a working code Code Output

The post Get Current User’s Home Directory in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
Overview

‘os/user’ package can be used to get the current user home directory

Let’s see a working code

Code

package main

import (
    "fmt"
    "log"
    "os/user"
)

func main() {
    user, err := user.Current()
    if err != nil {
        log.Fatalf(err.Error())
    }
    homeDirectory := user.HomeDir
    fmt.Printf("Home Directory: %s\n", homeDirectory)
}

Output

Home Directory: "some_home_directory"

The post Get Current User’s Home Directory in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/get-current-user-home-directory-go/feed/ 0 2049
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
Get Current Working Directory in Go (Golang) https://vikasboss.github.io/current-working-directory-golang/ https://vikasboss.github.io/current-working-directory-golang/#respond Thu, 16 Jan 2020 04:56:21 +0000 https://vikasboss.github.io/?p=1171 os.Getwd() is used to get the current working directory. Output will similar to pwd command on linux Output: Current Working Direcoty: <will be current working directory on the machine>

The post Get Current Working Directory in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
os.Getwd() is used to get the current working directory. Output will similar to pwd command on linux

package main
import (
    "fmt"
    "log"
    "os"
)

func main() {
    currentWorkingDirectory, err := os.Getwd()
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("Current Wroking Direcoty: %s", currentWorkingDirectory)
}

Output:

Current Working Direcoty: <will be current working directory on the machine>

The post Get Current Working Directory in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/current-working-directory-golang/feed/ 0 1171
Check if file is a directory or not in Go (Golang) https://vikasboss.github.io/check-if-file-is-a-directory-go/ https://vikasboss.github.io/check-if-file-is-a-directory-go/#respond Wed, 15 Jan 2020 15:38:21 +0000 https://vikasboss.github.io/?p=1132 See the below code to know if a file is a file or it is a directory If temp is a file output will be = “temp is a file” If temp...

The post Check if file is a directory or not in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
See the below code to know if a file is a file or it is a directory

  • If temp is a file output will be = “temp is a file”
  • If temp is a directory output will be = “temp is a directory”
package main

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

var (
    fileInfo *os.FileInfo
    err      error
)

func main() {
    info, err := os.Stat("temp")
    if os.IsNotExist(err) {
        log.Fatal("File does not exist.")
    }
    if info.IsDir() {
        fmt.Println("temp is a directory")
    } else {
        fmt.Println("temp is a file")
    }
}

The post Check if file is a directory or not in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/check-if-file-is-a-directory-go/feed/ 0 1132
Check if a file or directory exists in Go (Golang) https://vikasboss.github.io/check-if-file-or-directory-exists-go/ https://vikasboss.github.io/check-if-file-or-directory-exists-go/#respond Wed, 15 Jan 2020 15:37:42 +0000 https://vikasboss.github.io/?p=1131 os.Stat and os.IsNotExist() can be used to check whether a particular file or directory exist or not. File Exists    Folder Exists

The post Check if a file or directory exists in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
os.Stat and os.IsNotExist() can be used to check whether a particular file or directory exist or not.

File Exists   

package main

import (
    "log"
    "os"
)

func main() {
    fileinfo, err := os.Stat("temp.txt")
    if os.IsNotExist(err) {
        log.Fatal("File does not exist.")
    }
    log.Println(fileinfo)
}

Folder Exists

package main

import (
    "log"
    "os"
)

func main() {
    folderInfo, err := os.Stat("temp")
    if os.IsNotExist(err) {
        log.Fatal("Folder does not exist.")
    }
    log.Println(folderInfo)
}

The post Check if a file or directory exists in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/check-if-file-or-directory-exists-go/feed/ 0 1131