base

package module
v1.0.22 Latest Latest
Warning

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

Go to latest
Published: Jan 27, 2022 License: MIT Imports: 31 Imported by: 20

README

base

base

Documentation

Index

Constants

View Source
const (
	En   = `en`
	ZhTw = `zh-tw`
	ZhCn = `zh_CN`
)
View Source
const (
	ModeWait    = 1
	ModeMaxTime = 2
	ModeError   = 3
)

Variables

View Source
var (
	ErrInvalidPaddingChar = errors.New(`invalid padding char`)
	ErrAesDecrypt         = errors.New(`aes decrypt error`)
)
View Source
var (
	ErrMsgMapEmpty = errors.New("msgMap can not empty")
	ErrNoYamlFiles = errors.New("there are no `.yml` or `.yaml` files in this directory")
	ErrNoJsonFiles = errors.New("there are no `.json` files in this directory")
)
View Source
var (
	ErrIpV4Address = errors.New(`invalid ip v4 address`)
	ErrNotFoundIp  = errors.New("not found ip address")
)
View Source
var (

	// AcquireArgs 获取参数
	AcquireArgs = func() []interface{} {
		return argsPool.Get().([]interface{})
	}

	// ReleaseArgs 释放参数
	ReleaseArgs = func(args *[]interface{}) {
		if args == nil {
			return
		}

		*args = (*args)[:0]
		argsPool.Put(*args)
	}
)
View Source
var (
	ErrOutOfRange = errors.New("out of range")
	ErrTimeBack   = errors.New("time go back")
	ErrMachineId  = errors.New("illegal machine id")
	ErrLogicId    = errors.New("illegal logic id")
)
View Source
var ErrNoServer = errors.New("no server")
View Source
var (
	SetValue = struct{}{}
)

Functions

func BigCamels added in v1.0.20

func BigCamels(sep byte, data string) string

BigCamels 转换为大驼峰

func Black added in v1.0.3

func Black(format string, args ...interface{})

Black 默认输出

func Blue added in v1.0.3

func Blue(format string, args ...interface{})

Blue 颜色输出

func Bytes2Float64 added in v1.0.12

func Bytes2Float64(data []byte) float64

Bytes2Float64 字节切片转换为float64

func Bytes2Int64 added in v1.0.7

func Bytes2Int64(data []byte) int64

Bytes2Int64 字节切片转换为int64

func Bytes2String added in v1.0.7

func Bytes2String(data []byte) string

Bytes2String 字节切片转换为字符串

func CreatePkcs1Keys added in v1.0.6

func CreatePkcs1Keys(keyLength int) (privateKey, publicKey string)

CreatePkcs1Keys 生成pkcs1格式公钥私钥

func CreatePkcs8Keys added in v1.0.3

func CreatePkcs8Keys(keyLength int) (privateKey, publicKey string)

CreatePkcs8Keys 生成pkcs8格式公钥私钥

func Debug added in v1.0.21

func Debug(args ...interface{})

func Debugf added in v1.0.21

func Debugf(template string, args ...interface{})

func Error added in v1.0.21

func Error(args ...interface{})

func Errorf added in v1.0.21

func Errorf(template string, args ...interface{})

func Fatal added in v1.0.21

func Fatal(args ...interface{})

func Fatalf added in v1.0.21

func Fatalf(template string, args ...interface{})

func FileExists added in v1.0.22

func FileExists(fileName string) bool

FileExists 判断文件是否存在

func Fuchsia added in v1.0.3

func Fuchsia(format string, args ...interface{})

Fuchsia 颜色输出

func Green added in v1.0.3

func Green(format string, args ...interface{})

Green 成功输出

func HMac added in v1.0.3

func HMac(key []byte, data []byte, hash crypto.Hash) string

HMac HMac算法

func Hash added in v1.0.3

func Hash(data []byte, hash crypto.Hash) string

Hash Hash算法

