From 261c43184e2736212ce4f6de259a292bda1efc03 Mon Sep 17 00:00:00 2001 From: Alexandre Tuleu Date: Wed, 20 Jan 2016 15:21:05 +0100 Subject: [PATCH] Adds some simple utilities for go thing --- data_test.go | 2 +- utils.go | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 utils.go diff --git a/data_test.go b/data_test.go index 839fd56..ee0d912 100644 --- a/data_test.go +++ b/data_test.go @@ -83,7 +83,7 @@ func init() { if err != nil { panic(fmt.Sprintf("Could not open '%s': %s", albumsPath, err)) } - defer CloseOrPanic(f, albumsPath) + defer closeOrPanic(f, albumsPath) dec := json.NewDecoder(f) if err = dec.Decode(&albumsDataTest); err != nil { diff --git a/utils.go b/utils.go new file mode 100644 index 0000000..23a4682 --- /dev/null +++ b/utils.go @@ -0,0 +1,35 @@ +package main + +import ( + "fmt" + "io" + "log" + "runtime" +) + +// close a Closer or panic. better than just close, object scoping safe +func closeOrPanic(c io.Closer, description string) { + if err := c.Close(); err != nil { + panic("Could not close '" + description + "': " + err.Error()) + } +} + +// close and ignore any error +func closeIgnore(c io.Closer) { + c.Close() +} + +func closeOrLog(c io.Closer, format string) { + if err := c.Close(); err != nil { + log.Printf(format, err) + } +} + +// retrun a nice not yet implemented error +func notYetImplemented() error { + pc, _, _, ok := runtime.Caller(1) + if ok == false { + return fmt.Errorf("notYetImplemented() cshould not be called from static context") + } + return fmt.Errorf("%s is not yet implemented", runtime.FuncForPC(pc)) +}