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
+8.2k PHP : How to parse ElasticSearch JSON ?
+6.3k Golang : Map within a map example
+33.6k Golang : Proper way to set function argument default value
+6.4k Golang : When to use make or new?
+18.1k Golang : Read binary file into memory
+14.1k Golang : Parsing or breaking down URL
+11.9k Golang : md5 hash of a string
+8.9k Golang : Generate Codabar
+5.7k Golang : Extract unicode string from another unicode string example
+15.4k Golang : Get current time from the Internet time server(ntp) example
+8.2k Android Studio : Import third-party library or package into Gradle Scripts
+10.4k Golang : Flip coin example