🖲

字符|编码

 
import( "github.com/sohaha/zlsgo/zstring" )

🗃️ 函数列表

// bash64 解码 func Base64Decode(data []byte) (value []byte, err error) func Base64DecodeString(data string) (value string, err error) // bash64 编码 func Base64Encode(value []byte) []byte func Base64EncodeString(value string) string // 字符加密(CBC 模式) func AesEncrypt(plainText []byte, key string, iv ...string) ([]byte, error) func AesEncryptString(plainText string, key string, iv ...string) (string, error) // 字符解密(CBC 模式) func AesDecrypt(cipherText []byte, key string, iv ...string) ([]byte, error) func AesDecryptString(cipherText string, key string, iv ...string) (string, error) // RSA 密钥对生成 func GenRSAKey(bits ...int) (prvkey, pubkey []byte, err error) // RSA 加密 可通过 bits 来实现分段加解密来处理大内容 func RSAEnCrypt(plainText []byte, publicKey []byte, bits ...int) ([]byte, error) func RSAKeyEncrypt(plainText []byte, publicKey *rsa.PublicKey, bits ...int) ([]byte, error) func RSAEncryptString(plainText string, publicKey string, bits ...int) (string, error) // RSA 解密 func RSADecrypt(cipherText []byte, privateKey []byte, bits ...int) ([]byte, error) func RSAKeyDecrypt(cipherText []byte, privateKey *rsa.PrivateKey, bits ...int) ([]byte, error) func RSADecryptString(cipherText string, privateKey string, bits ...int) (string, error) // 序列化 func Serialize(value interface{}) ([]byte, error) // 反序列化 func UnSerialize(valueBytes []byte, registers ...interface{}) (value interface{}, err error) // md5 编码 func Md5(str string) string // 文件 md5 编码 func Md5File(path string) (encrypt string, err error) // 随机字符串 func Rand(n int, tpl ...string) string // 随机整数 func RandInt(min int, max int) int // 字符串切割 func Substr(str string, start int, length ...int) string // 字符串填充 func Pad(str string, length int, padStr string, padType int) string // 字符串截取 func Substr(str string, start int, length ...int) string // URL 字符串解码,UrlRawDecode 会把+转成%20 func UrlDecode(str string) (string, error) func UrlRawDecode(str string) (string, error) // URL 字符串编码 func UrlEncode(str string) string func UrlRawEncode(str string) string // 正则提取 func RegexExtract(pattern string, str string) ([]string, error) func RegexExtractAll(pattern string, str string, count ...int) ([][]string, error) // 正则查找 func RegexFind(pattern string, str string, count int) [][]int // 正则匹配 func RegexMatch(pattern string, str string) bool // 正则替换 func RegexReplace(pattern string, str, repl string) (string, error) func RegexReplaceFunc(pattern string, str string, repl func(string) string) (string, error) // 驼峰转蛇形 func CamelCaseToSnakeCase(str string, delimiter ...string) string // 驼峰转蛇形 func SnakeCaseToCamelCase(str string, ucfirst bool, delimiter ...string) string // 首字母是否小写 func IsLcfirst(str string) bool // 首字母是否大写 func IsUcfirst(str string) bool // 转小写 func Lcfirst(str string) string // 转大写 func Ucfirst(str string) string // 字符串长度 func Len(str string) int // 字符串匹配 func Match(str, pattern string) bool // 创建 strings.Builder func Buffer(size ...int) *strings.Builder // 字符串转字节 func String2Bytes(s string) []byte // 字节转字符串 func Bytes2String(b []byte) string // 清除 BOM 头 func TrimBOM(fileBytes []byte) []byte // 生成唯一 ID func UniqueID(workerid ...int64) string // 生成 UUID func UUID(workerid ...int64) int64 // 解析 UUID func ParseID(id int64) (t time.Time, ts int64, workerId int64, seq int64) // 清除 HTML 代码防止 xss 攻击 func XSSClean(str string) string // 随机字符串 func Rand(n int, tpl ...string) string // 随机数字 func RandInt(min int, max int) int // 权重随机 func WeightedRand(choices map[interface{}]uint32) (interface{}, error)

❄️ 唯一 ID

// 真.随机 id := zstring.UniqueID() // 雪花算法 id := zstring.UUID() // or wr, err := zstring.NewIDWorker(1) id, err := wr.ID()
 

🎩 字符串模板

// 初始化一个模板对象 tmpl, err := zstring.NewTemplate("hello {name}", "{", "}") // 使用 GetBuff 提高性能 w := zutil.GetBuff() defer zutil.PutBuff(w) // 执行替换占位符 _, err = tmpl.Process(w, func(w io.Writer, tag string) (int, error) { // tag 为对应占位字符串 return w.Write([]byte("Go")) }) zlog.Debug(w.String())