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.4k Golang : Human readable time elapsed format such as 5 days ago
+9.7k Golang : Sieve of Eratosthenes algorithm
+7.1k Your page has meta tags in the body instead of the head
+3.9k Google : Block or disable caching of your website content
+14.1k Golang : Delete certain files in a directory
+12.4k Golang : How to get year, month and day?
+14.8k Golang : Get IP addresses of a domain name
+10.1k CodeIgniter : How to check if a session exist in PHP?
+14.2k Golang : invalid character ',' looking for beginning of value
+44.1k Golang : Marshal and unmarshal json.RawMessage struct example
+16.3k Golang : How to log each HTTP request to your web server?
+39.6k Golang : How to check if a string contains another sub-string?