Skip to content

Commit 69911bc

Browse files
author
wintrmvte
committed
+ unified Serializer() for structs
1 parent 1028e97 commit 69911bc

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

data_manipulation.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"strings"
1111
"os"
1212
"bufio"
13+
"encoding/gob"
1314
"time"
1415
"github.com/c-robinson/iplib"
1516
)
@@ -384,6 +385,23 @@ func BoolCheck(boolean interface{}) bool {
384385
return false
385386
}
386387

388+
// Unified serializer/deserializer for structs - logic is based on whether a .gob file already exists
389+
func Serializer(gobpath string, obj interface{}){
390+
if (Exists(gobpath)){
391+
gobfile, err := os.Open(gobpath)
392+
Check(err)
393+
decoder := gob.NewDecoder(gobfile)
394+
decoder.Decode(obj)
395+
gobfile.Close()
396+
} else {
397+
gobfile, err := os.Create(gobpath)
398+
Check(err)
399+
encoder := gob.NewEncoder(gobfile)
400+
encoder.Encode(obj)
401+
gobfile.Close()
402+
}
403+
}
404+
387405
// Removes values from generics that do noe pass a truthcheck of f()
388406
/*func Decimator[T any](s []T, f func(T) bool) []T {
389407
var r []T

0 commit comments

Comments
 (0)