Welcome To Golang By Example

Menu
  • Home
  • Blog
Menu

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

Posted on April 17, 2023April 17, 2023 by admin

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"
  • directory
  • go
  • golang
  • home
  • Popular Articles

    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

    Function in Golang- Complete Guide

    All data types in Golang with examples

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

    ©2023 Welcome To Golang By Example | Design: Web XP