fn

package
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2022 License: MIT Imports: 12 Imported by: 3

Documentation

Index

Constants

View Source
const (
	String = iota
	Int32
	Int64
	Unknown
)
View Source
const (
	OptIgnore    = "-"         // 忽略当前这个域
	OptOmitempty = "omitempty" // 当这个域的值为空,忽略这个域
	OptDive      = "dive"      // 递归地遍历这个结构体,将所有字段作为键
	OptWildcard  = "wildcard"  // 只适用于字符串类型,返回"%"+值+"%",这是为了方便数据库的模糊查询
)

Variables

View Source
var (
	FieldNoExist error = errors.New("field no exist")
)

Functions

func Apply

func Apply(obj_func any, args []any) []reflect.Value

Apply 通过反射支持字符串直接调用函数

func Assign

func Assign(a, b any) any

Assign

func Atob added in v1.1.2

func Atob(s string) string

Atob 解码base64编码字符串

func Atob2Bytes added in v1.1.2

func Atob2Bytes(s string) []byte

Atob2Bytes 解码base64编码字符串

func B2S

func B2S(b []byte) string

B2S Byte to String 高性能转换

func Btoa added in v1.1.2

func Btoa(s string) string

Btoa 编码base64字符串

func BtoaBytes added in v1.1.2

func BtoaBytes(bytes []byte) string

func Call

func Call(obj_func any, args ...any) []reflect.Value

Call 通过反射支持字符串直接调用函数

func CallMethod

func CallMethod(obj any, methodName string, args ...any) []reflect.Value

CallMethod @param obj 为结构体指针

func FmtString

func FmtString(src string, data map[string]any) string

FmtString @param str @param data @example FmtString("{a}",map[string]any{}{"a":1})

func GetField

func GetField(obj any, name string) (reflect.StructField, bool)

GetField

func GetFieldValu added in v1.1.2

func GetFieldValu(obj any, name string) reflect.Value

func GetFieldValueWithLower added in v1.1.2

func GetFieldValueWithLower(obj any, name string) reflect.Value

func GetKind

func GetKind(v any) reflect.Kind

GetKind 通过反射获取数据类型 GetKind(string) == reflect.String GetKind(nil) == reflect.Invalid

func GetReflectType

func GetReflectType(v any) reflect.Type

GetType 获取数据类型(不包括指针)

func GetReflectValue added in v1.1.2

func GetReflectValue(v any) reflect.Value

GetType 获取数据值(不包括指针)

func GetStructTypeName

func GetStructTypeName(v any) string

获取一个struct的类型名称

func GetTag

func GetTag(obj struct{}, name, tagName string) string

GetTag 获取结构体标签

func GetType

func GetType(v any) string

GetType 没办法判断time.Time GetType(string) == "string" GetType(int) == "int" GetType(int32) == "int32" GetType(int64) == "int64"

func GetTypeEnum added in v1.1.1

func GetTypeEnum(value any) int

func Has

func Has(obj any, name string) bool

Has 通过判断对象是否包含某一个类型

func IsBool

func IsBool(v any) bool

IsBool

func IsInt

func IsInt(v any) bool

IsInt

func IsMap

func IsMap(v any) bool

IsInt

func IsPtr

func IsPtr(v any) bool

IsPtr 是否为指针

func IsString

func IsString(v any) bool

IsString

func IsStringBlank

func IsStringBlank(str string) bool

IsStringBlank 是否空字符串

func IsStringEmail

func IsStringEmail(value string) bool

IsStringEmail 判断字符串是否符合邮箱地址

func IsStringNumber

func IsStringNumber(str string) bool

IsStringNumber 判断字符串是否是数字

func IsStringURL

func IsStringURL(str string) bool

判断字符串是否是一个url地址

func IsTime added in v1.1.1

func IsTime(v any) bool

IsTime 是否为time.Time类型

func IsValidNumber added in v1.1.1

func IsValidNumber(value any) bool

func Map2Struct

func Map2Struct(m map[string]any, s struct{}) error

ToStruct 用map填充结构

func Max added in v1.1.2

func Max[T Number](a, b T) T

func New

func New(v any) any

New 通过反射创建结构体对象,这里最好只传递引用类型

func NewSlice

func NewSlice(v any) any

NewSlice 通过反射创建结构体对象

func Panic added in v1.1.2

func Panic(err error, msg string)

func ParseBool

func ParseBool(str string) bool

ParseBool 将字符串转换为布尔值 它接受真值:1, t, T, TRUE, true, True 它接受假值:0, f, F, FALSE, false, False. 其它任何值都返回一个错误

func ParseFloat32

func ParseFloat32(str string) float32

ParseFloat32

func ParseFloat64

func ParseFloat64(str string) float64

类似于javascript,全局的parseInt、parseFloat 需要去掉首尾空格,否则可能会解析为0 ParseFloat64

func ParseHex

func ParseHex(str string) uint64

ParseHex

func ParseInt

func ParseInt(str string) int

ParseInt 解析失败时,返回int默认值

func ParseInt16

func ParseInt16(str string) int16

ParseInt16

func ParseInt32

func ParseInt32(str string) int32

ParseInt32 有问题,不包括浮点数解析

