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.8k Facebook : How to force facebook to scrape latest URL link data?
+8.7k Golang : Accept any number of function arguments with three dots(...)
+21.2k Curl usage examples with Golang
+6.6k Golang : Experimental emojis or emoticons icons programming language
+13.7k Golang : convert(cast) string to float value
+31.2k Golang : bufio.NewReader.ReadLine to read file line by line
+9.2k Golang : How to get ECDSA curve and parameters data?
+14.4k Golang : Rename directory
+10.1k Golang : Random Rune generator
+8.2k Golang : Number guessing game with user input verification example
+4.9k Golang : PGX CopyFrom to insert rows into Postgres database
+10k Golang : How to get quoted string into another string?