Welcome To Golang By Example

Get Current User’s Home Directory in Go (Golang)

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"