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
+12.3k Golang : Set image canvas or background to transparent
+4.8k Linux : Disable and enable IPv4 forwarding
+8.3k Golang : How to generate Code 39 barcode?
+8.4k Golang : Accessing content anonymously with Tor
+18.2k Golang : Compare floating-point numbers
+38.1k Golang : UDP client server read write example
+4.8k PHP : Get client IP address
+9.3k Generate Random number with math/rand in Go
+28k Golang : How to declare kilobyte, megabyte, gigabyte, terabyte and so on?
+19.4k Golang : Convert PNG transparent background image to JPG or JPEG image
+19k Golang : How to force compile or remove object files first before rebuild?
+16.2k Google Chrome : Your connection to website is encrypted with obsolete cryptography