Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
jhonnli committed Oct 11, 2019
0 parents commit 9bf303a
Show file tree
Hide file tree
Showing 47 changed files with 5,777 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea/
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#golibs

go通用库
115 changes: 115 additions & 0 deletions bytes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package golibs

import (
"bytes"
"compress/zlib"
"encoding/binary"
"io/ioutil"
"strconv"
)

func ZlibZipBytes(input []byte) ([]byte, error) {
var buf bytes.Buffer
compressor, err := zlib.NewWriterLevel(&buf, zlib.BestCompression)
if err != nil {
return nil, err
}
compressor.Write(input)
compressor.Close()
return buf.Bytes(), nil
}

func ZlibUnzipBytes(input []byte) ([]byte, error) {
b := bytes.NewReader(input)
r, err := zlib.NewReader(b)
defer r.Close()
if err != nil {
return nil, err
}
data, _ := ioutil.ReadAll(r)
return data, nil
}

// 混淆[]byte
func ConfusedTwo(sourceBytes []byte) []byte {
var confusedBytes []byte = make([]byte, len(sourceBytes))
idx := 0
for index := 0; index < len(sourceBytes); index++ {
if index%2 == 0 {
confusedBytes[idx] = byte(255 - sourceBytes[index])
idx++
}
}
for index := 0; index < len(sourceBytes); index++ {
if index%2 == 1 {
confusedBytes[idx] = byte(255 - sourceBytes[index])
idx++
}
}
return confusedBytes
}

// 反混淆[]byte
func UnConfusedTwo(confusedBytes []byte) []byte {
loopCount := len(confusedBytes)
count := loopCount / 2
if loopCount%2 == 1 {
count = loopCount/2 + 1
}
beforeBytes := confusedBytes[0:count]
afterBytes := confusedBytes[count:]
var unConfusedBytes []byte = make([]byte, loopCount)
beforeIndex := 0
afterIndex := 0
for index := 0; index < loopCount; index++ {
if index%2 == 0 {
if beforeIndex >= len(beforeBytes) {
continue
}
unConfusedBytes[index] = 255 - beforeBytes[beforeIndex]
beforeIndex++
} else {
if afterIndex >= len(afterBytes) {
continue
}
unConfusedBytes[index] = 255 - afterBytes[afterIndex]
afterIndex++
}
}
return unConfusedBytes
}

//反转[]byte
func ReversalBytes(source []byte) []byte {
builder := make([]byte, len(source))
for i, v := range source {
builder[i] = 255 - v
}
return builder
}

//把十六进制字符串转成字节数组
func HexStringToBytes(hex_str string) []byte {
len := len(hex_str) / 2
byte_array := make([]byte, len)
for index := 0; index < len; index++ {
ina, _ := strconv.ParseInt(SubString(hex_str, index*2, 2), 16, 32)
byte_array[index] = byte(ina)
}
return byte_array
}

//整形转换成字节
func UIntToBytes(value uint32) []byte {
arr := []byte{0, 0, 0, 0}
arr[3] = byte((value >> 24) & 0xff)
arr[2] = byte((value >> 16) & 0xff)
arr[1] = byte((value >> 8) & 0xff)
arr[0] = byte(value & 0xff)
return arr
}

//字节转换成整形
func BytesToUInt(b []byte) uint32 {
return binary.LittleEndian.Uint32(b)
}
73 changes: 73 additions & 0 deletions cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package golibs

import (
"sync"
"time"
)

var _cache sync.Map

func CacheSet(key string, value interface{}) {
_cache.Store(key, value)
}

func CacheGetInt(key string) (value int, ok bool) {
result, ok := _cache.Load(key)
if !ok {
return -1, false
}
return result.(int), true
}

func CacheGetInt64(key string) (value int64, ok bool) {
result, ok := _cache.Load(key)
if !ok {
return -1, false
}
return result.(int64), true
}

func CacheGetString(key string) (value string, ok bool) {
result, ok := _cache.Load(key)
if !ok {
return "", false
}
return result.(string), true
}

func CacheGetBool(key string) (value bool, ok bool) {
result, ok := _cache.Load(key)
if !ok {
return false, false
}
return result.(bool), true
}

func CacheGetTime(key string) (value time.Time, ok bool) {
result, ok := _cache.Load(key)
if !ok {
return time.Date(1970, 1, 1, 0, 0, 0, 0, time.Local), false
}
return result.(time.Time), true
}

func CacheGetByteArray(key string) (value []byte, ok bool) {
result, ok := _cache.Load(key)
if !ok {
return nil, false
}
return result.([]byte), true
}

func CacheDel(key string) {
_cache.Delete(key)
}

func CacheCount() int {
count := 0
_cache.Range(func(k, v interface{}) bool {
count++
return true
})
return count
}
7 changes: 7 additions & 0 deletions container/container.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Copyright 2017 gf Author(https://gitee.com/johng/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://gitee.com/johng/gf.

package container
Loading

0 comments on commit 9bf303a

Please sign in to comment.