func HashValue

func HashValue(key interface{}) uint32

HashValue 计算任意类型hash值

func Index4Bit

func Index4Bit(key interface{}, bitCount uint8) uint32

Index4Bit 索引路由方法,值范围为uint32

func Index4Int16

func Index4Int16(key interface{}) int16

Index4Int16 索引路由方法,值范围为int16

func Index4Int8

func Index4Int8(key interface{}) int8

Index4Int8 索引路由方法,值范围为int8

func Index4Uint8

func Index4Uint8(key interface{}) uint8

Index4Uint8 索引路由方法,值范围为uint8

func Info added in v1.0.21

func Info(args ...interface{})

func Infof added in v1.0.21

func Infof(template string, args ...interface{})

func InitLogger added in v1.0.21

func InitLogger(l Logger)

func InitZap added in v1.0.21

func InitZap(l *zap.Logger)

InitZap 初始化Zap日志

func InitZapWithConfig added in v1.0.21

func InitZapWithConfig(conf zap.Config, opts ...zap.Option) error

InitZapWithConfig with zap.Config初始化Zap日志

func InitZapWithCore added in v1.0.21

func InitZapWithCore(core zapcore.Core, opts ...zap.Option)

InitZapWithCore with core初始化Zap日志

func InitZapWithProduction added in v1.0.21

func InitZapWithProduction(opts ...zap.Option) error

InitZapWithProduction with production初始化Zap日志

func Ip2Long

func Ip2Long(ip string) (ipVal uint32, err error)

Ip2Long Ip转换为uint32

func JsonDecode

func JsonDecode(data []byte, v interface{}) (err error)

JsonDecode ---

func JsonDecodeFile

func JsonDecodeFile(filePath string, v interface{}) (err error)

JsonDecodeFile ---

func JsonEncode

func JsonEncode(v interface{}) (data []byte, err error)

JsonEncode ---

func LocalIp added in v1.0.8

func LocalIp() (ip string, err error)

LocalIp 获取本机Ip

func Long2Ip

func Long2Ip(ipVal uint32) string

Long2Ip uint32转换为Ip

func MarshalPkcs8PrivateKey added in v1.0.6

func MarshalPkcs8PrivateKey(key *rsa.PrivateKey) []byte

func Md5 added in v1.0.3

func Md5(data []byte) string

Md5 Md5算法

func Panic added in v1.0.21

func Panic(args ...interface{})

func Panicf added in v1.0.21

func Panicf(template string, args ...interface{})

func Red added in v1.0.3

func Red(format string, args ...interface{})

Red 错误输出

func RedFatal added in v1.0.8

func RedFatal(format string, args ...interface{})

RedFatal 致命错误输出

func Sha1 added in v1.0.3

func Sha1(data []byte) string

Sha1 Sha1算法

func SmallCamels added in v1.0.20

func SmallCamels(sep byte, data string) string

SmallCamels 转换为小驼峰

func Warn added in v1.0.21

func Warn(args ...interface{})

func Warnf added in v1.0.21

func Warnf(template string, args ...interface{})

func YamlDecode

func YamlDecode(data []byte, v interface{}) (err error)

YamlDecode ---

func YamlDecodeFile

func YamlDecodeFile(filePath string, v interface{}) (err error)

YamlDecodeFile ---

func YamlEncode

func YamlEncode(v interface{}) (data []byte, err error)

YamlEncode ---

func Yellow added in v1.0.3

func Yellow(format string, args ...interface{})

Yellow 警告输出

func ZapDebug added in v1.0.21

func ZapDebug(msg string, fields ...zap.Field)

func ZapError added in v1.0.21

func ZapError(msg string, fields ...zap.Field)

func ZapFatal added in v1.0.21

func ZapFatal(msg string, fields ...zap.Field)

func ZapInfo added in v1.0.21

func ZapInfo(msg string, fields ...zap.Field)

