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
+7.2k Golang : Scanf function weird error in Windows
+16.1k Golang : Find out mime type from bytes in buffer
+7.2k Golang : How to convert strange string to JSON with json.MarshalIndent
+9.8k Golang : Channels and buffered channels examples
+11.9k Golang : convert(cast) string to integer value
+6.1k WARNING: UNPROTECTED PRIVATE KEY FILE! error message
+17.8k Golang : Check if a directory exist or not
+17.4k Golang : Upload/Receive file progress indicator
+34.8k Golang : Upload and download file to/from AWS S3
+4.5k Unix/Linux : How to pipe/save output of a command to file?
+8.8k Golang : io.Reader causing panic: runtime error: invalid memory address or nil pointer dereference