gokits

package
v0.0.0-...-ed7c0e1 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2021 License: Apache-2.0 Imports: 26 Imported by: 0

README

Some golang toolkits

This is an open source project for commonly used toolkits for the Go programming language.

This package need >= go 1.3

Documentation

Index

Constants

View Source
const (
	// For use with functions that take an expiration time.
	NoExpiration time.Duration = -1
)

Variables

View Source
var (
	ErrAESTextSize = errors.New("ciphertext is not a multiple of the block size")
	ErrAESPadding  = errors.New("cipher padding size error")
	ErrCrc         = errors.New("factor crc is invalid")
)

Functions

func CmpPasswd

func CmpPasswd(passwd, salt, encrypted string) bool

compare the password

func Convert

func Convert(dst interface{}, val interface{}) error

Convert varies types of value to a certain type. Value can be type of string or json or whatever type which is convertable to the target type.

func Crc16

func Crc16(bs []byte) (crc uint16)

func FileExist

func FileExist(path string) bool

checks whether a file or directory exists. It returns false when the file or directory does not exist.

func FileSize

func FileSize(file string) (int64, error)

returns file size in bytes and possible error.

func GenHmacSha256

func GenHmacSha256(plaintext string, saltlen int) string

generate a hmac sha256 string

func GenPasswd

func GenPasswd(password string, saltlen int) (string, string)

generate a hmac sha256 string

func GetProcPwd

func GetProcPwd() string

returns the complete directory of the current execution file

func IfEmpty

func IfEmpty(a, b string) string

func IsChinese

func IsChinese(str string) bool

func IsChineseLetterNumUnline

func IsChineseLetterNumUnline(str string) bool

func IsDigit

func IsDigit(str string) bool

func IsDir

func IsDir(dir string) bool

returns true if given path is a directory, or returns false when it's a file or does not exist.

func IsEmail

func IsEmail(str string) bool

func IsEmailRFC

func IsEmailRFC(str string) bool

func IsFile

func IsFile(filePath string) bool

returns true if given path is a file, or returns false when it's a directory or does not exist.

func IsLetterNumUnline

func IsLetterNumUnline(str string) bool

func IsSliceContainsInt64

func IsSliceContainsInt64(sl []int64, i int64) bool

----------------------------------------------------------

func IsSliceContainsStr

func IsSliceContainsStr(sl []string, str string) bool

----------------------------------------------------------

func IsURL

func IsURL(str string) bool

func Kermit

func Kermit(byteArray []byte) uint16

func KermitStr

func KermitStr(input string) string

func Map2Field

func Map2Field(vals map[string]interface{}, dst interface{}, tag string) error

func Map2Struct

func Map2Struct(vals map[string]interface{}, dst interface{}) (err error)

func Map2StructTag

func Map2StructTag(vals map[string]interface{}, dst interface{}, tagName string) (err error)

func NewRand

func NewRand(len int) string

Create a rang string

func NewRandWithPrefix

func NewRandWithPrefix(prefix string, len int) string

func NewUUID

func NewUUID() string

NewUUID generates a new UUID based on version 4.

func NormRand

func NormRand(n int64) float64

标准正态分布随机整数,n为随机个数,从0开始

func RangeRand

func RangeRand(n int) int

生成规定范围内的整数 设置起始数字范围,0开始,n截止

func SliceInt64To

func SliceInt64To(sts []int64) []interface{}

----------------------------------------------------------

func SliceStrTo

func SliceStrTo(sts []string) []interface{}

----------------------------------------------------------

func Struct2Map

func Struct2Map(s interface{}) map[string]interface{}

func Struct2MapTag

func Struct2MapTag(s interface{}, tagName string) map[string]interface{}

Types

type Attachment

type Attachment struct {
	Filename string
	Data     []byte
	Inline   bool
}

type Cache

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

func NewCache

func NewCache(cleanupInterval time.Duration) *Cache

Return a new cache with a given cleanup interval. If the cleanup interval is less than one, expired items are not deleted from the cache before calling c.DeleteExpired().

func (Cache) Add

func (c Cache) Add(k string, x interface{}, d time.Duration) error

Add an item to the cache only if an item doesn't already exist for the given key, or if the existing item has expired. Returns an error otherwise.

func (Cache) Clear

func (c Cache) Clear()

Delete all items from the cache.

func (Cache) Delete

func (c Cache) Delete(k string)

Delete an item from the cache. Does nothing if the key is not in the cache.

func (Cache) DeleteExpired

func (c Cache) DeleteExpired()

Delete all expired items from the cache.

func (Cache) Get

func (c Cache) Get(k string) (interface{}, bool)

Get an item from the cache. Returns the item or nil, and a bool indicating whether the key was found.

func (Cache) ItemCount

func (c Cache) ItemCount() int

Returns the number of items in the cache. This may include items that have expired, but have not yet been cleaned up. Equivalent to len(c.Items()).

func (Cache) OnEvicted

func (c Cache) OnEvicted(f func(string, interface{}))

Sets an (optional) function that is called with the key and value when an item is evicted from the cache. (Including when it is deleted manually, but not when it is overwritten.) Set to nil to disable.

func (Cache) Replace

func (c Cache) Replace(k string, x interface{}, d time.Duration) error

Set a new value for the cache key only if it already exists, and the existing item hasn't expired. Returns an error otherwise.

func (Cache) Set

func (c Cache) Set(k string, x interface{}, d time.Duration)

Add an item to the cache, replacing any existing item. If it is -1 (NoExpiration), the item never expires.

func (Cache) UpdateExpiration

func (c Cache) UpdateExpiration(k string, Expiration int64) error

type Crypto

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

the crypto object

func NewCrypto

func NewCrypto(factor, crc string) (*Crypto, error)

func (*Crypto) Decrypt

func (c *Crypto) Decrypt(src []byte) ([]byte, error)

func (*Crypto) DecryptStr

func (c *Crypto) DecryptStr(scuritytext string) (string, error)

AES解密

func (*Crypto) Encrypt

func (c *Crypto) Encrypt(src []byte) ([]byte, error)

AES加密

func (*Crypto) EncryptStr

func (c *Crypto) EncryptStr(plaintext string) (string, error)

AES加密

func (*Crypto) Init

func (c *Crypto) Init(factor, crc string) error

type Item

type Item struct {
	Object     interface{}
	Expiration int64
}

func (Item) Expired

func (item Item) Expired() bool

Returns true if the item has expired.

type Message

type Message struct {
	From            string
	To              []string
	Cc              []string
	Bcc             []string
	Subject         string
	Body            string
	BodyContentType string
	Attachments     map[string]*Attachment
}

func NewHTMLMessage

func NewHTMLMessage(subject string, body string) *Message

NewMessage returns a new Message that can compose an HTML email with attachments

func NewMessage

func NewMessage(subject string, body string) *Message

NewMessage returns a new Message that can compose an email with attachments

func (*Message) Attach

func (m *Message) Attach(file string) error

func (*Message) Inline

func (m *Message) Inline(file string) error

func (*Message) Send

func (m *Message) Send(auth smtp.Auth) error

func (*Message) Tolist

func (m *Message) Tolist() []string

ToList returns all the recipients of the email

type RefVal

type RefVal reflect.Value

func (RefVal) IsEmpty

func (r RefVal) IsEmpty() bool

func (RefVal) ToString

func (r RefVal) ToString() string

Jump to

Keyboard shortcuts

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