_type

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2021 License: Apache-2.0 Imports: 13 Imported by: 1

README

t

golang type extension. Conversion of Basic Types.
(golang基本数据类型的相互转换)

catalog index

usage

1. type bind convert (类型定义转换)

package main

import (
	"fmt"
	t "gitee.com/KotlinToGo/type"
)

func main()  {
    var b t.Type
    b = t.New("2.3")

    fmt.Println(b.ToPredict())
    fmt.Println(b.Float64())
    fmt.Println(b.Float32())
    fmt.Println(b.Int64())
    fmt.Println(b.Int())
    fmt.Println(b.Int32())
    fmt.Println(b.Int16())
    fmt.Println(b.Int8())
    fmt.Println(b.Uint64())
    fmt.Println(b.Uint())
    fmt.Println(b.Uint32())
    fmt.Println(b.Uint16())
    fmt.Println(b.Uint8())
    fmt.Println(b.Bool())
}

result

2.3
2.3
2.3
2
2
2
2
2
2
2
2
2
2
true

2. standard type convertion (标准类型转换)

package main

import (
	"fmt"
	"github.com/gohouse/golib/t"
)

func main()  {
	var a = "8.8"
	fmt.Println(t.ParseString(a))
	fmt.Println(t.ParseFloat64(a))
	fmt.Println(t.ParseInt64(a))
	fmt.Println(t.ParseInt(a))
	fmt.Println(t.ParseUint64(a))
	fmt.Println(t.ParseUint(a))
	fmt.Println(t.ParseBool(a))
}

result

8.8
8.8
8
8
8
8
true

3. complex type converts (复杂类型转换)

func convertComplex() {
	var cc interface{}
	cc = map[string]int{
		"a": 1,
		"b": 2,
	}
	fmt.Printf("%#v \n", t.New(cc).Map())
	fmt.Printf("%#v \n", t.New(cc).MapInterfaceT())
	fmt.Printf("%#v \n", t.New(cc).MapStringT())
	fmt.Printf("%#v \n", t.New(cc).MapStringInterface())
}

4. 验证规则判断

t.New("http://www.gorose.com").IsUrl()
t.New("qq@gorose.com").IsEmail()

所有判断方法

