forked from gookit/goutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoutil.go
63 lines (53 loc) · 1.3 KB
/
goutil.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
59
60
61
62
63
// Package goutil 💪 Useful utils for Go: int, string, array/slice, map, error, time, format, CLI, ENV, filesystem,
// system, testing, debug and more.
package goutil
import (
"fmt"
"github.com/gookit/goutil/stdutil"
)
// Value alias of stdutil.Value
type Value = stdutil.Value
// Go is a basic promise implementation: it wraps calls a function in a goroutine
// and returns a channel which will later return the function's return value.
func Go(f func() error) error {
ch := make(chan error)
go func() {
ch <- f()
}()
return <-ch
}
// PanicIfErr if error is not empty, will panic
func PanicIfErr(err error) {
if err != nil {
panic(err)
}
}
// PanicErr if error is not empty, will panic
func PanicErr(err error) {
if err != nil {
panic(err)
}
}
// MustOK if error is not empty, will panic
func MustOK(err error) {
if err != nil {
panic(err)
}
}
// Panicf format panic message use fmt.Sprintf
func Panicf(format string, v ...interface{}) {
panic(fmt.Sprintf(format, v...))
}
// FuncName get func name
func FuncName(f interface{}) string {
return stdutil.FuncName(f)
}
// PkgName get current package name. alias of stdutil.PkgName()
//
// Usage:
//
// funcName := goutil.FuncName(fn)
// pgkName := goutil.PkgName(funcName)
func PkgName(funcName string) string {
return stdutil.PkgName(funcName)
}