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.2k Golang : Print UTF-8 fonts on image example
+12.6k Android Studio : Highlight ImageButton when pressed on example
+30.8k error: trying to remove "yum", which is protected
+7.7k Golang : How to execute code at certain day, hour and minute?
+6.7k Golang : Find the longest line of text example
+11k Golang : Simple image viewer with Go-GTK
+5.7k Unix/Linux : How to test user agents blocked successfully ?
+11.1k Google Maps URL parameters configuration
+5.6k Unix/Linux : How to find out the hard disk size?
+11.6k Golang : How to detect a server/machine network interface capabilities?
+31.4k Golang : Example for ECDSA(Elliptic Curve Digital Signature Algorithm) package functions
+25.5k Golang : convert rune to integer value