Golang os/user.User type, Current(), Lookup and LookupId() functions example
package os/user
Golang os/user.User type, Current(), Lookup and LookupId() functions usage example.
NOTE : Useful in looking up a user's home directory.
package main
import (
"fmt"
"os/user"
)
func main() {
usr, err := user.Current()
if err != nil {
panic(err)
}
fmt.Println("Uid : ", usr.Uid)
fmt.Println("Gid : ", usr.Gid)
fmt.Println("Username : ", usr.Username)
fmt.Println("Name : ", usr.Name)
fmt.Println("HomeDir: ", usr.HomeDir)
fmt.Println("========user.Lookup()======================")
lookupUsr, err := user.Lookup(usr.Username)
fmt.Println("Uid : ", lookupUsr.Uid)
fmt.Println("Gid : ", lookupUsr.Gid)
fmt.Println("Username : ", lookupUsr.Username)
fmt.Println("Name : ", lookupUsr.Name)
fmt.Println("HomeDir: ", lookupUsr.HomeDir)
fmt.Println("========user.LookupId()======================")
lookupidUsr, err := user.LookupId(usr.Uid)
fmt.Println("Uid : ", lookupidUsr.Uid)
fmt.Println("Gid : ", lookupidUsr.Gid)
fmt.Println("Username : ", lookupidUsr.Username)
fmt.Println("Name : ", lookupUsr.Name)
fmt.Println("HomeDir: ", lookupUsr.HomeDir)
}
References:
http://golang.org/pkg/os/user/#Current
Advertisement
Something interesting
Tutorials
+3.8k JavaScript : Rounding number to decimal formats to display currency
+13.6k Golang : Search folders for file recursively with wildcard support
+10.1k Golang : Find age or leap age from date of birth example
+5.9k Golang : Normalize email to prevent multiple signups example
+11.5k Golang : Get terminal width and height example
+8.2k Golang : Create unique title slugs example
+5k Golang : Extract unicode string from another unicode string example
+11.9k Golang : Handle or parse date string with Z suffix(RFC3339) example
+6.3k Golang : How to stop user from directly running an executable file?
+61.4k Golang : Convert HTTP Response body to string
+6.4k Golang : Create zip/ePub file without compression(use Store algorithm)