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
+9.8k Golang : Find correlation coefficient example
+9.5k Golang : Detect Pascal, Kebab, Screaming Snake and Camel cases
+6.6k Golang : Calculate diameter, circumference, area, sphere surface and volume
+15.4k Golang : Delete certain files in a directory
+24k Golang : Call function from another package
+15.3k Golang : Save(pipe) HTTP response into a file
+5.4k Golang : Get FX sentiment from website example
+5.8k List of Golang XML tutorials
+35.4k Golang : Strip slashes from string example
+4.8k MariaDB/MySQL : Form select statement or search query with Chinese characters
+36.4k Golang : Convert(cast) int64 to string