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
+11.6k Golang : Handle API query by curl with Gorilla Queries example
+22.8k Generate checksum for a file in Go
+15.4k nginx: [emerg] unknown directive "ssl"
+8.9k Golang : Gorilla web tool kit schema example
+5.4k Golang : Get FX sentiment from website example
+12.6k Golang : Arithmetic operation with numerical slices or arrays example
+17.2k Golang : Fix cannot convert buffer (type *bytes.Buffer) to type string error
+5.3k Golang : Issue HTTP commands to server and port example
+5.8k List of Golang XML tutorials
+21.6k Curl usage examples with Golang
+11k Golang : Removes punctuation or defined delimiter from the user's input
+4.7k JavaScript : Rounding number to decimal formats to display currency