0
0
mirror of https://github.com/thegeeklab/drone-admin.git synced 2024-11-24 21:20:42 +00:00
drone-admin/admin/util/util.go

43 lines
636 B
Go
Raw Permalink Normal View History

2022-07-20 15:12:41 +00:00
package util
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)
2022-07-20 15:12:41 +00:00
for _, v := range vs {
if f(v) {
filtered = append(filtered, v)
}
}
2022-07-20 15:12:41 +00:00
return filtered
}
func WriteGob(filePath string, object interface{}) error {
file, err := os.Create(filePath)
if err == nil {
encoder := gob.NewEncoder(file)
err = encoder.Encode(object)
}
file.Close()
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)
}
file.Close()
return err
}