Adds some simple utilities for go thing
This commit is contained in:
@@ -83,7 +83,7 @@ func init() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
panic(fmt.Sprintf("Could not open '%s': %s", albumsPath, err))
|
panic(fmt.Sprintf("Could not open '%s': %s", albumsPath, err))
|
||||||
}
|
}
|
||||||
defer CloseOrPanic(f, albumsPath)
|
defer closeOrPanic(f, albumsPath)
|
||||||
|
|
||||||
dec := json.NewDecoder(f)
|
dec := json.NewDecoder(f)
|
||||||
if err = dec.Decode(&albumsDataTest); err != nil {
|
if err = dec.Decode(&albumsDataTest); err != nil {
|
||||||
|
|||||||
35
utils.go
Normal file
35
utils.go
Normal 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))
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user