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
+14.2k Golang : Rename directory
+6.6k Golang : Fibonacci number generator examples
+5.9k Apt-get to install and uninstall Golang
+23k Find and replace a character in a string in Go
+18.8k Golang : Execute shell command
+8.1k Golang : Generate Datamatrix barcode
+17.8k Golang : Aligning strings to right, left and center with fill example
+18.2k Golang : Padding data for encryption and un-padding data for decryption
+5.8k Linux/Unix : Commands that you need to be careful about
+15.8k Golang : convert string or integer to big.Int type
+13.1k Golang : reCAPTCHA example
+7.4k Javascript : How to check a browser's Do Not Track status?