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
+9.4k Golang : Delay or limit HTTP requests example
+7.8k Golang : Find correlation coefficient example
+7k Golang : Metaprogramming example of wrapping a function
+4.3k Golang : ROT32768 (rotate by 0x80) UTF-8 strings example
+22.9k Golang : Convert long hexadecimal with strconv.ParseUint example
+4.6k Golang : Struct field tags and what is their purpose?
+14.2k Golang : Update database with GORM example
+9.9k Golang : Sort and reverse sort a slice of runes
+11.6k Golang : Image to ASCII art example
+20.2k Golang : How to run Golang application such as web server in the background or as daemon?
+22.6k Golang : Call function from another package
+9k Golang : Fix go.exe is not compatible with the version of Windows you're running