Adds some simple utilities for go thing

This commit is contained in:
2016-01-20 15:21:05 +01:00
parent f6fbe8bd4e
commit 261c43184e
2 changed files with 36 additions and 1 deletions

View File

@@ -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 {

35
utils.go Normal file
View File

@@ -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))
}