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
+5k Javascript : How to get width and height of a div?
+13k Golang : Convert int(year) to time.Time type
+7.6k Golang : Check to see if *File is a file or directory
+11.4k Golang : Proper way to test CIDR membership of an IP 4 or 6 address example
+5.6k Golang : What is StructTag and how to get StructTag's value?
+5k Unix/Linux : secure copying between servers with SCP command examples
+18.6k Golang : How to get hour, minute, second from time?
+8.5k Golang : Check if integer is power of four example
+5.1k Python : Find out the variable type and determine the type with simple test
+13.6k Golang : Generate Code128 barcode
+8.1k Golang : How to feed or take banana with Gorilla Web Toolkit Session package
+6.5k Javascript : Generate random key with specific length