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
+10.5k Golang : Find and draw contours with OpenCV example
+20.7k Golang : How to run Golang application such as web server in the background or as daemon?
+9.1k Golang : Get login name from environment and prompt for password
+22.5k Golang : Generate MD5 checksum of a file
+9.8k Golang : Web routing/multiplex example
+28k Golang : How to declare kilobyte, megabyte, gigabyte, terabyte and so on?
+6.1k Golang : Trim everything onward after a word
+24.6k Mac/Linux and Golang : Fix bind: address already in use error
+3.8k Golang : Experimental Jawi programming language
+8.1k Golang : Generate Codabar
+18.2k Golang : Convert seconds to human readable time format example
+15.9k Golang : Get future or past hours, minutes or seconds