func ZapPanic added in v1.0.21

func ZapPanic(msg string, fields ...zap.Field)

func ZapSync added in v1.0.21

func ZapSync() error

func ZapWarn added in v1.0.21

func ZapWarn(msg string, fields ...zap.Field)

Types

type Aes added in v1.0.3

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

Aes Aes加密

func NewAes added in v1.0.3

func NewAes(key string, iv string) (a *Aes, err error)

NewAes 实例化Aes

func (*Aes) CbcDecrypt added in v1.0.3

func (a *Aes) CbcDecrypt(secretData []byte) (data []byte, err error)

CbcDecrypt cbc解密

func (*Aes) CbcEncrypt added in v1.0.3

func (a *Aes) CbcEncrypt(plain []byte) (secretData []byte)

CbcEncrypt cbc加密

type Bitmap

type Bitmap interface {
	// HasBit 是否有值
	HasBit() bool
	// BitCount 值的数量
	BitCount() int
	// Exists 标签是否存在
	Exists(tag int) (exists bool)
	// AddTag 添加标签
	AddTag(tag int) Bitmap
	// DelTag 删除标签
	DelTag(tag int) Bitmap
	// Data 原始数据
	Data() (data []byte)
	// SprinfBinary 二进制打印
	SprinfBinary() string
}

Bitmap bitmap数据结构

func NewBitmap

func NewBitmap(data []byte) Bitmap

type CanHash

type CanHash interface {
	// HashCode 计算hash值
	HashCode() (hashValue uint32)
}

CanHash hash接口

type GetMachineId added in v1.0.8

type GetMachineId func() (id int64, err error)

GetMachineId 获取机器Id

func GetMachineIdByEnv added in v1.0.8

func GetMachineIdByEnv(key string) GetMachineId

GetMachineIdByEnv 根据环境变量获取机器Id

func GetMachineIdByIp added in v1.0.8

func GetMachineIdByIp() GetMachineId

GetMachineIdByIp 根据Ip获取机器Id

type HashRing added in v1.0.9

type HashRing interface {
	// Store 存储servers
	Store(servers ...CanHash)
	// Get 获取server
	Get(key interface{}) (server CanHash, err error)
	// Index 根据index获取server
	Index(index int) (server CanHash, err error)
	// Add 添加server
	Add(server CanHash)
	// Remove 移除server
	Remove(server CanHash)
	// Length 获取servers长度
	Length() int
	// Range 遍历servers
	Range(handler func(index int, server CanHash, hitCount uint64) (handled bool))
}

HashRing Hash环

func NewHashRing added in v1.0.9

func NewHashRing(servers ...CanHash) HashRing

type HashSet

type HashSet interface {
	// Add 添加元素
	Add(items ...interface{}) (newNum int)
	// Delete 删除元素
	Delete(items ...interface{}) (delNum int)
	// Exists 元素是否存在
	Exists(item interface{}) (exists bool)
	// Length 长度
	Length() (length int)
}

HashSet 接口

func NewHashSet

func NewHashSet(size int) HashSet

NewHashSet 实例化HashSet

type I18n added in v1.0.3

type I18n interface {
	// T 翻译
	T(key, language string) (msg string)
}

I18n 多语言接口

func NewI18n added in v1.0.3

func NewI18n(defaultLanguage string, msgMap map[string]map[string]string) (i I18n, err error)

NewI18n 实例化多语言

func NewI18nFromJson added in v1.0.3

func NewI18nFromJson(defaultLanguage string, dir string) (i I18n, err error)

NewI18nFromJson 从Json文件配置实例化多语言

func NewI18nFromYaml added in v1.0.3

func NewI18nFromYaml(defaultLanguage string, dir string) (i I18n, err error)

NewI18nFromYaml 从Yaml文件配置实例化多语言

type Logger added in v1.0.21

