loginToTemplate/user_list.go

32 lines
574 B
Go
Raw Normal View History

2022-03-01 16:55:49 +00:00
package main
import (
"encoding/json"
"fmt"
"io"
)
// UserList stores a collection of users.
type UserList []User
// Find tries to return username from UserList
func (ul UserList) Find(username string) *User {
for i, u := range ul {
if u.Username == username {
return &ul[i]
}
}
return nil
}
// NewUserList creates a UserList from JSON
func NewUserList(rdr io.Reader) (UserList, error) {
var userList []User
err := json.NewDecoder(rdr).Decode(&userList)
if err != nil {
err = fmt.Errorf("problem parsing userlist, %v", err)
}
return userList, err
}