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.1k Golang : Underscore string example
+7.5k Findstr command the Grep equivalent for Windows
+17.2k Golang : Simple client server example
+20.5k Golang : Get password from console input without echo or masked
+16.6k Golang : Covert map/slice/array to JSON or XML format
+5.2k Golang : Detect words using using consecutive letters in a given string
+28k Golang : Detect (OS) Operating System
+4.1k Linux/MacOSX : Search and delete files by extension
+10.7k CodeIgniter : How to check if a session exist in PHP?
+10.4k Golang : Fix go.exe is not compatible with the version of Windows you're running
+9.7k Golang : How to tokenize source code with text/scanner package?
+30.1k Golang : Download file example