os/user

Guided tour · CLI & Runtime · pkg.go.dev →

Look up the current user, users by name/uid, and their groups.

Current user and home dir

user.Current

u, _ := user.Current()
fmt.Println(u.Username, u.Uid, u.HomeDir)

Home dir — also available via os.UserHomeDir

os.UserHomeDir is often what you really want — it consults $HOME and falls back correctly.

home, _ := os.UserHomeDir()
fmt.Println(home)

Lookup

Lookup by name or ID

u, _ := user.Lookup("ada")
u, _  = user.LookupId("1000")

GroupIds — groups a user belongs to

gids, _ := u.GroupIds()
for _, gid := range gids {
    g, _ := user.LookupGroupId(gid)
    fmt.Println(g.Name)
}