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
+5.4k Linux/Unix : Commands that you need to be careful about
+7.3k Golang : HTTP Server Example
+7.4k Golang : Auto-generate reply email with text/template package
+11.6k Golang : Remove or trim extra comma from CSV
+32.6k Golang : All update packages with go get command
+18.6k Golang : Close channel after ticker stopped example
+45k Golang : Marshal and unmarshal json.RawMessage struct example
+5k PHP : Get client IP address
+10.2k Golang : Read until certain character to break for loop
+5k Javascript : How to replace HTML inside <div>?
+21.5k Golang : Set and Get HTTP request headers example
+9.8k Golang : Get local time and equivalent time in different time zone