description |
---|
spring-utils |
提供一些针对单元测试、原生类型、集合、网络、错误、反射、同步等方面的工具函数。
提供一系列 Assert 函数用于编写单元测试。
asserts that expect and got are equal as defined by reflect.DeepEqual
.
func AssertEqual(t *testing.T, expect interface{}, got interface{})
asserts that function fn()
would panic. It fails if the panic message does not match the regular expression in expr
.
func AssertPanic(t *testing.T, fn func(), expr string)
asserts that a got value matches a given regular expression.
func AssertMatches(t *testing.T, expr string, got string)
提供一些针对原生类型的工具函数。
安全地关闭一个管道。
func SafeCloseChan(ch chan struct{})
将对象序列化为 Json 字符串,错误信息以结果返回。
func ToJson(i interface{}) string
使用 Json 序列化框架进行拷贝,支持匿名字段,支持类型转换。
func CopyBeanUseJson(src interface{}, dest interface{}) error
提供一些针对集合的工具函数。
在一个 int 数组中进行查找,找不到返回 -1。
func ContainsInt(array []int, val int) int
在一个 string 数组中进行查找,找不到返回 -1。
func ContainsString(array []string, val string) int
使用输入的元素创建列表。
func NewList(v ...interface{}) *list.List
在列表中查询指定元素,存在则返回列表项指针,不存在返回 nil。
func FindInList(v interface{}, l *list.List) (*list.Element, bool)
提供一些针对数据编码的工具函数。
获取 MD5 计算后的字符串.
func MD5(str string) string
返回 BASE64 加密后的字符串。
func BASE64(str string) string
提供一些针对 error 的工具函数。
封装一个异常源。
func WithCause(r interface{}) error
获取异常源。
func Cause(err error) interface{}
获取 error 的字符串。
func ErrorToString(err error) string
返回 error 发生的文件行信息。
func ErrorWithFileLine(err error, skip ...int) error
提供一些针对 panic 的工具函数。
封装触发 panic 的内容和条件。
type PanicCond struct {}
// NewPanicCond PanicCond 的构造函数。
func NewPanicCond(fn func() interface{}) *PanicCond {}
// When 满足给定条件时抛出一个 panic。
func (p *PanicCond) When(isPanic bool) {}
抛出一个异常值。
func Panic(err error) *PanicCond
抛出一段需要格式化的错误字符串。
func Panicf(format string, a ...interface{}) *PanicCond
提供一些针对反射的工具函数。
allAccess 为 true 时开放 v 的私有字段,返回修改后的副本。
func PatchValue(v reflect.Value, allAccess bool) reflect.Value
解除 Type 的指针。
func Indirect(t reflect.Type) reflect.Type
获取函数所在文件、行数以及函数名。
func FileLine(fn interface{}) (file string, line int, fnName string)
返回 reflect.Value 的值是否为 nil,比原生方法更安全。
func IsNil(v reflect.Value) bool
提供一些针对网络的工具函数。
获取本机的 IPv4 地址。
func LocalIPv4() string
提供一些针对时间的工具函数。
返回当前的毫秒时间。
func CurrentMilliSeconds() int64
返回 Duration 返回对应的毫秒时长。
func MilliSeconds(d time.Duration) int64
封装 sync.WaitGroup,提供更简单的 API。
type WaitGroup struct {
wg sync.WaitGroup
}
// Add 添加一个任务,任务在 Goroutine 中执行
func (wg *WaitGroup) Add(fn func())
// Wait 等待所有任务执行完成
func (wg *WaitGroup) Wait()