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
+4.9k Golang : Denco multiplexer example
+5.2k Golang : Create new color from command line parameters
+29.7k error: trying to remove "yum", which is protected
+17.1k Golang : Implement getters and setters
+25k Golang : Convert file content into array of bytes
+8.7k Golang : List available AWS regions
+6.2k Nginx : How to block user agent ?
+11.4k Golang : "https://" not allowed in import path
+6.3k Golang : File system scanning
+22.6k Golang : Generate MD5 checksum of a file
+12.1k Golang : Image to ASCII art example
+16.2k Golang : Find smallest number in array