// iDetermine 判断
type iDetermine interface {
	IsNumeric() bool                        // 是否数字
	IsInteger() bool                        // 是否整数
	IsFloat() bool                          // 是否浮点数
	IsZero() bool                           // 是否零值
	IsChineseCharacters() bool              // 是否汉字
	IsChineseName() bool                    // 是否中文名字
	IsHost() bool                           // 是否域名
	IsUrl() bool                            // 是否互联网url地址
	IsEmail() bool                          // 是否邮箱地址
	IsChineseMobile() bool                  // 是否中国手机号
	IsDate() bool                           // 是否常用的日期格式
	IsDatetime() bool                       // 是否常用的日期时间格式
	IsIpV4() bool                           // 是否ipv4地址
	IsIpV6() bool                           // 是否ipv6地址
	IsIp() bool                             // 是否ip地址
	IsChineseID() bool                      // 是否中国大陆身份证号码
	IsXml() bool                            // 是否xml
	IsJson() bool                           // 是否json
	IsJsonMap() bool                        // 是否是json对象
	IsJsonSlice() bool                      // 是否是json数组
	IsBetween(min, max interface{}) bool    // 是否在两数之间
	IsBetweenFloat64(min, max float64) bool // 是否在两个浮点数之间
	IsBetweenAlpha(min, max string) bool    // 是否在两个字符之间
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ArrayUniqueString

func ArrayUniqueString(arr []string) (newArr []string)

ArrayUnique 数组去重

func InArray

func InArray(argv interface{}, haystack interface{}) bool

InArray 是否存在给定的数组中

func InArrayString

func InArrayString(arg string, args []string) bool

InArrayString 是否存在给定的字符串数组中

func ParseBool

func ParseBool(o interface{}) bool

ParseBool ...

func ParseByte

func ParseByte(i interface{}) byte

ParseByte ...

func ParseBytes

func ParseBytes(o interface{}) []byte

ParseBytes ...

func ParseFloat32

func ParseFloat32(o interface{}) float32

ParseFloat32 ...

func ParseFloat64

func ParseFloat64(o interface{}) (res float64)

ParseFloat64 ...

func ParseInt

func ParseInt(o interface{}) int

ParseInt ...

func ParseInt16

func ParseInt16(o interface{}) (res int16)

ParseInt16 ...

func ParseInt32

func ParseInt32(o interface{}) (res int32)

ParseInt32 ...

func ParseInt64

func ParseInt64(o interface{}) (res int64)

ParseInt64 ...

func ParseInt8

func ParseInt8(o interface{}) (res int8)

ParseInt8 ...

func ParseReader

func ParseReader(arg interface{}) *bufio.Reader

func ParseRune

func ParseRune(o interface{}) rune

ParseRune ...

func ParseRunes

func ParseRunes(o interface{}) []rune

ParseRunes ...

func ParseString

func ParseString(o interface{}) string

ParseString 任意对象转换为字符串

func ParseUint

func ParseUint(o interface{}) uint

ParseUint ...

func ParseUint16

func ParseUint16(o interface{}) uint16

ParseUint16 ...

func ParseUint32

func ParseUint32(o interface{}) uint32

ParseUint32 ...

func ParseUint64

func ParseUint64(o interface{}) uint64

ParseUint64 ...

func ParseUint8

func ParseUint8(o interface{}) uint8

ParseUint8 ...

func ParseWriter

func ParseWriter(arg interface{}) io.Writer

func SetDefaultRecover

func SetDefaultRecover(dr func(err error))

func SplitAndTrimSpace

func SplitAndTrimSpace(s, sep string) (res []string)

SplitAndTrimSpace 字符串分割并且去掉两边的空格

func WithMutexContext

func WithMutexContext(mu *sync.Mutex, fn func())

func WithRWMutexContext

func WithRWMutexContext(mur *sync.RWMutex, fn func())

func WithRecover

func WithRecover(h func(), errDefaultRecover ...func(err error))

func WithRunTimeContext

func WithRunTimeContext(closer func(), callback func(time.Duration))

func WithTicker

func WithTicker(duration time.Duration, fn func())

Types

type Map

type Map map[Type]Type

Map ...

type MapIntT

type MapIntT map[int]Type

type MapIntT64T

type MapIntT64T map[int64]Type

MapIntT64T ...

type MapInterfaceT

type MapInterfaceT map[interface{}]Type

MapInterfaceT ...

type MapStringT

type MapStringT map[string]Type

MapStringT ...

type Slice

type Slice []Type

Slice ...

func ArrayUnique

func ArrayUnique(array interface{}) (newArr Slice)

ArrayUnique 数组去重

func (Slice) SliceInt

func (s Slice) SliceInt() (res []int)

func (Slice) SliceString

func (s Slice) SliceString() (res []string)

func (Slice) String

func (s Slice) String() string

type Type

type Type interface {
	// contains filtered or unexported methods
}

Type 超级type类型

func If

func If(b bool, x, y interface{}) Type

If 三元判断

func Max

func Max(args ...interface{}) Type

Max 获取最大的数

func Min

func Min(args ...interface{}) Type

Min 获取最小的数

type TypeContext

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

TypeContext ...

func New

func New(o interface{}) TypeContext

New 初始化 Type 对象

func (TypeContext) Bind

func (tc TypeContext) Bind(o interface{}) error

Bind 将绑定结果当做json,来绑定到对象上

func (TypeContext) Bool

func (tc TypeContext) Bool() bool

Bool ...

func (TypeContext) Byte

func (tc TypeContext) Byte() byte

Byte ...

func (TypeContext) Bytes

func (tc TypeContext) Bytes() []byte

Bytes ...

func (TypeContext) Extract

func (tc TypeContext) Extract(keys ...interface{}) Type

Extract 多层次抽取值 例: New(`{"a":1,"b":["c",{"d":2}]}`).Extract("b.1.d") // 2 或者 New(`{"a":1,"b":["c",{"d":2}]}`).Extract("b",1,"d") // 2 以上两种方式, 只能使用其中一种, 不能两种混用, 如果key中有点., 则可以使用第二种,不会按照点去分割的

func (TypeContext) ExtractUrl

func (tc TypeContext) ExtractUrl() []string

func (TypeContext) ExtractWithDefault

func (tc TypeContext) ExtractWithDefault(keys []interface{}, defaultVal interface{}) Type

ExtractWithDefault 多层次抽取值, 如果是零值, 则返回给定的默认值

func (TypeContext) Float32

func (tc TypeContext) Float32() float32

Float32 ...

func (TypeContext) Float64

func (tc TypeContext) Float64() float64

Float64 ...

func (TypeContext) InArray

func (tc TypeContext) InArray(haystack interface{}) bool

InArray 是否存在给定的数组中

func (TypeContext) InArrayString

func (tc TypeContext) InArrayString(args []string) bool

InArrayString 是否存在给定的字符串数组中

func (TypeContext) Int

func (tc TypeContext) Int() int

Int ...

func (TypeContext) Int16

func (tc TypeContext) Int16() int16

Int16 ...

func (TypeContext) Int32

func (tc TypeContext) Int32() int32

Int32 ...

func (TypeContext) Int64

func (tc TypeContext) Int64() int64

Int64 ...

func (TypeContext) Int8

func (tc TypeContext) Int8() int8

Int8 ...

func (TypeContext) Interface

func (tc TypeContext) Interface() interface{}

Interface ...

func (TypeContext) IsBetween

func (tc TypeContext) IsBetween(min, max interface{}) bool

Between 是否在给定的两个整数之间

func (TypeContext) IsBetweenAlpha

func (tc TypeContext) IsBetweenAlpha(min, max string) bool

BetweenFloat64 是否在给定的两个字母之间

func (TypeContext) IsBetweenFloat64

func (tc TypeContext) IsBetweenFloat64(min, max float64) bool

BetweenFloat64 是否在给定的两个浮点数字之间

func (TypeContext) IsChineseCharacters

func (tc TypeContext) IsChineseCharacters() bool

IsChineseCharacters 是否汉字

func (TypeContext) IsChineseID

func (tc TypeContext) IsChineseID() bool

IsChineseID 是否中国大陆身份证号码

func (TypeContext) IsChineseMobile

func (tc TypeContext) IsChineseMobile() bool

IsEmail 是否中国手机号

func (TypeContext) IsChineseName

func (tc TypeContext) IsChineseName() bool

IsChineseCharacters 是否中国的名字

func (TypeContext) IsDate

func (tc TypeContext) IsDate() bool

IsDate 是否日期 yyyy-mm-dd 格式

func (TypeContext) IsDatetime

func (tc TypeContext) IsDatetime() bool

IsDateTime 是否日期时间 yyyy-mm-dd HH:ii:ss 格式

func (TypeContext) IsEmail

func (tc TypeContext) IsEmail() bool

IsEmail 是否邮箱地址

func (TypeContext) IsFloat

func (tc TypeContext) IsFloat() bool

IsFloat 是否为float

func (TypeContext) IsHost

func (tc TypeContext) IsHost() bool

IsHost 是否域名

func (TypeContext) IsInteger

func (tc TypeContext) IsInteger() bool

IsInteger 是否为整数

func (TypeContext) IsIp

func (tc TypeContext) IsIp() bool

IsIp 是否ip

func (TypeContext) IsIpV4

func (tc TypeContext) IsIpV4() bool

IsIpV4 是否ipv4

func (TypeContext) IsIpV6

func (tc TypeContext) IsIpV6() bool

IsIpV6 是否ipv6

func (TypeContext) IsJson

func (tc TypeContext) IsJson() bool

IsJson 是否json

func (TypeContext) IsJsonMap

func (tc TypeContext) IsJsonMap() bool

IsJsonMap 是否json对象

func (TypeContext) IsJsonSlice

func (tc TypeContext) IsJsonSlice() bool

IsJsonSlice 是否json数组

func (TypeContext) IsNumeric

func (tc TypeContext) IsNumeric() bool

IsNumeric 是否为数字,包含整数和小数

func (TypeContext) IsUrl

func (tc TypeContext) IsUrl() bool

IsUrl 是否域名

func (TypeContext) IsXml

func (tc TypeContext) IsXml() bool

IsXml 是否xml

func (TypeContext) IsZero

func (tc TypeContext) IsZero() bool

func (TypeContext) Length

func (tc TypeContext) Length() int

Length 数据长度, 以 []byte 统计

func (TypeContext) Map

func (tc TypeContext) Map() Map

Map ...

func (TypeContext) MapIntT

func (tc TypeContext) MapIntT() MapIntT

MapIntT64T ...

func (TypeContext) MapIntT64T

func (tc TypeContext) MapIntT64T() MapIntT64T

MapIntT64T ...

func (TypeContext) MapInterfaceT

func (tc TypeContext) MapInterfaceT() MapInterfaceT

MapInterfaceT ...

func (TypeContext) MapStringInterface

func (tc TypeContext) MapStringInterface() map[string]interface{}

MapStringInterface ...

func (TypeContext) MapStringT

func (tc TypeContext) MapStringT() MapStringT

MapStringT ...

func (TypeContext) Reader

func (tc TypeContext) Reader() *bufio.Reader

func (TypeContext) Rune

func (tc TypeContext) Rune() rune

Rune ...

func (TypeContext) Runes

func (tc TypeContext) Runes() []rune

ParseRunes ...

func (TypeContext) Slice

func (tc TypeContext) Slice() Slice

Slice ...

func (TypeContext) SliceFloat64

func (tc TypeContext) SliceFloat64() []float64

SliceFloat64 ...

func (TypeContext) SliceInt

func (tc TypeContext) SliceInt() []int

SliceInt ...

func (TypeContext) SliceInt64

func (tc TypeContext) SliceInt64() []int64

SliceInt64 ...

func (TypeContext) SliceInterface

func (tc TypeContext) SliceInterface() []interface{}

SliceInterface ...

func (TypeContext) SliceMapStringInterface

func (tc TypeContext) SliceMapStringInterface() []map[string]interface{}

SliceMapStringInterface ...

func (TypeContext) SliceMapStringT

func (tc TypeContext) SliceMapStringT() []MapStringT

SliceMapStringT ...

func (TypeContext) SliceString

func (tc TypeContext) SliceString() []string

SliceString ...

func (TypeContext) SplitAndTrimSpace

func (tc TypeContext) SplitAndTrimSpace(sep string) (res []string)

SplitAndTrimSpace 字符串分割并且去掉两边的空格

func (TypeContext) String

func (tc TypeContext) String() string

ToPredict ...

func (TypeContext) Uint

func (tc TypeContext) Uint() uint

Uint ...

func (TypeContext) Uint16

func (tc TypeContext) Uint16() uint16

Uint16 ...

func (TypeContext) Uint32

func (tc TypeContext) Uint32() uint32

Uint32 ...

func (TypeContext) Uint64

func (tc TypeContext) Uint64() uint64

Uint64 ...

func (TypeContext) Uint8

func (tc TypeContext) Uint8() uint8

Uint8 ...

func (TypeContext) Writer

func (tc TypeContext) Writer() io.Writer

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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