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
+6k Golang : Denco multiplexer example
+12.2k Golang : Save webcamera frames to video file
+11.5k Golang : How to flush a channel before the end of program?
+9.5k Golang : Find the length of big.Int variable example
+9.8k Golang : Populate slice with sequential integers example
+14.7k Golang : Convert(cast) int to float example
+5.3k Golang : Calculate half life decay example
+26k Golang : Daemonizing a simple web server process example
+18.2k Golang : Check if a directory exist or not
+14.1k Golang : How to determine if a year is leap year?
+8.6k Golang : Ackermann function example
+15.3k Golang : Save(pipe) HTTP response into a file