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
+5.9k Swift : substringWithRange() function example
+21.1k Generate checksum for a file in Go
+12.6k Golang : Gin framework accept query string by post request example
+15.8k Golang : XML to JSON example
+6.9k Golang : How to feed or take banana with Gorilla Web Toolkit Session package
+7.1k Golang : How To Use Panic and Recover
+3k Golang : PGX CopyFrom to insert rows into Postgres database
+12.8k Golang: Pad right or print ending(suffix) zero or spaces in fmt.Printf example
+17.3k Golang : Get path name to current directory or folder
+13.3k Golang : How to filter a map's elements for faster lookup
+9.5k Golang : Fix go.exe is not compatible with the version of Windows you're running