Golang : How to get a user home directory path?
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 (
"fmt"
"os/user"
)
func main() {
//usr, err := user.Current()
usr, err := user.Lookup("dude")
if err != nil {
panic(err)
}
fmt.Println("Dude username is : ", usr.Username)
fmt.Println("Name : ", usr.Name)
fmt.Println("User's home directory is : ", usr.HomeDir)
}
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.
Advertisement
Tutorials
+7.2k Golang : Routes multiplexer routing example with regular expression control
+23.9k Golang : Convert uint value to string type
+12.3k Golang : Verify token from Google Authenticator App
+32.6k Golang : Proper way to set function argument default value
+16.5k Golang : Put UTF8 text on OpenCV video capture image frame
+5.8k Golang : Pat multiplexer routing example
+8k Golang : Detect Pascal, Kebab, Screaming Snake and Camel cases
+28.3k Golang : Get and Set User-Agent examples
+6k Golang : Null and nil value
+22.1k Golang : minus time with Time.Add() or Time.AddDate() functions to calculate past date
+7.6k Golang : Gaussian blur on image and camera video feed examples
+11.7k Generate salted password with OpenSSL example