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
+9.7k PHP : Get coordinates latitude/longitude from string
+26.4k Golang : Convert(cast) string to uint8 type and back to string
+4.3k Javascript : How to show different content with noscript?
+14.2k Javascript : Prompt confirmation before exit
+10.6k Golang : Flip coin example
+5.4k Python : Delay with time.sleep() function example
+13.8k Golang : Check if an integer is negative or positive
+22.7k Golang : Strings to lowercase and uppercase example
+5.4k Golang : What is StructTag and how to get StructTag's value?
+11.6k SSL : The certificate is not trusted because no issuer chain was provided
+16k Golang : Get file permission
+7.3k Golang : How to iterate a slice without using for loop?