-
Notifications
You must be signed in to change notification settings - Fork 1
/
common.go
58 lines (46 loc) · 1.08 KB
/
common.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package common
import (
"fmt"
"path/filepath"
)
var (
// DataDir - global data directory declaration
DataDir = GetDefaultDataDir()
// ConfigDir - configuration directory
ConfigDir = filepath.FromSlash(fmt.Sprintf("%s/config", DataDir))
)
/* BEGIN EXPORTED METHODS */
/*
BEGIN UTIL METHODS
*/
// ByteIsEqual - == for []byte
func ByteIsEqual(a []byte, b []byte) bool {
if len(a) != len(b) { // Check match
return false // No match
}
for i, v := range a { // Iterate through bytes
if v != b[i] { // Check for match
return false // Return false
}
}
return true // Return true
}
// CatchPanic catches any panic and writes the error to out.
func CatchPanic(out *error) {
if err := recover(); err != nil { // Attempt to recover from panic
*out = UnifyError(err) // Log error
}
}
// UnifyError converts e to error.
func UnifyError(e interface{}) error {
switch e.(type) { // Switch error type
case error: // Is genuine error type
return e.(error) // Log as error type
default:
return fmt.Errorf("%+v", e) // Log
}
}
/*
END UTIL METHODS
*/
/* END EXPORTED METHODS */