forked from facesea/banshee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
52 lines (44 loc) · 1.08 KB
/
util.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
// Copyright 2015 Eleme Inc. All rights reserved.
// Package util provides util functions.
package util
import (
"os"
"runtime"
"strconv"
"strings"
"testing"
"time"
)
// Must asserts the given value is True for testing.
func Must(t *testing.T, v bool) {
if !v {
_, fileName, line, _ := runtime.Caller(1)
t.Errorf("\n unexcepted: %s:%d", fileName, line)
t.FailNow()
}
}
// ToFixed truncates float64 type to a particular percision in string.
func ToFixed(n float64, prec int) string {
s := strconv.FormatFloat(n, 'f', prec, 64)
return strings.TrimRight(strings.TrimRight(s, "0"), ".")
}
// IsFileExist test whether a filepath is exist.
func IsFileExist(fileName string) bool {
_, err := os.Stat(fileName)
if os.IsNotExist(err) {
return false
}
return true
}
// Timer is a minimal timer util.
type Timer struct {
startAt time.Time
}
// NewTimer creates a minimal timer.
func NewTimer() *Timer {
return &Timer{time.Now()}
}
// Elapsed returns milliseconds elapsed.
func (t *Timer) Elapsed() float64 {
return float64(time.Since(t.startAt).Nanoseconds()) / float64(1000*1000)
}