Golang : How to get a user home directory path?

Tags : golang home-directory os-user lookup-username

Problem :

You need to get a user's home directory path in a Unix/Linux machine for storing or processing customized data.

Solution :

Use the os/user.Current() or os/user.Lookup() function and the HomeDir() method.

For example :

 package main

 import (

 func main() {

 //usr, err := user.Current()

 usr, err := user.Lookup("dude")

 if err != nil {

 fmt.Println("Dude username is : ", usr.Username)

 fmt.Println("Name : ", usr.Name)

 fmt.Println("User's home directory is : ", usr.HomeDir)

Tags : golang home-directory os-user lookup-username

By Adam Ng

IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.