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
+7.6k Golang : Mapping Iban to Dunging alphabets
+6k Golang : Convert Chinese UTF8 characters to Pin Yin
+9.6k PHP : Get coordinates latitude/longitude from string
+7.8k Golang : Trim everything onward after a word
+4.6k MariaDB/MySQL : Form select statement or search query with Chinese characters
+9.4k Golang : Changing a RGBA image number of channels with OpenCV
+7.5k Golang : Set horizontal, vertical scroll bars policies and disable interaction on Qt image
+4.8k Golang : A program that contain another program and executes it during run-time
+22.4k Golang : Convert Unix timestamp to UTC timestamp
+18.7k Unmarshal/Load CSV record into struct in Go
+13.8k Golang : Human readable time elapsed format such as 5 days ago