2022-07-20 15:12:41 +00:00
|
|
|
package util
|
|
|
|
|
2022-07-26 12:01:11 +00:00
|
|
|
import (
|
|
|
|
"encoding/gob"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2022-07-20 15:12:41 +00:00
|
|
|
func Filter[T any](vs []T, f func(T) bool) []T {
|
|
|
|
filtered := make([]T, 0)
|
2023-02-08 09:16:05 +00:00
|
|
|
|
2022-07-20 15:12:41 +00:00
|
|
|
for _, v := range vs {
|
|
|
|
if f(v) {
|
|
|
|
filtered = append(filtered, v)
|
|
|
|
}
|
|
|
|
}
|
2023-02-08 09:16:05 +00:00
|
|
|
|
2022-07-20 15:12:41 +00:00
|
|
|
return filtered
|
|
|
|
}
|
2022-07-26 12:01:11 +00:00
|
|
|
|
|
|
|
func WriteGob(filePath string, object interface{}) error {
|
|
|
|
file, err := os.Create(filePath)
|
|
|
|
if err == nil {
|
|
|
|
encoder := gob.NewEncoder(file)
|
|
|
|
err = encoder.Encode(object)
|
|
|
|
}
|
2023-02-08 09:16:05 +00:00
|
|
|
|
2022-07-26 12:01:11 +00:00
|
|
|
file.Close()
|
2023-02-08 09:16:05 +00:00
|
|
|
|
2022-07-26 12:01:11 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func ReadGob(filePath string, object interface{}) error {
|
|
|
|
file, err := os.Open(filePath)
|
|
|
|
if err == nil {
|
|
|
|
decoder := gob.NewDecoder(file)
|
|
|
|
err = decoder.Decode(object)
|
|
|
|
}
|
2023-02-08 09:16:05 +00:00
|
|
|
|
2022-07-26 12:01:11 +00:00
|
|
|
file.Close()
|
2023-02-08 09:16:05 +00:00
|
|
|
|
2022-07-26 12:01:11 +00:00
|
|
|
return err
|
|
|
|
}
|