func ParseInt64

func ParseInt64(str string) int64

ParseInt64

func ParseInt8

func ParseInt8(str string) int8

ParseInt8

func ParseUint

func ParseUint(str string) uint

ParseUint

func ParseUint64

func ParseUint64(str string) uint64

ParseUint64

func Round added in v1.1.1

func Round(f float64, n int) float64

Round 四舍五入保留小数

func RoundInt added in v1.1.2

func RoundInt(f float64) int

RoundInt 四舍五入取整数

func S2B

func S2B(s string) []byte

S2B String to Bytes 高性能转换

func SetInterval added in v1.1.2

func SetInterval(callback func(), delay time.Duration) func()

SetInterval 间隔delay毫秒执行一次

func SetSleep

func SetSleep(action func() bool, delay int)

SetSleep 循环执行,同步等待 不同于setInterval的非阻塞执行,这里会停止直到返回false delay 1s

func SetStructValue added in v1.1.1

func SetStructValue(obj any, name string, value any) error

SetStructValue 设置结构体的值

func SetTimeout added in v1.1.2

func SetTimeout(callback func(), delay time.Duration) func()

SetTimeout 毫秒

func SplitLines added in v1.1.1

func SplitLines(s string) []string

SplitLines

func SplitLines2 added in v1.1.1

func SplitLines2(s string) []string

SplitLines2

func SplitWords added in v1.1.1

func SplitWords(s string) []string

SplitWords 以连续的空白字符为分隔符,将s切分成多个子串,结果中不包含空白字符本身。 空白字符有:\\t, \\n, \\v, \\f, \\r, ’ ‘, U+0085 (NEL), U+00A0 (NBSP) 。 如果 s 中只包含空白字符,则返回一个空列表\n

func StringReverse

func StringReverse(s string) string

ReverseString 反转字符串

func StringReverseOffset

func StringReverseOffset(s string, offset rune) string

StringReverseOffset

func StructToMap

func StructToMap(s any, tag string, methodName string) (res map[string]any, err error)

StructToMap 结构体转map fn.StructToMap(struct, "map", "")

func TimeCost

func TimeCost() func(name string)

TimeCost defer TimeCost()("执行时间耗时")

func ToCamelCase

func ToCamelCase(val string) string

ToCamelCase ToCamelCase("camel-case") == "camelCase"

func ToSliceReflectValue

func ToSliceReflectValue(args []any) []reflect.Value

ToSliceReflectValue

func ToSnakeCase

func ToSnakeCase(val string) string

ToSnakeCase ToSnakeCase("snakeCase") == "snake-case"

func Unicode2ZH

func Unicode2ZH(from string) string

Unicode2ZH 将unicode编码转成中文

Types

type Builder added in v1.1.1

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

@see https://github.com/gaofeng-lin/codehome

func NewStructBuilder added in v1.1.1

func NewStructBuilder() *Builder

func (*Builder) AddBool added in v1.1.1

func (b *Builder) AddBool(name string, tag string) *Builder

func (*Builder) AddField added in v1.1.1

func (b *Builder) AddField(field string, typ reflect.Type, tag string) *Builder

添加字段,传入字段名和类型

func (*Builder) AddFloat64 added in v1.1.1

func (b *Builder) AddFloat64(name string, tag string) *Builder

func (*Builder) AddFunc added in v1.1.1

func (b *Builder) AddFunc(name string, tag string) *Builder

func (*Builder) AddInt64 added in v1.1.1

func (b *Builder) AddInt64(name string, tag string) *Builder

func (*Builder) AddString added in v1.1.1

func (b *Builder) AddString(name string, tag string) *Builder

func (*Builder) AddStringSlice added in v1.1.1

func (b *Builder) AddStringSlice(name string, tag string) *Builder

func (*Builder) Build added in v1.1.1

func (b *Builder) Build() *Struct

根据预先添加的字段构建出结构体类型

type Instance added in v1.1.1

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

结构体的值

func (*Instance) Addr added in v1.1.1

func (i *Instance) Addr() any

func (Instance) Field added in v1.1.1

func (in Instance) Field(name string) (reflect.Value, error)

func (*Instance) Interface added in v1.1.1

func (i *Instance) Interface() any

func (*Instance) SetBool added in v1.1.1

func (in *Instance) SetBool(name string, value bool)

func (*Instance) SetFloat64 added in v1.1.1

func (in *Instance) SetFloat64(name string, value float64)

func (*Instance) SetFunc added in v1.1.1

func (in *Instance) SetFunc(name string, value func())

func (*Instance) SetInt64 added in v1.1.1

func (in *Instance) SetInt64(name string, value int64)

func (*Instance) SetString added in v1.1.1

func (in *Instance) SetString(name, value string)

func (*Instance) SetStringSlice added in v1.1.1

func (in *Instance) SetStringSlice(name string, value []string)

type Number added in v1.1.2

type Number interface {
	int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | float32 | float64
}

type Struct added in v1.1.1

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

实际生成的结构体,基类 结构体的类型

func (Struct) New added in v1.1.1

func (s Struct) New() *Instance

根据结构体类型创建一个实例化

Jump to

Keyboard shortcuts

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