util

package
v1.1.0-rc4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 4, 2022 License: Apache-2.0 Imports: 14 Imported by: 19

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ForbiddenMethod = errors.New("forbidden method")

ForbiddenMethod 如果某个方法禁止被调用则可以抛出此错误。

View Source
var UnimplementedMethod = errors.New("unimplemented method")

UnimplementedMethod 如果某个方法未实现则可以抛出此错误。

View Source
var WrapFormat = func(err error, fileline string, format string, a ...interface{}) error {
	if err == nil {
		if format != "" {
			return fmt.Errorf(fileline+" "+format, a...)
		}
		return errors.New(fileline + " " + fmt.Sprint(a...))
	}
	if format == "" {
		return fmt.Errorf("%s %s\n%w", fileline, fmt.Sprint(a...), err)
	}
	return fmt.Errorf("%s %s\n%w", fileline, fmt.Sprintf(format, a...), err)
}

Functions

func Contract

func Contract(filename string, maxLength int) string

Contract 压缩 filename 的长度,超出的部分使用 ... 代替。

func CopyBean

func CopyBean(src interface{}, dest interface{}) error

CopyBean 使用 JSON 序列化的方式进行拷贝,支持匿名字段,支持类型转换。

func Error

func Error(fileline string, text string) error

Error 创建携带文件信息的 error 对象。文件信息未来也许可以在编译期计算。

func Errorf

func Errorf(fileline string, format string, a ...interface{}) error

Errorf 创建携带文件信息的 error 对象。文件信息未来也许可以在编译期计算。

func FileLine

func FileLine(fn interface{}) (file string, line int, fnName string)

FileLine 获取函数所在文件、行数以及函数名

func HasReceiver

func HasReceiver(t reflect.Type, receiver reflect.Type) bool

HasReceiver 函数是否具有接收者。

func Indirect

func Indirect(t reflect.Type) reflect.Type

Indirect 解除 Type 所有层级的指针。

func IsConstructor

func IsConstructor(t reflect.Type) bool

IsConstructor 返回以函数形式注册 bean 的函数是否合法。一个合法 的注册函数需要以下条件:入参可以有任意多个,支持一般形式和 Option 形式,返回值只能有一个或者两个,第一个返回值必须是 bean 源,它可以是 结构体等值类型也可以是指针等引用类型,为值类型时内部会自动转换为引用类 型(获取可引用的地址),如果有第二个返回值那么它必须是 error 类型。

func IsContextType

func IsContextType(t reflect.Type) bool

IsContextType t 是否是 context.Context 类型。

func IsErrorType

func IsErrorType(t reflect.Type) bool

IsErrorType t 是否是 error 类型。

func IsFuncType

func IsFuncType(t reflect.Type) bool

IsFuncType t 是否是 func 类型。

func IsNil

func IsNil(v reflect.Value) bool

IsNil 返回 v 的值是否为 nil,但是不会 panic 。

func IsStructPtr

func IsStructPtr(t reflect.Type) bool

IsStructPtr 返回是否是结构体的指针类型。

func LocalIPv4

func LocalIPv4() string

LocalIPv4 获取本机的 IPv4 地址。

func MD5

func MD5(str string) string

MD5 获取 MD5 计算后的字符串。

func MustTestMode

func MustTestMode()

MustTestMode 非测试模式下调用此函数会发生 panic 。

func NewList

func NewList(v ...interface{}) *list.List

NewList 使用输入的元素创建列表。

func PatchValue

func PatchValue(v reflect.Value) reflect.Value

PatchValue 开放 v 的私有字段,返回修改后的副本。

func ReadDirNames

func ReadDirNames(dirname string) ([]string, error)

ReadDirNames reads the directory named by dirname and returns an unsorted list of directory entries.

func ReturnNothing

func ReturnNothing(t reflect.Type) bool

ReturnNothing 函数是否无返回值。

func ReturnOnlyError

func ReturnOnlyError(t reflect.Type) bool

ReturnOnlyError 函数是否只返回错误值。

func SafeCloseChan

func SafeCloseChan(ch chan struct{})

SafeCloseChan 安全地关闭一个通道。

func SearchList

func SearchList(l *list.List, v interface{}) *list.Element

SearchList 在列表中查询指定元素,存在则返回列表项指针,不存在返回 nil。

func T

func T(a ...interface{}) []interface{}

T 将可变参数转换成切片形式。

func TripleSort

func TripleSort(sorting *list.List, fn GetBeforeItems) *list.List

TripleSort 三路排序

func Wrap

func Wrap(err error, fileline string, text string) error

Wrap 创建携带文件信息的 error 对象。文件信息未来也许可以在编译期计算。

func Wrapf

func Wrapf(err error, fileline string, format string, a ...interface{}) error

Wrapf 创建携带文件信息的 error 对象。文件信息未来也许可以在编译期计算。

Types

type GetBeforeItems

type GetBeforeItems func(sorting *list.List, current interface{}) *list.List

GetBeforeItems 获取 sorting 中排在 current 前面的元素

type NoCopy

type NoCopy struct{}

NoCopy may be embedded into structs which must not be copied after the first use.

See https://golang.org/issues/8005#issuecomment-190753527 for details.

func (*NoCopy) Lock

func (*NoCopy) Lock()

Lock is a no-op used by -copylocks checker from `go vet`.

func (*NoCopy) Unlock

func (*NoCopy) Unlock()

type PanicCond

type PanicCond struct {
	// contains filtered or unexported fields
}

PanicCond 封装触发 panic 的条件。

func NewPanicCond

func NewPanicCond(fn func() interface{}) *PanicCond

NewPanicCond PanicCond 的构造函数。

func Panic

func Panic(err error) *PanicCond

Panic 抛出一个异常值。

func Panicf

func Panicf(format string, a ...interface{}) *PanicCond

Panicf 抛出一段需要格式化的错误字符串。

func (*PanicCond) When

func (p *PanicCond) When(isPanic bool)

When 满足给定条件时抛出一个 panic 。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL