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
+23k Golang : Print out struct values in string format
+6.1k Golang : How to get capacity of a slice or array?
+9.4k Golang : Changing a RGBA image number of channels with OpenCV
+5.4k Clean up Visual Studio For Mac installation failed disk full problem
+10.7k Golang : Get UDP client IP address and differentiate clients by port number
+11.5k Golang : Find age or leap age from date of birth example
+13.2k Golang : Linear algebra and matrix calculation example
+15.3k Golang : ROT47 (Caesar cipher by 47 characters) example
+9.9k Golang : Edge detection with Sobel method
+12k Golang : Decompress zlib file example
+18.3k Golang : Aligning strings to right, left and center with fill example
+7.4k Golang : How to stop user from directly running an executable file?