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
+43.7k Golang : Get hardware information such as disk, memory and CPU usage
+5.1k Golang : Get a list of crosses(instruments) available to trade from Oanda account
+7.5k Android Studio : How to detect camera, activate and capture example
+10.9k Golang : Command line file upload program to server example
+10.2k Golang : How to get quoted string into another string?
+24.1k Golang : Find biggest/largest number in array
+15.1k Golang : Search folders for file recursively with wildcard support
+14.2k Javascript : Prompt confirmation before exit
+9.4k Golang : Launch Mac OS X Preview (or other OS) application from your program example
+11.2k Golang : Roll the dice example
+12.6k Golang : Forwarding a local port to a remote server example