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
+3.6k Python : Convert(cast) string to bytes example
+13.8k Golang : read gzipped http response
+3.4k Google : Block or disable caching of your website content
+4.5k Golang : Handling image beyond OpenCV video capture boundary
+8.1k Golang : Channels and buffered channels examples
+26.9k Golang : Interpolating or substituting variables in string examples
+6k Golang : Tell color name with OpenCV example
+3k Golang : Experimental Jawi programming language
+10.3k Golang : How to check if your program is running in a terminal
+17.3k nginx: [emerg] unknown directive "passenger_enabled"
+7.6k Golang : Accessing content anonymously with Tor