Golang : Get all local users and print out their home directory, description and group id
In this short tutorial, we will explore how to get a list of users from the local machine and iterate each one of them to find out their home directory, group ID and description. The code is pretty much self-explanatory and please take note that the code can only be executed on Linux/Unix machine as it will parse the /etc/passwd
file to get the users list.
For Windows machine, please see https://www.nextofwindows.com/the-net-command-line-to-list-local-users-and-groups. You can dump out the net users
output to a text file first and then parse the text file to build the users list from there.
Here you go!
package main
import (
"bufio"
"fmt"
"io"
"os"
"os/user"
"strings"
)
var Users []string
func main() {
// this is for Linux/Unix machines
// for Windows
// see https://www.nextofwindows.com/the-net-command-line-to-list-local-users-and-groups
file, err := os.Open("/etc/passwd")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer file.Close()
reader := bufio.NewReader(file)
for {
line, err := reader.ReadString('\n')
// skip all line starting with #
if equal := strings.Index(line, "#"); equal < 0 {
// get the username and description
lineSlice := strings.FieldsFunc(line, func(divide rune) bool {
return divide == ':' // we divide at colon
})
if len(lineSlice) > 0 {
Users = append(Users, lineSlice[0])
}
}
if err == io.EOF {
break
}
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
// now we have a list of users
// iterate(cycle) each of them to
// print out HomeDir, GroupID, description, etc
for _, name := range Users {
usr, err := user.Lookup(name)
if err != nil {
panic(err)
}
// see https://golang.org/pkg/os/user/#User
fmt.Printf("username:%s\n", usr.Username)
fmt.Printf("homedir:%s\n", usr.HomeDir)
fmt.Printf("groupID:%s\n", usr.Gid)
fmt.Printf("DisplayName:%s\n", usr.Name)
fmt.Println("*********************************")
}
}
Hope this helps and happy coding!
References:
https://www.socketloop.com/tutorials/golang-how-to-get-a-user-home-directory-path#comment-3195013804
https://www.socketloop.com/references/golang-bytes-fieldsfunc-function-example
See also : Golang : Reverse IP address for reverse DNS lookup example
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
+24.2k Golang : How to print rune, unicode, utf-8 and non-ASCII CJK(Chinese/Japanese/Korean) characters?
+34.2k Golang : How to stream file to client(browser) or write to http.ResponseWriter?
+4.5k Golang : How to pass data between controllers with JSON Web Token
+28.4k Golang : Detect (OS) Operating System
+13.4k Golang : Set image canvas or background to transparent
+29.2k Golang : Login(Authenticate) with Facebook example
+17k Google Chrome : Your connection to website is encrypted with obsolete cryptography
+14.7k Golang : package is not in GOROOT during compilation
+11.9k Golang : Split strings into command line arguments
+34.3k Golang : Smarter Error Handling with strings.Contains()
+6.7k Golang : Gargish-English language translator
+11.4k How to tell if a binary(executable) file or web application is built with Golang?