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
+6.2k nginx : force all pages to be SSL
+8.6k Golang : How to check variable or object type during runtime?
+5.7k PHP : Convert string to timestamp or datestamp before storing to database(MariaDB/MySQL)
+6k Golang : Use NLP to get sentences for each paragraph example
+17.7k Golang : Upload/Receive file progress indicator
+41.6k Golang : Convert string to array/slice
+14.3k Golang : Check if a file exist or not
+27.9k PHP : Count number of JSON items/objects
+7k Android Studio : Hello World example
+8k Golang : Lock executable to a specific machine with unique hash of the machine
+7.4k Golang : alternative to os.Exit() function
+34.1k Golang : Call a function after some delay(time.Sleep and Tick)