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
+32.9k Golang : Integer is between a range
+8.4k Golang : Qt get screen resolution and display on center example
+7.4k Android Studio : Image button and button example
+4.8k Unix/Linux : Get reboot history or check when was the last reboot date
+4.3k JavaScript/JQuery : Redirect page examples
+9.1k Golang : cannot assign type int to value (type uint8) in range error
+3.7k Mac OSX : Get disk partitions' size, type and name
+9.5k Golang : How to unmarshal JSON inner/nested value and assign to specific struct?
+4.1k Golang : Constant and variable names in native language
+12.6k Golang : How to check if a file is hidden?
+7k Golang : Get final or effective URL with Request.URL example
+5.6k Elasticsearch : Shutdown a local node