type Logger interface {
	Debug(args ...interface{})
	Info(args ...interface{})
	Warn(args ...interface{})
	Error(args ...interface{})
	Panic(args ...interface{})
	Fatal(args ...interface{})
	Debugf(template string, args ...interface{})
	Infof(template string, args ...interface{})
	Warnf(template string, args ...interface{})
	Errorf(template string, args ...interface{})
	Panicf(template string, args ...interface{})
	Fatalf(template string, args ...interface{})
}

Logger 日志接口

type Msg

type Msg struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

Msg 通用消息

func (*Msg) Json

func (m *Msg) Json() (msg []byte)

Json 转换为Json

type Node added in v1.0.9

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

type NodeList added in v1.0.9

type NodeList []Node

func (NodeList) Len added in v1.0.9

func (n NodeList) Len() int

func (NodeList) Less added in v1.0.9

func (n NodeList) Less(i, j int) bool

func (NodeList) Swap added in v1.0.9

func (n NodeList) Swap(i, j int)

type Rsa added in v1.0.3

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

Rsa Rsa

func NewRsa added in v1.0.3

func NewRsa(publicKey, privateKey string) (r *Rsa, err error)

NewRsa 实例化Rsa

func NewRsaWithPkcs1 added in v1.0.6

func NewRsaWithPkcs1(publicKey, privateKey string) (r *Rsa, err error)

NewRsaWithPkcs1 pkcs1实例化Rsa

func NewRsaWithPkcs8 added in v1.0.6

func NewRsaWithPkcs8(publicKey, privateKey string) (r *Rsa, err error)

NewRsaWithPkcs8 pkcs8实例化Rsa

func (*Rsa) Decrypt added in v1.0.3

func (r *Rsa) Decrypt(secretData []byte) ([]byte, error)

Decrypt 解密

func (*Rsa) Encrypt added in v1.0.3

func (r *Rsa) Encrypt(data []byte) ([]byte, error)

Encrypt 加密

func (*Rsa) Sign added in v1.0.3

func (r *Rsa) Sign(data []byte, algorithmSign crypto.Hash) ([]byte, error)

Sign 数据签名

func (*Rsa) Verify added in v1.0.3

func (r *Rsa) Verify(data []byte, sign []byte, algorithmSign crypto.Hash) bool

Verify 数据验签

type ShardMap

type ShardMap interface {
	// Set 存储
	Set(key interface{}, value interface{})
	// Get 获取
	Get(key interface{}) (value interface{}, exists bool)
	// Exists 是否存在
	Exists(key interface{}) (exists bool)
	// Delete 删除
	Delete(keys ...interface{})
	// Length 长度
	Length() int64
}

ShardMap 分片Map

func NewShardMap

func NewShardMap() ShardMap

NewShardMap 实例化ShardMap

type SnowFlake added in v1.0.8

type SnowFlake interface {
	// Id 生成id
	Id(logicId uint8) (int64, error)
	// ZapInfo 根据id获取信息
	Info(id int64) (timestamp int64, machineId uint8, logicId uint8, index int16)
}

SnowFlake 雪花算法接口,1位0,41位毫秒时间戳,8位机器码,2位业务码,12位递增值

func NewSF added in v1.0.8

func NewSF(mode uint8, id uint8, timeStampBegin int64) (sfl SnowFlake, err error)

NewSF 实例化雪花算法

func NewSFByIp added in v1.0.8

func NewSFByIp(mode uint8, timeStampBegin int64) (sfl SnowFlake, err error)

NewSFByIp ip方式实例化雪花算法

func NewSFByMachineFunc added in v1.0.8

func NewSFByMachineFunc(mode uint8, machindFunc GetMachineId, timeStampBegin int64) (sfl SnowFlake, err error)

NewSFByMachineFunc GetMachineId方式实例化雪花算法

Directories

Path Synopsis
examples
i18n command
logger command

Jump to

Keyboard shortcuts

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