users

package module
v0.0.0-...-aa7de21 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 21, 2023 License: Apache-2.0 Imports: 7 Imported by: 1

README

Users

Package users retrieves Users in the Linux system

Install

go get github.com/Faione/users

Usage

loginUsers, err := ListLogged()

if err != nil {
    return
}

rlt, _ := json.MarshalIndent(loginUsers, "", "     ")
fmt.Println(string(rlt))

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ListAll

func ListAll(filters ...UserFilter) ([]*user.User, error)

读取 `/etc/passwd` 文件,获得所有用户的信息

可以通过设置 UserFilter 来对用户进行过滤

Example
users, err := ListAll(func(user *user.User) bool {
	return len(user.HomeDir) > 4 && user.HomeDir[:5] == "/home"
})
if err != nil {
	return
}

if len(users) == 0 {
	return
}

rlt, _ := json.MarshalIndent(users, "", "     ")
fmt.Println(string(rlt))
Output:

Types

type LoggedUser

type LoggedUser struct {
	Username  string
	Tty       string
	LoginTime string
}

func ListLogged

func ListLogged() ([]*LoggedUser, error)

读取 `/var/run/utmp` 文件,获得登陆用户的信息

Example
loginUsers, err := ListLogged()

if err != nil {
	return
}

rlt, _ := json.MarshalIndent(loginUsers, "", "     ")
fmt.Println(string(rlt))
Output:

type UserFilter

type UserFilter func(user *user.User) bool

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL