mirror of
https://github.com/pyihe/go-pkg.git
synced 2025-10-06 16:36:50 +08:00
style(go-pkg): rename pkg
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
package bytes
|
||||
package bytepkg
|
||||
|
||||
import (
|
||||
"unsafe"
|
||||
@@ -22,8 +22,8 @@ func String(b []byte) string {
|
||||
return *(*string)(unsafe.Pointer(&b))
|
||||
}
|
||||
|
||||
// SliceEqual 判断两个字节切片每个元素是否相等
|
||||
func SliceEqual(a, b []byte) bool {
|
||||
// BytesEqual 判断两个字节切片每个元素是否相等
|
||||
func BytesEqual(a, b []byte) bool {
|
||||
aLen, bLen := len(a), len(b)
|
||||
if aLen != bLen {
|
||||
return false
|
||||
@@ -43,7 +43,7 @@ func SliceEqual(a, b []byte) bool {
|
||||
}
|
||||
|
||||
// Contain 判断字节切片b是否包含ele元素
|
||||
func Contain(ele byte, b []byte) bool {
|
||||
func Contain(b []byte, ele byte) bool {
|
||||
for _, v := range b {
|
||||
if v == ele {
|
||||
return true
|
||||
@@ -62,7 +62,7 @@ func Reverse(b []byte) {
|
||||
}
|
||||
|
||||
// Remove 从b中删除ele
|
||||
func Remove(ele byte, b []byte) {
|
||||
func Remove(b []byte, ele byte) {
|
||||
for i := range b {
|
||||
if ele == b[i] {
|
||||
b = append(b[:i], b[i+1:]...)
|
@@ -1,19 +1,24 @@
|
||||
package certs
|
||||
package certpkg
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"encoding/pem"
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
|
||||
"golang.org/x/crypto/pkcs12"
|
||||
)
|
||||
|
||||
func P12ToPem(p12Path string, password string) (*tls.Certificate, error) {
|
||||
p12, err := ioutil.ReadFile(p12Path)
|
||||
func P12ToPem(p12Path string, password string) (cert *tls.Certificate, err error) {
|
||||
if p12Path == "" {
|
||||
err = errors.New("empty path")
|
||||
return
|
||||
}
|
||||
data, err := ioutil.ReadFile(p12Path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
blocks, err := pkcs12.ToPEM(p12, password)
|
||||
blocks, err := pkcs12.ToPEM(data, password)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
@@ -1,4 +1,4 @@
|
||||
package clone
|
||||
package clonepkg
|
||||
|
||||
import "reflect"
|
||||
|
||||
@@ -47,6 +47,7 @@ func deepCopy(dst, src reflect.Value) {
|
||||
}
|
||||
}
|
||||
|
||||
// DeepCopy 深拷贝
|
||||
func DeepCopy(dst, src interface{}) {
|
||||
typeDst := reflect.TypeOf(dst)
|
||||
typeSrc := reflect.TypeOf(src)
|
||||
@@ -66,6 +67,7 @@ func DeepCopy(dst, src interface{}) {
|
||||
deepCopy(valueDst, valueSrc)
|
||||
}
|
||||
|
||||
// DeepClone 深克隆
|
||||
func DeepClone(v interface{}) interface{} {
|
||||
dst := reflect.New(reflect.TypeOf(v)).Elem()
|
||||
deepCopy(dst, reflect.ValueOf(v))
|
@@ -1,4 +1,4 @@
|
||||
package encrypts
|
||||
package cryptopkg
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
"golang.org/x/crypto/scrypt"
|
||||
)
|
||||
|
||||
//用于加密web的密码
|
||||
// BcEncryptPass 用于加密web的密码
|
||||
func BcEncryptPass(plainPass string) (string, error) {
|
||||
var encryptPass string
|
||||
data, err := bcrypt.GenerateFromPassword([]byte(plainPass), bcrypt.DefaultCost)
|
||||
@@ -18,7 +18,7 @@ func BcEncryptPass(plainPass string) (string, error) {
|
||||
return encryptPass, nil
|
||||
}
|
||||
|
||||
//比较密码是否匹配
|
||||
// BcComparePass 比较密码是否匹配
|
||||
func BcComparePass(hashPass, plainPass string) error {
|
||||
hashBytes, err := base64.StdEncoding.DecodeString(hashPass)
|
||||
if err != nil {
|
||||
@@ -27,7 +27,7 @@ func BcComparePass(hashPass, plainPass string) error {
|
||||
return bcrypt.CompareHashAndPassword(hashBytes, []byte(plainPass))
|
||||
}
|
||||
|
||||
//scrypt加密
|
||||
// ScryptPass scrypt加密
|
||||
func ScryptPass(plainPass, salt string) (string, error) {
|
||||
data, err := scrypt.Key([]byte(plainPass), []byte(salt), 1<<15, 8, 1, 32)
|
||||
if err != nil {
|
@@ -1,99 +0,0 @@
|
||||
package encoding
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/gob"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
"github.com/golang/protobuf/proto"
|
||||
"github.com/vmihailenco/msgpack/v5"
|
||||
)
|
||||
|
||||
type Encoding interface {
|
||||
Marshal(v interface{}) ([]byte, error)
|
||||
Unmarshal(data []byte, v interface{}) error
|
||||
}
|
||||
|
||||
// JSONEncoding json格式
|
||||
func JSONEncoding() Encoding {
|
||||
return &jsonEncoding{}
|
||||
}
|
||||
|
||||
// GobEncoding
|
||||
func GobEncoding() Encoding {
|
||||
return &gobEncoding{}
|
||||
}
|
||||
|
||||
// MsgpackEncoding
|
||||
func MsgpackEncoding() Encoding {
|
||||
return &msgpackEncoding{}
|
||||
}
|
||||
|
||||
// ProtoEncoding
|
||||
func ProtoEncoding() Encoding {
|
||||
return &protoEncoding{}
|
||||
}
|
||||
|
||||
/***JSON Marshaler***/
|
||||
type jsonEncoding struct{}
|
||||
|
||||
// Marshal
|
||||
func (j *jsonEncoding) Marshal(v interface{}) ([]byte, error) {
|
||||
return json.Marshal(v)
|
||||
}
|
||||
|
||||
// Unmarshal
|
||||
func (j *jsonEncoding) Unmarshal(data []byte, v interface{}) error {
|
||||
return json.Unmarshal(data, v)
|
||||
}
|
||||
|
||||
/***gob Marshaler***/
|
||||
type gobEncoding struct{}
|
||||
|
||||
// Marshal
|
||||
func (g *gobEncoding) Marshal(v interface{}) ([]byte, error) {
|
||||
var b = bytes.NewBuffer(nil)
|
||||
err := gob.NewEncoder(b).Encode(v)
|
||||
return b.Bytes(), err
|
||||
}
|
||||
|
||||
// Unmarshal
|
||||
func (g *gobEncoding) Unmarshal(data []byte, v interface{}) error {
|
||||
return gob.NewDecoder(bytes.NewReader(data)).Decode(v)
|
||||
}
|
||||
|
||||
/***msgpack Marshaler***/
|
||||
type msgpackEncoding struct{}
|
||||
|
||||
// Marshal
|
||||
func (m *msgpackEncoding) Marshal(v interface{}) ([]byte, error) {
|
||||
return msgpack.Marshal(v)
|
||||
}
|
||||
|
||||
// Unmarshal
|
||||
func (m *msgpackEncoding) Unmarshal(data []byte, v interface{}) error {
|
||||
return msgpack.Unmarshal(data, v)
|
||||
}
|
||||
|
||||
/***proto Encoding***/
|
||||
type protoEncoding struct{}
|
||||
|
||||
// Marshal
|
||||
func (p *protoEncoding) Marshal(v interface{}) ([]byte, error) {
|
||||
m, ok := v.(proto.Message)
|
||||
if !ok {
|
||||
return nil, errors.New("not proto.Message")
|
||||
}
|
||||
|
||||
return proto.Marshal(m)
|
||||
}
|
||||
|
||||
// Unmarshal
|
||||
func (p *protoEncoding) Unmarshal(data []byte, v interface{}) error {
|
||||
m, ok := v.(proto.Message)
|
||||
if !ok {
|
||||
return errors.New("not proto.Message")
|
||||
}
|
||||
return proto.Unmarshal(data, m)
|
||||
}
|
@@ -1,35 +0,0 @@
|
||||
package errors
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
const (
|
||||
DefaultErrCode ErrorCode = 100000
|
||||
)
|
||||
|
||||
type ErrorCode int64
|
||||
|
||||
func NewErrCode(code int64) ErrorCode {
|
||||
return ErrorCode(code)
|
||||
}
|
||||
|
||||
func (ec ErrorCode) Int() int {
|
||||
return int(ec)
|
||||
}
|
||||
|
||||
func (ec ErrorCode) Int64() int64 {
|
||||
return int64(ec)
|
||||
}
|
||||
|
||||
func (ec ErrorCode) Int32() int32 {
|
||||
return int32(ec)
|
||||
}
|
||||
|
||||
func (ec ErrorCode) ToString() string {
|
||||
return fmt.Sprintf("%d", ec)
|
||||
}
|
||||
|
||||
func (ec ErrorCode) Equal(target ErrorCode) bool {
|
||||
return ec == target
|
||||
}
|
@@ -1,83 +0,0 @@
|
||||
package errors
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type Error interface {
|
||||
Error() string // 包含code和msg
|
||||
Code() ErrorCode // 错误码
|
||||
Desc() string // 错误描述
|
||||
Data() interface{} // 附加信息
|
||||
WithData(interface{}) // 给错误添加附加信息
|
||||
Is(target error) bool
|
||||
As(target interface{}) bool
|
||||
}
|
||||
|
||||
type _err struct {
|
||||
data interface{} // 如果需要包含特定的数据
|
||||
code ErrorCode // 错误码
|
||||
msg string // 错误描述
|
||||
}
|
||||
|
||||
func New(err string) Error {
|
||||
return &_err{
|
||||
code: DefaultErrCode,
|
||||
msg: err,
|
||||
}
|
||||
}
|
||||
|
||||
func NewWithCode(err string, code ErrorCode) Error {
|
||||
return &_err{
|
||||
code: code,
|
||||
msg: err,
|
||||
}
|
||||
}
|
||||
|
||||
func (e *_err) Error() string {
|
||||
if e == nil {
|
||||
return ""
|
||||
}
|
||||
return fmt.Sprintf("code: %d err: %s", e.code, e.msg)
|
||||
}
|
||||
|
||||
func (e *_err) Code() ErrorCode {
|
||||
if e == nil {
|
||||
return 0
|
||||
}
|
||||
return e.code
|
||||
}
|
||||
|
||||
func (e *_err) Desc() string {
|
||||
if e == nil {
|
||||
return ""
|
||||
}
|
||||
return e.msg
|
||||
}
|
||||
|
||||
func (e *_err) Data() interface{} {
|
||||
if e == nil {
|
||||
return nil
|
||||
}
|
||||
return e.data
|
||||
}
|
||||
|
||||
func (e *_err) WithData(data interface{}) {
|
||||
if data == nil || e == nil {
|
||||
return
|
||||
}
|
||||
e.data = data
|
||||
}
|
||||
|
||||
func (e *_err) Is(target error) bool {
|
||||
return errors.Is(e, target)
|
||||
}
|
||||
|
||||
func (e *_err) As(target interface{}) bool {
|
||||
return errors.As(e, target)
|
||||
}
|
||||
|
||||
func (e *_err) Unwrap() error {
|
||||
return errors.Unwrap(e)
|
||||
}
|
24
errpkg/error.go
Normal file
24
errpkg/error.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package errpkg
|
||||
|
||||
type Error struct {
|
||||
err string
|
||||
code int32
|
||||
}
|
||||
|
||||
func New(err string, codes ...int32) error {
|
||||
e := &Error{
|
||||
err: err,
|
||||
}
|
||||
if len(codes) > 0 {
|
||||
e.code = codes[0]
|
||||
}
|
||||
return e
|
||||
}
|
||||
|
||||
func (e *Error) Error() (err string) {
|
||||
return e.err
|
||||
}
|
||||
|
||||
func (e *Error) Code() int32 {
|
||||
return e.code
|
||||
}
|
@@ -1,4 +1,4 @@
|
||||
package files
|
||||
package filepkg
|
||||
|
||||
import "os"
|
||||
|
@@ -1,4 +1,4 @@
|
||||
package https
|
||||
package httppkg
|
||||
|
||||
import (
|
||||
"io"
|
||||
@@ -6,16 +6,16 @@ import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/pyihe/go-pkg/encoding"
|
||||
"github.com/pyihe/go-pkg/errors"
|
||||
"github.com/pyihe/go-pkg/errpkg"
|
||||
"github.com/pyihe/go-pkg/serialize"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrInvalidUrl = errors.New("url must start with 'http'")
|
||||
ErrInvalidEncoder = errors.New("invalid encoder")
|
||||
ErrInvalidUrl = errpkg.New("url must start with 'http'")
|
||||
ErrInvalidEncoder = errpkg.New("invalid encoder")
|
||||
)
|
||||
|
||||
// Get
|
||||
// Get 发起http get请求
|
||||
func Get(client *http.Client, url string) ([]byte, error) {
|
||||
if url == "" || !strings.HasPrefix(url, "http") {
|
||||
return nil, ErrInvalidUrl
|
||||
@@ -33,17 +33,17 @@ func Get(client *http.Client, url string) ([]byte, error) {
|
||||
return ioutil.ReadAll(response.Body)
|
||||
}
|
||||
|
||||
// GetWithObj
|
||||
func GetWithObj(client *http.Client, url string, encoder encoding.Encoding, obj interface{}) error {
|
||||
// GetWithObj 发起get请求,并将结果反序列化到指定obj中
|
||||
func GetWithObj(client *http.Client, url string, encoder serialize.Serializer, obj interface{}) error {
|
||||
data, err := Get(client, url)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = encoder.Unmarshal(data, obj)
|
||||
err = encoder.Decode(data, obj)
|
||||
return err
|
||||
}
|
||||
|
||||
// Post
|
||||
// Post 发起POST请求
|
||||
func Post(client *http.Client, url string, contentType string, body io.Reader) ([]byte, error) {
|
||||
if url == "" || !strings.HasPrefix(url, "http") {
|
||||
return nil, ErrInvalidUrl
|
||||
@@ -60,12 +60,11 @@ func Post(client *http.Client, url string, contentType string, body io.Reader) (
|
||||
return ioutil.ReadAll(response.Body)
|
||||
}
|
||||
|
||||
// PostWithObj
|
||||
func PostWithObj(client *http.Client, url string, contentType string, body io.Reader, encoder encoding.Encoding, v interface{}) error {
|
||||
// PostWithObj 发起POST请求并将结果发序列化到指定obj
|
||||
func PostWithObj(client *http.Client, url string, contentType string, body io.Reader, encoder serialize.Serializer, v interface{}) error {
|
||||
data, err := Post(client, url, contentType, body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return encoder.Unmarshal(data, v)
|
||||
return encoder.Decode(data, v)
|
||||
}
|
@@ -1,4 +1,4 @@
|
||||
package https
|
||||
package httppkg
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
@@ -7,29 +7,20 @@ import (
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
|
||||
"github.com/pyihe/go-pkg/encoding"
|
||||
"github.com/pyihe/go-pkg/serialize"
|
||||
)
|
||||
|
||||
type TLSClient interface {
|
||||
Get(url string) ([]byte, error)
|
||||
GetWithObj(url string, obj interface{}) error
|
||||
Post(url, contentType string, body io.Reader) ([]byte, error)
|
||||
PostWithObj(url, contentType string, body io.Reader, obj interface{}) error
|
||||
ListenAndServeTLS(serverCrt, serverKey string) error
|
||||
}
|
||||
|
||||
// httpClient
|
||||
type httpClient struct {
|
||||
type TLSClient struct {
|
||||
client *http.Client
|
||||
ca *x509.CertPool
|
||||
certificate tls.Certificate
|
||||
encoder encoding.Encoding
|
||||
encoder serialize.Serializer
|
||||
}
|
||||
|
||||
// NewTLSClient
|
||||
func NewTLSClient(caCrt, clientCrt, clientKey string, encoder encoding.Encoding) {
|
||||
// NewTLSClient 创建TLS客户端
|
||||
func NewTLSClient(caCrt, clientCrt, clientKey string, encoder serialize.Serializer) *TLSClient {
|
||||
var err error
|
||||
c := &httpClient{
|
||||
c := &TLSClient{
|
||||
encoder: encoder,
|
||||
}
|
||||
|
||||
@@ -51,11 +42,11 @@ func NewTLSClient(caCrt, clientCrt, clientKey string, encoder encoding.Encoding)
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
// ListenAndServeTLS
|
||||
func (h *httpClient) ListenAndServeTLS(serverCrt, serverKey string) error {
|
||||
// ListenAndServeTLS start
|
||||
func (h *TLSClient) ListenAndServeTLS(serverCrt, serverKey string) error {
|
||||
server := &http.Server{
|
||||
Addr: ":443",
|
||||
TLSConfig: &tls.Config{
|
||||
@@ -66,8 +57,8 @@ func (h *httpClient) ListenAndServeTLS(serverCrt, serverKey string) error {
|
||||
return server.ListenAndServeTLS(serverCrt, serverKey)
|
||||
}
|
||||
|
||||
// Get
|
||||
func (h *httpClient) Get(url string) ([]byte, error) {
|
||||
// Get GET请求
|
||||
func (h *TLSClient) Get(url string) ([]byte, error) {
|
||||
response, err := h.client.Get(url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -78,8 +69,8 @@ func (h *httpClient) Get(url string) ([]byte, error) {
|
||||
return ioutil.ReadAll(response.Body)
|
||||
}
|
||||
|
||||
// GetWithObj
|
||||
func (h *httpClient) GetWithObj(url string, obj interface{}) error {
|
||||
// GetWithObj GET并反序列化
|
||||
func (h *TLSClient) GetWithObj(url string, obj interface{}) error {
|
||||
data, err := h.Get(url)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -88,11 +79,11 @@ func (h *httpClient) GetWithObj(url string, obj interface{}) error {
|
||||
return ErrInvalidEncoder
|
||||
}
|
||||
|
||||
return h.encoder.Unmarshal(data, obj)
|
||||
return h.encoder.Decode(data, obj)
|
||||
}
|
||||
|
||||
// Post
|
||||
func (h *httpClient) Post(url, contentType string, body io.Reader) ([]byte, error) {
|
||||
// Post POST请求
|
||||
func (h *TLSClient) Post(url, contentType string, body io.Reader) ([]byte, error) {
|
||||
response, err := h.client.Post(url, contentType, body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -102,8 +93,8 @@ func (h *httpClient) Post(url, contentType string, body io.Reader) ([]byte, erro
|
||||
return ioutil.ReadAll(response.Body)
|
||||
}
|
||||
|
||||
// PostWithObj
|
||||
func (h *httpClient) PostWithObj(url, contentType string, body io.Reader, obj interface{}) error {
|
||||
// PostWithObj POST请求并反序列化
|
||||
func (h *TLSClient) PostWithObj(url, contentType string, body io.Reader, obj interface{}) error {
|
||||
data, err := h.Post(url, contentType, body)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -111,10 +102,9 @@ func (h *httpClient) PostWithObj(url, contentType string, body io.Reader, obj in
|
||||
if h.encoder == nil {
|
||||
return ErrInvalidEncoder
|
||||
}
|
||||
return h.encoder.Unmarshal(data, obj)
|
||||
return h.encoder.Decode(data, obj)
|
||||
}
|
||||
|
||||
// loadCA 加载身份证书
|
||||
func loadCA(caFile string) (*x509.CertPool, error) {
|
||||
p := x509.NewCertPool()
|
||||
ca, err := ioutil.ReadFile(caFile)
|
85
listpkg/list_array.go
Normal file
85
listpkg/list_array.go
Normal file
@@ -0,0 +1,85 @@
|
||||
package listpkg
|
||||
|
||||
import "sync"
|
||||
|
||||
const (
|
||||
defaultCap = 128
|
||||
)
|
||||
|
||||
// ArrayList 切片实现的队列
|
||||
type ArrayList struct {
|
||||
mu *sync.Mutex
|
||||
data []interface{}
|
||||
}
|
||||
|
||||
func NewArrayList() *ArrayList {
|
||||
return &ArrayList{
|
||||
mu: &sync.Mutex{},
|
||||
data: make([]interface{}, 0, defaultCap),
|
||||
}
|
||||
}
|
||||
|
||||
// LPush 从队首添加元素
|
||||
func (array *ArrayList) LPush(datas ...interface{}) {
|
||||
array.mu.Lock()
|
||||
defer array.mu.Unlock()
|
||||
|
||||
n := len(datas)
|
||||
if n == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
length := len(array.data)
|
||||
capacity := cap(array.data)
|
||||
switch length == capacity {
|
||||
case true: // 如果已经放满了,需要扩容
|
||||
na := make([]interface{}, length+1, capacity+capacity/2)
|
||||
copy(na[1:], array.data)
|
||||
na[0] = datas
|
||||
array.data = na
|
||||
default: // 没有放满的话
|
||||
array.data = append(array.data, nil)
|
||||
for i := length; i > 0; i-- {
|
||||
array.data[i] = array.data[i-1]
|
||||
}
|
||||
array.data[0] = datas
|
||||
}
|
||||
}
|
||||
|
||||
// LPop 从队首取数据
|
||||
func (array *ArrayList) LPop() (data interface{}) {
|
||||
array.mu.Lock()
|
||||
defer array.mu.Unlock()
|
||||
if len(array.data) == 0 {
|
||||
return
|
||||
}
|
||||
data = array.data[0]
|
||||
array.data = array.data[1:]
|
||||
return
|
||||
}
|
||||
|
||||
// RPush 从队尾添加元素
|
||||
func (array *ArrayList) RPush(data interface{}) {
|
||||
array.mu.Lock()
|
||||
array.data = append(array.data, data)
|
||||
array.mu.Unlock()
|
||||
}
|
||||
|
||||
// RPop 从队尾取出并删除元素
|
||||
func (array *ArrayList) RPop() (data interface{}) {
|
||||
array.mu.Lock()
|
||||
defer array.mu.Unlock()
|
||||
|
||||
n := len(array.data)
|
||||
if n == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
data = array.data[n-1]
|
||||
array.data = array.data[:n-1]
|
||||
return data
|
||||
}
|
||||
|
||||
func (array *ArrayList) Data() []interface{} {
|
||||
return array.data
|
||||
}
|
402
listpkg/list_hash.go
Normal file
402
listpkg/list_hash.go
Normal file
@@ -0,0 +1,402 @@
|
||||
package listpkg
|
||||
|
||||
import (
|
||||
"container/list"
|
||||
"reflect"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type HashList struct {
|
||||
mu *sync.RWMutex
|
||||
data map[interface{}]*list.List
|
||||
}
|
||||
|
||||
func NewHList() *HashList {
|
||||
return &HashList{
|
||||
mu: &sync.RWMutex{},
|
||||
data: make(map[interface{}]*list.List),
|
||||
}
|
||||
}
|
||||
|
||||
// LPush 向队首添加数据
|
||||
func (hl *HashList) LPush(key interface{}, data ...interface{}) (n int) {
|
||||
hl.mu.Lock()
|
||||
n = hl.push(true, key, data...)
|
||||
hl.mu.Unlock()
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// LPop 从队首取出数据
|
||||
func (hl *HashList) LPop(key interface{}) (data interface{}) {
|
||||
hl.mu.Lock()
|
||||
data = hl.pop(true, key)
|
||||
hl.mu.Unlock()
|
||||
return
|
||||
}
|
||||
|
||||
// RPush 向队尾添加数据
|
||||
func (hl *HashList) RPush(key interface{}, data ...interface{}) (n int) {
|
||||
hl.mu.Lock()
|
||||
n = hl.push(false, key, data...)
|
||||
hl.mu.Unlock()
|
||||
return
|
||||
}
|
||||
|
||||
// RPop 从队尾取数据
|
||||
func (hl *HashList) RPop(key interface{}) (data interface{}) {
|
||||
hl.mu.Lock()
|
||||
data = hl.pop(false, key)
|
||||
hl.mu.Unlock()
|
||||
return
|
||||
}
|
||||
|
||||
// Index 根据索引查找数据
|
||||
func (hl *HashList) Index(key interface{}, idx int) (data interface{}) {
|
||||
hl.mu.RLock()
|
||||
defer hl.mu.RUnlock()
|
||||
|
||||
record := hl.data[key]
|
||||
ok, newIndex := validIndex(record, idx)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
if element := index(record, newIndex); element != nil {
|
||||
data = element.Value
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Remove 删除数据
|
||||
// count为删除个数
|
||||
// count == 0 从队首开始删除所有的data
|
||||
// count > 0 从队首开始删除count个data
|
||||
// count < 0 从队尾开始删除count个data
|
||||
func (hl *HashList) Remove(key interface{}, data interface{}, count int) (rmCount int) {
|
||||
hl.mu.Lock()
|
||||
defer hl.mu.Unlock()
|
||||
|
||||
record := hl.data[key]
|
||||
if record == nil || record.Len() == 0 {
|
||||
return
|
||||
}
|
||||
var es []*list.Element
|
||||
switch {
|
||||
case count > 0:
|
||||
for e := record.Front(); e != nil && len(es) < count; e = e.Next() {
|
||||
if reflect.DeepEqual(e.Value, data) {
|
||||
es = append(es, e)
|
||||
}
|
||||
}
|
||||
case count < 0:
|
||||
for e := record.Back(); e != nil && len(es) < count; e = e.Prev() {
|
||||
if reflect.DeepEqual(e.Value, data) {
|
||||
es = append(es, e)
|
||||
}
|
||||
}
|
||||
default:
|
||||
for e := record.Front(); e != nil; e = e.Next() {
|
||||
if reflect.DeepEqual(e.Value, data) {
|
||||
es = append(es, e)
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, e := range es {
|
||||
record.Remove(e)
|
||||
}
|
||||
rmCount = len(es)
|
||||
return
|
||||
}
|
||||
|
||||
func (hl *HashList) UnsafeLInsert(key interface{}, pivot, val interface{}) int {
|
||||
record := hl.data[key]
|
||||
pEle := find(record, pivot)
|
||||
if pEle == nil {
|
||||
return -1
|
||||
}
|
||||
record.InsertBefore(val, pEle)
|
||||
return record.Len()
|
||||
}
|
||||
|
||||
// LInsert 在指定位置前面插入数据
|
||||
// 插入失败返回-1
|
||||
// 插入成功返回队列长度
|
||||
func (hl *HashList) LInsert(key interface{}, mark, val interface{}) int {
|
||||
hl.mu.Lock()
|
||||
defer hl.mu.Unlock()
|
||||
|
||||
record := hl.data[key]
|
||||
pElement := find(record, mark)
|
||||
if pElement == nil {
|
||||
return -1
|
||||
}
|
||||
record.InsertBefore(val, pElement)
|
||||
return record.Len()
|
||||
}
|
||||
|
||||
// RInsert 在指定位置后面插入数据
|
||||
// 插入失败返回-1
|
||||
// 插入成功返回队列长度
|
||||
func (hl *HashList) RInsert(key interface{}, mark, val interface{}) int {
|
||||
hl.mu.Lock()
|
||||
defer hl.mu.Unlock()
|
||||
|
||||
record := hl.data[key]
|
||||
pElement := find(record, mark)
|
||||
if pElement == nil {
|
||||
return -1
|
||||
}
|
||||
record.InsertAfter(val, pElement)
|
||||
|
||||
return record.Len()
|
||||
}
|
||||
|
||||
// Set 将索引处的数据设置为val
|
||||
func (hl *HashList) Set(key interface{}, idx int, val interface{}) (ok bool) {
|
||||
hl.mu.Lock()
|
||||
defer hl.mu.Unlock()
|
||||
|
||||
record := hl.data[key]
|
||||
data := index(record, idx)
|
||||
if data != nil {
|
||||
data.Value = val
|
||||
ok = true
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (hl *HashList) UnsafeRange(key interface{}, start, end int) (result []interface{}) {
|
||||
record := hl.data[key]
|
||||
if record == nil || record.Len() == 0 {
|
||||
return nil
|
||||
}
|
||||
length := record.Len()
|
||||
start, end = handleIndex(length, start, end)
|
||||
if start > end || start >= length {
|
||||
return nil
|
||||
}
|
||||
|
||||
mid := length >> 1
|
||||
if end <= mid || end-mid < mid-start {
|
||||
flag := 0
|
||||
for p := record.Front(); p != nil && flag <= end; p, flag = p.Next(), flag+1 {
|
||||
if flag >= start {
|
||||
result = append(result, p.Value)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
flag := length - 1
|
||||
for p := record.Back(); p != nil && flag >= start; p, flag = p.Prev(), flag-1 {
|
||||
if flag <= end {
|
||||
result = append(result, p.Value)
|
||||
}
|
||||
}
|
||||
if len(result) > 0 {
|
||||
for i, j := 0, len(result)-1; i < j; i, j = i+1, j-1 {
|
||||
result[i], result[j] = result[j], result[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Range 遍历start到end之间的数据
|
||||
func (hl *HashList) Range(key interface{}, start, end int) (result []interface{}) {
|
||||
hl.mu.Lock()
|
||||
defer hl.mu.Unlock()
|
||||
|
||||
record := hl.data[key]
|
||||
if record == nil || record.Len() == 0 {
|
||||
return nil
|
||||
}
|
||||
length := record.Len()
|
||||
start, end = handleIndex(length, start, end)
|
||||
if start > end || start >= length {
|
||||
return nil
|
||||
}
|
||||
|
||||
mid := length >> 1
|
||||
if end <= mid || end-mid < mid-start {
|
||||
flag := 0
|
||||
for p := record.Front(); p != nil && flag <= end; p, flag = p.Next(), flag+1 {
|
||||
if flag >= start {
|
||||
result = append(result, p.Value)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
flag := length - 1
|
||||
for p := record.Back(); p != nil && flag >= start; p, flag = p.Prev(), flag-1 {
|
||||
if flag <= end {
|
||||
result = append(result, p.Value)
|
||||
}
|
||||
}
|
||||
if len(result) > 0 {
|
||||
for i, j := 0, len(result)-1; i < j; i, j = i+1, j-1 {
|
||||
result[i], result[j] = result[j], result[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Trim 截断
|
||||
func (hl *HashList) Trim(key interface{}, start, end int) bool {
|
||||
hl.mu.Lock()
|
||||
defer hl.mu.Unlock()
|
||||
|
||||
record := hl.data[key]
|
||||
if record == nil || record.Len() <= 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
length := record.Len()
|
||||
start, end = handleIndex(length, start, end)
|
||||
|
||||
//start小于等于左边界,end大于等于右边界,不处理
|
||||
if start <= 0 && end >= length-1 {
|
||||
return false
|
||||
}
|
||||
|
||||
//start大于end,或者start超出右边界,则直接将列表置空
|
||||
if start > end || start >= length {
|
||||
hl.data[key] = nil
|
||||
delete(hl.data, key)
|
||||
return true
|
||||
}
|
||||
|
||||
startEle, endEle := index(record, start), index(record, end)
|
||||
switch end-start+1 < (length >> 1) {
|
||||
case true:
|
||||
newList := list.New()
|
||||
for p := startEle; p != endEle.Next(); p = p.Next() {
|
||||
newList.PushBack(p.Value)
|
||||
}
|
||||
hl.data[key] = newList
|
||||
default:
|
||||
var ele []*list.Element
|
||||
for p := record.Front(); p != startEle; p = p.Next() {
|
||||
ele = append(ele, p)
|
||||
}
|
||||
for p := record.Back(); p != endEle; p = p.Prev() {
|
||||
ele = append(ele, p)
|
||||
}
|
||||
for _, e := range ele {
|
||||
record.Remove(e)
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (hl *HashList) Len(key interface{}) (length int) {
|
||||
hl.mu.RLock()
|
||||
length = hl.data[key].Len()
|
||||
hl.mu.RUnlock()
|
||||
return
|
||||
}
|
||||
|
||||
func (hl *HashList) push(front bool, key interface{}, val ...interface{}) int {
|
||||
record := hl.data[key]
|
||||
if record == nil {
|
||||
record = list.New()
|
||||
hl.data[key] = record
|
||||
}
|
||||
|
||||
switch {
|
||||
case front == true:
|
||||
for _, v := range val {
|
||||
record.PushFront(v)
|
||||
}
|
||||
default:
|
||||
for _, v := range val {
|
||||
record.PushBack(v)
|
||||
}
|
||||
}
|
||||
return record.Len()
|
||||
}
|
||||
|
||||
func (hl *HashList) pop(front bool, key interface{}) interface{} {
|
||||
record := hl.data[key]
|
||||
if record == nil || record.Len() == 0 {
|
||||
return nil
|
||||
}
|
||||
var element *list.Element
|
||||
switch {
|
||||
case front:
|
||||
element = record.Front()
|
||||
default:
|
||||
element = record.Back()
|
||||
}
|
||||
record.Remove(element)
|
||||
return element.Value
|
||||
}
|
||||
|
||||
func handleIndex(length, start, end int) (int, int) {
|
||||
if start < 0 {
|
||||
start += length
|
||||
}
|
||||
if end < 0 {
|
||||
end += length
|
||||
}
|
||||
if start < 0 {
|
||||
start = 0
|
||||
}
|
||||
if end >= length {
|
||||
end = length - 1
|
||||
}
|
||||
return start, end
|
||||
}
|
||||
|
||||
func validIndex(dataList *list.List, index int) (bool, int) {
|
||||
if dataList == nil || dataList.Len() <= 0 {
|
||||
return false, index
|
||||
}
|
||||
n := dataList.Len()
|
||||
if index < 0 {
|
||||
index += n
|
||||
}
|
||||
return index >= 0 && index < n, index
|
||||
}
|
||||
|
||||
func find(dataList *list.List, target interface{}) (result *list.Element) {
|
||||
if dataList == nil || dataList.Len() == 0 {
|
||||
return
|
||||
}
|
||||
for ele := dataList.Front(); ele != nil; ele = ele.Next() {
|
||||
if reflect.DeepEqual(ele.Value, target) {
|
||||
result = ele
|
||||
break
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func index(dataList *list.List, index int) (data *list.Element) {
|
||||
if dataList == nil || dataList.Len() == 0 {
|
||||
return
|
||||
}
|
||||
ok, newIndex := validIndex(dataList, index)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
index = newIndex
|
||||
|
||||
var element *list.Element
|
||||
// 如果index在前半段,则从头开始找,否则从后半段查找
|
||||
switch index <= ((dataList.Len()) >> 1) {
|
||||
case true:
|
||||
val := dataList.Front()
|
||||
for i := 0; i < index; i++ {
|
||||
val = val.Next()
|
||||
}
|
||||
element = val
|
||||
default:
|
||||
val := dataList.Back()
|
||||
for i := dataList.Len() - 1; i > index; i-- {
|
||||
val = val.Prev()
|
||||
}
|
||||
element = val
|
||||
}
|
||||
return element
|
||||
}
|
3
listpkg/list_link.go
Normal file
3
listpkg/list_link.go
Normal file
@@ -0,0 +1,3 @@
|
||||
package listpkg
|
||||
|
||||
// 链表实现的队列
|
3
listpkg/list_priority.go
Normal file
3
listpkg/list_priority.go
Normal file
@@ -0,0 +1,3 @@
|
||||
package listpkg
|
||||
|
||||
// 优先级队列
|
415
lists/list.go
415
lists/list.go
@@ -1,415 +0,0 @@
|
||||
package list
|
||||
|
||||
import (
|
||||
"container/list"
|
||||
"reflect"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type HList struct {
|
||||
mu sync.RWMutex
|
||||
data map[interface{}]*list.List
|
||||
}
|
||||
|
||||
func NewHList() *HList {
|
||||
return &HList{
|
||||
data: make(map[interface{}]*list.List),
|
||||
}
|
||||
}
|
||||
|
||||
func (hl *HList) UnsafeLPush(key interface{}, data ...interface{}) int {
|
||||
hl.init()
|
||||
return hl.push(true, key, data...)
|
||||
}
|
||||
|
||||
func (hl *HList) LPush(key interface{}, data ...interface{}) int {
|
||||
hl.mu.Lock()
|
||||
defer hl.mu.Unlock()
|
||||
return hl.UnsafeLPush(key, data...)
|
||||
}
|
||||
|
||||
func (hl *HList) UnsafeLPop(key interface{}) interface{} {
|
||||
hl.init()
|
||||
return hl.pop(true, key)
|
||||
}
|
||||
|
||||
func (hl *HList) LPop(key interface{}) interface{} {
|
||||
hl.mu.Lock()
|
||||
defer hl.mu.Unlock()
|
||||
return hl.UnsafeLPop(key)
|
||||
}
|
||||
|
||||
func (hl *HList) UnsafeRPush(key interface{}, data ...interface{}) int {
|
||||
hl.init()
|
||||
return hl.push(false, key, data...)
|
||||
}
|
||||
|
||||
func (hl *HList) RPush(key interface{}, data ...interface{}) int {
|
||||
hl.mu.Lock()
|
||||
defer hl.mu.Unlock()
|
||||
return hl.UnsafeRPush(key, data...)
|
||||
}
|
||||
|
||||
func (hl *HList) UnsafeRPop(key interface{}) interface{} {
|
||||
hl.init()
|
||||
return hl.pop(false, key)
|
||||
}
|
||||
|
||||
func (hl *HList) RPop(key interface{}) interface{} {
|
||||
hl.mu.Lock()
|
||||
defer hl.mu.Unlock()
|
||||
return hl.UnsafeRPop(key)
|
||||
}
|
||||
|
||||
func (hl *HList) UnsafeIndex(key interface{}, index int) interface{} {
|
||||
hl.init()
|
||||
ok, newIndex := hl.validIndex(key, index)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
index = newIndex
|
||||
element := hl.index(key, index)
|
||||
if element != nil {
|
||||
return element.Value
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (hl *HList) Index(key interface{}, index int) interface{} {
|
||||
hl.mu.RLock()
|
||||
defer hl.mu.RUnlock()
|
||||
return hl.UnsafeIndex(key, index)
|
||||
}
|
||||
|
||||
func (hl *HList) UnsafeRem(key interface{}, data interface{}, count int) (rmCount int) {
|
||||
hl.init()
|
||||
record := hl.data[key]
|
||||
if record == nil || record.Len() == 0 {
|
||||
return
|
||||
}
|
||||
var es []*list.Element
|
||||
if count == 0 {
|
||||
for e := record.Front(); e != nil; e = e.Next() {
|
||||
if reflect.DeepEqual(e.Value, data) {
|
||||
es = append(es, e)
|
||||
}
|
||||
}
|
||||
}
|
||||
if count > 0 {
|
||||
for e := record.Front(); e != nil && len(es) < count; e = e.Next() {
|
||||
if reflect.DeepEqual(e.Value, data) {
|
||||
es = append(es, e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if count < 0 {
|
||||
for e := record.Back(); e != nil && len(es) < count; e = e.Prev() {
|
||||
if reflect.DeepEqual(e.Value, data) {
|
||||
es = append(es, e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for _, e := range es {
|
||||
record.Remove(e)
|
||||
}
|
||||
rmCount = len(es)
|
||||
es = nil
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (hl *HList) Rem(key interface{}, data interface{}, count int) (rmCount int) {
|
||||
hl.mu.Lock()
|
||||
defer hl.mu.Unlock()
|
||||
return hl.UnsafeRem(key, data, count)
|
||||
}
|
||||
|
||||
func (hl *HList) UnsafeLInsert(key interface{}, pivot, val interface{}) int {
|
||||
hl.init()
|
||||
pEle := hl.find(key, pivot)
|
||||
if pEle == nil {
|
||||
return -1
|
||||
}
|
||||
|
||||
record := hl.data[key]
|
||||
if record == nil {
|
||||
record = list.New()
|
||||
hl.data[key] = record
|
||||
}
|
||||
record.InsertBefore(val, pEle)
|
||||
return record.Len()
|
||||
}
|
||||
|
||||
func (hl *HList) LInsert(key interface{}, pivot, val interface{}) int {
|
||||
hl.mu.Lock()
|
||||
defer hl.mu.Unlock()
|
||||
return hl.UnsafeLInsert(key, pivot, val)
|
||||
}
|
||||
|
||||
func (hl *HList) UnsafeRInsert(key interface{}, pivot, val interface{}) int {
|
||||
hl.init()
|
||||
pEle := hl.find(key, pivot)
|
||||
if pEle == nil {
|
||||
return -1
|
||||
}
|
||||
|
||||
record := hl.data[key]
|
||||
if record == nil {
|
||||
record = list.New()
|
||||
hl.data[key] = record
|
||||
}
|
||||
record.InsertAfter(val, pEle)
|
||||
return record.Len()
|
||||
}
|
||||
|
||||
func (hl *HList) RInsert(key interface{}, pivot, val interface{}) int {
|
||||
hl.mu.Lock()
|
||||
defer hl.mu.Unlock()
|
||||
return hl.UnsafeRInsert(key, pivot, val)
|
||||
}
|
||||
|
||||
func (hl *HList) UnsafeSet(key interface{}, index int, val interface{}) bool {
|
||||
hl.init()
|
||||
element := hl.index(key, index)
|
||||
if element == nil {
|
||||
return false
|
||||
}
|
||||
element.Value = val
|
||||
return true
|
||||
}
|
||||
|
||||
func (hl *HList) Set(key interface{}, index int, val interface{}) bool {
|
||||
hl.mu.Lock()
|
||||
defer hl.mu.Unlock()
|
||||
return hl.UnsafeSet(key, index, val)
|
||||
}
|
||||
|
||||
func (hl *HList) UnsafeRange(key interface{}, start, end int) (result []interface{}) {
|
||||
hl.init()
|
||||
record := hl.data[key]
|
||||
if record == nil || record.Len() == 0 {
|
||||
return nil
|
||||
}
|
||||
length := record.Len()
|
||||
start, end = hl.handleIndex(length, start, end)
|
||||
if start > end || start >= length {
|
||||
return nil
|
||||
}
|
||||
|
||||
mid := length >> 1
|
||||
if end <= mid || end-mid < mid-start {
|
||||
flag := 0
|
||||
for p := record.Front(); p != nil && flag <= end; p, flag = p.Next(), flag+1 {
|
||||
if flag >= start {
|
||||
result = append(result, p.Value)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
flag := length - 1
|
||||
for p := record.Back(); p != nil && flag >= start; p, flag = p.Prev(), flag-1 {
|
||||
if flag <= end {
|
||||
result = append(result, p.Value)
|
||||
}
|
||||
}
|
||||
if len(result) > 0 {
|
||||
for i, j := 0, len(result)-1; i < j; i, j = i+1, j-1 {
|
||||
result[i], result[j] = result[j], result[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (hl *HList) Range(key interface{}, start, end int) (result []interface{}) {
|
||||
hl.mu.Lock()
|
||||
defer hl.mu.Unlock()
|
||||
return hl.UnsafeRange(key, start, end)
|
||||
}
|
||||
|
||||
func (hl *HList) UnsafeTrim(key interface{}, start, end int) bool {
|
||||
hl.init()
|
||||
item := hl.data[key]
|
||||
if item == nil || item.Len() <= 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
length := item.Len()
|
||||
start, end = hl.handleIndex(length, start, end)
|
||||
|
||||
//start小于等于左边界,end大于等于右边界,不处理
|
||||
if start <= 0 && end >= length-1 {
|
||||
return false
|
||||
}
|
||||
|
||||
//start大于end,或者start超出右边界,则直接将列表置空
|
||||
if start > end || start >= length {
|
||||
hl.data[key] = nil
|
||||
delete(hl.data, key)
|
||||
return true
|
||||
}
|
||||
|
||||
startEle, endEle := hl.index(key, start), hl.index(key, end)
|
||||
if end-start+1 < (length >> 1) {
|
||||
newList := list.New()
|
||||
for p := startEle; p != endEle.Next(); p = p.Next() {
|
||||
newList.PushBack(p.Value)
|
||||
}
|
||||
|
||||
item = nil
|
||||
hl.data[key] = newList
|
||||
} else {
|
||||
var ele []*list.Element
|
||||
for p := item.Front(); p != startEle; p = p.Next() {
|
||||
ele = append(ele, p)
|
||||
}
|
||||
for p := item.Back(); p != endEle; p = p.Prev() {
|
||||
ele = append(ele, p)
|
||||
}
|
||||
|
||||
for _, e := range ele {
|
||||
item.Remove(e)
|
||||
}
|
||||
|
||||
ele = nil
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (hl *HList) Trim(key interface{}, start, end int) bool {
|
||||
hl.mu.Lock()
|
||||
defer hl.mu.Unlock()
|
||||
return hl.UnsafeTrim(key, start, end)
|
||||
}
|
||||
|
||||
func (hl *HList) UnsafeLen(key interface{}) int {
|
||||
hl.init()
|
||||
record := hl.data[key]
|
||||
if record != nil {
|
||||
return record.Len()
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
func (hl *HList) Len(key interface{}) int {
|
||||
hl.mu.RLock()
|
||||
defer hl.mu.RUnlock()
|
||||
return hl.UnsafeLen(key)
|
||||
}
|
||||
|
||||
func (hl *HList) init() {
|
||||
if hl.data == nil {
|
||||
hl.data = make(map[interface{}]*list.List)
|
||||
}
|
||||
}
|
||||
|
||||
func (hl *HList) push(front bool, key interface{}, val ...interface{}) int {
|
||||
record := hl.data[key]
|
||||
if record == nil {
|
||||
record = list.New()
|
||||
hl.data[key] = record
|
||||
}
|
||||
|
||||
switch {
|
||||
case front == true:
|
||||
for _, v := range val {
|
||||
record.PushFront(v)
|
||||
}
|
||||
default:
|
||||
for _, v := range val {
|
||||
record.PushBack(v)
|
||||
}
|
||||
}
|
||||
return record.Len()
|
||||
}
|
||||
|
||||
func (hl *HList) pop(front bool, key interface{}) interface{} {
|
||||
record := hl.data[key]
|
||||
if record == nil || record.Len() == 0 {
|
||||
return nil
|
||||
}
|
||||
var element *list.Element
|
||||
switch {
|
||||
case front:
|
||||
element = record.Front()
|
||||
default:
|
||||
element = record.Back()
|
||||
}
|
||||
record.Remove(element)
|
||||
return element.Value
|
||||
}
|
||||
|
||||
func (hl *HList) find(key interface{}, data interface{}) *list.Element {
|
||||
record := hl.data[key]
|
||||
if record == nil || record.Len() == 0 {
|
||||
return nil
|
||||
}
|
||||
var e *list.Element
|
||||
for ele := record.Front(); ele != nil; ele = ele.Next() {
|
||||
if reflect.DeepEqual(ele.Value, data) {
|
||||
e = ele
|
||||
break
|
||||
}
|
||||
}
|
||||
return e
|
||||
}
|
||||
|
||||
func (hl *HList) index(key interface{}, index int) *list.Element {
|
||||
ok, newIndex := hl.validIndex(key, index)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
index = newIndex
|
||||
var record = hl.data[key]
|
||||
if record == nil || record.Len() == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
var element *list.Element
|
||||
// 如果index在前半段,则从头开始找,否则从后半段查找
|
||||
if index <= ((record.Len()) >> 1) {
|
||||
val := record.Front()
|
||||
for i := 0; i < index; i++ {
|
||||
val = val.Next()
|
||||
}
|
||||
element = val
|
||||
} else {
|
||||
val := record.Back()
|
||||
for i := record.Len() - 1; i > index; i-- {
|
||||
val = val.Prev()
|
||||
}
|
||||
element = val
|
||||
}
|
||||
return element
|
||||
}
|
||||
|
||||
func (hl *HList) validIndex(key interface{}, index int) (bool, int) {
|
||||
record := hl.data[key]
|
||||
if record == nil || record.Len() <= 0 {
|
||||
return false, index
|
||||
}
|
||||
n := record.Len()
|
||||
if index < 0 {
|
||||
index += n
|
||||
}
|
||||
return index >= 0 && index < n, index
|
||||
}
|
||||
|
||||
func (hl *HList) handleIndex(length, start, end int) (int, int) {
|
||||
if start < 0 {
|
||||
start += length
|
||||
}
|
||||
if end < 0 {
|
||||
end += length
|
||||
}
|
||||
if start < 0 {
|
||||
start = 0
|
||||
}
|
||||
if end >= length {
|
||||
end = length - 1
|
||||
}
|
||||
return start, end
|
||||
}
|
@@ -1,4 +1,4 @@
|
||||
package logs
|
||||
package logpkg
|
||||
|
||||
var (
|
||||
logger Logger
|
||||
@@ -22,62 +22,50 @@ type Logger interface {
|
||||
WithFields(fields Fields) Logger
|
||||
}
|
||||
|
||||
// Debug
|
||||
func Debug(args ...interface{}) {
|
||||
logger.Debug(args...)
|
||||
}
|
||||
|
||||
// Debugf
|
||||
func Debugf(format string, args ...interface{}) {
|
||||
logger.Debugf(format, args...)
|
||||
}
|
||||
|
||||
// Info
|
||||
func Info(args ...interface{}) {
|
||||
logger.Info(args...)
|
||||
}
|
||||
|
||||
// Infof
|
||||
func Infof(format string, args ...interface{}) {
|
||||
logger.Infof(format, args...)
|
||||
}
|
||||
|
||||
// Warn
|
||||
func Warn(args ...interface{}) {
|
||||
logger.Warn(args...)
|
||||
}
|
||||
|
||||
// Warnf
|
||||
func Warnf(format string, args ...interface{}) {
|
||||
logger.Warnf(format, args...)
|
||||
}
|
||||
|
||||
// Error
|
||||
func Error(args ...interface{}) {
|
||||
logger.Error(args...)
|
||||
}
|
||||
|
||||
// Errorf
|
||||
func Errorf(format string, args ...interface{}) {
|
||||
logger.Errorf(format, args...)
|
||||
}
|
||||
|
||||
// Fatal
|
||||
func Fatal(args ...interface{}) {
|
||||
logger.Fatal(args...)
|
||||
}
|
||||
|
||||
// Fatalf
|
||||
func Fatalf(format string, args ...interface{}) {
|
||||
logger.Fatalf(format, args...)
|
||||
}
|
||||
|
||||
// Panicf
|
||||
func Panicf(format string, args ...interface{}) {
|
||||
logger.Panicf(format, args...)
|
||||
}
|
||||
|
||||
// WithFields
|
||||
func WithFields(fields Fields) Logger {
|
||||
return logger.WithFields(fields)
|
||||
}
|
@@ -1,4 +1,4 @@
|
||||
package logs
|
||||
package logpkg
|
||||
|
||||
// LogOption 日志配置项
|
||||
type LogOption func(zl *zapLogger)
|
@@ -1,4 +1,4 @@
|
||||
package logs
|
||||
package logpkg
|
||||
|
||||
import (
|
||||
"io"
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/pyihe/go-pkg/nets"
|
||||
"github.com/pyihe/go-pkg/netpkg"
|
||||
|
||||
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
|
||||
"go.uber.org/zap"
|
||||
@@ -57,7 +57,7 @@ func newZapLogger(opts ...LogOption) (Logger, error) {
|
||||
|
||||
encoder := getJSONEncoder()
|
||||
|
||||
op := zap.Fields(zap.String("ip", nets.GetLocalIP()), zap.String("app", zlogger.name))
|
||||
op := zap.Fields(zap.String("ip", netpkg.GetLocalIP()), zap.String("app", zlogger.name))
|
||||
options = append(options, op)
|
||||
|
||||
allLevel := zap.LevelEnablerFunc(func(lv zapcore.Level) bool {
|
217
logs/log.go
217
logs/log.go
@@ -1,217 +0,0 @@
|
||||
package logs
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path"
|
||||
"runtime"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
colorRed = 31
|
||||
colorYellow = 33
|
||||
colorBlue = 34
|
||||
|
||||
levelT = "[T] "
|
||||
levelE = "[E] "
|
||||
levelW = "[W] "
|
||||
levelF = "[F] "
|
||||
|
||||
defaultFileSize = 60 * 1024 * 1024
|
||||
minFileSize = 1 * 1024 * 1024
|
||||
defaultLogDir = "logs"
|
||||
defaultLogName = "default.logs"
|
||||
)
|
||||
|
||||
const (
|
||||
logTypeStd logType = iota + 1
|
||||
logTypeFile
|
||||
)
|
||||
|
||||
type (
|
||||
logType int
|
||||
|
||||
logOption func(log *myLog)
|
||||
|
||||
myLog struct {
|
||||
sync.Once
|
||||
sync.Mutex
|
||||
outs map[logType]io.Writer //writer集合
|
||||
file *os.File //文件句柄
|
||||
fileName string //日志名
|
||||
dir string //日志存放路径
|
||||
size int64 //单个日志文件的大小限制
|
||||
}
|
||||
)
|
||||
|
||||
var (
|
||||
defaultLogger = &myLog{}
|
||||
)
|
||||
|
||||
func (m *myLog) init() {
|
||||
if m.dir == "" {
|
||||
m.dir = defaultLogDir
|
||||
}
|
||||
if m.fileName == "" {
|
||||
m.fileName = defaultLogName
|
||||
}
|
||||
if m.size == 0 {
|
||||
m.size = defaultFileSize
|
||||
} else {
|
||||
if m.size < minFileSize {
|
||||
panic(fmt.Sprintf("invalid size: %d", m.size))
|
||||
}
|
||||
}
|
||||
|
||||
if m.outs == nil {
|
||||
m.outs = make(map[logType]io.Writer)
|
||||
}
|
||||
if !isExist(m.dir) {
|
||||
if err := os.Mkdir(m.dir, 0777); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
name := path.Join(m.dir, m.fileName)
|
||||
file, err := os.OpenFile(name, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0755)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
m.file = file
|
||||
m.outs[logTypeStd] = os.Stdout
|
||||
m.outs[logTypeFile] = file
|
||||
}
|
||||
|
||||
func (m *myLog) checkLogSize() {
|
||||
if m.file == nil {
|
||||
return
|
||||
}
|
||||
m.Lock()
|
||||
defer m.Unlock()
|
||||
fileInfo, err := m.file.Stat()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if m.size > fileInfo.Size() {
|
||||
return
|
||||
}
|
||||
//需要分割
|
||||
newName := path.Join(m.dir, time.Now().Format("2006_01_02_15:04:03")+".logs")
|
||||
name := path.Join(m.dir, m.fileName)
|
||||
|
||||
err = os.Rename(name, newName)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
file, err := os.OpenFile(name, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0755)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
m.file.Close()
|
||||
m.file = file
|
||||
m.outs[logTypeFile] = file
|
||||
return
|
||||
}
|
||||
|
||||
func (m *myLog) writeLog() {
|
||||
|
||||
}
|
||||
|
||||
func (m *myLog) write(msg, timeStr, fileName, prefix string, line, color int) {
|
||||
m.checkLogSize()
|
||||
|
||||
for k, wr := range m.outs {
|
||||
if k == logTypeStd {
|
||||
fmt.Fprintf(wr, "%c[%dm%s[%s %s:%d] %s%c[0m\n", 0x1B, color, prefix, timeStr, fileName, line, msg, 0x1B)
|
||||
} else {
|
||||
fmt.Fprintf(wr, "%s[%s %s:%d] %s\n", prefix, timeStr, fileName, line, msg)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func WithSize(size int64) logOption {
|
||||
return func(log *myLog) {
|
||||
log.size = size
|
||||
}
|
||||
}
|
||||
|
||||
func WithLogDir(dir string) logOption {
|
||||
return func(log *myLog) {
|
||||
log.dir = dir
|
||||
}
|
||||
}
|
||||
|
||||
func WithFileName(name string) logOption {
|
||||
return func(log *myLog) {
|
||||
log.fileName = name
|
||||
}
|
||||
}
|
||||
|
||||
func InitLogger(args ...logOption) {
|
||||
defaultLogger.Do(func() {
|
||||
for _, af := range args {
|
||||
af(defaultLogger)
|
||||
}
|
||||
defaultLogger.init()
|
||||
})
|
||||
}
|
||||
|
||||
////Info
|
||||
func T(format string, v ...interface{}) {
|
||||
timeStr, fileName, line := getPrefixInfo()
|
||||
defaultLogger.write(fmt.Sprintf(format, v...), timeStr, fileName, levelT, line, colorBlue)
|
||||
}
|
||||
|
||||
//
|
||||
////Error
|
||||
func E(format string, v ...interface{}) {
|
||||
timeStr, fileName, line := getPrefixInfo()
|
||||
defaultLogger.write(fmt.Sprintf(format, v...), timeStr, fileName, levelE, line, colorYellow)
|
||||
}
|
||||
|
||||
//Warn
|
||||
func W(format string, v ...interface{}) {
|
||||
timeStr, fileName, line := getPrefixInfo()
|
||||
defaultLogger.write(fmt.Sprintf(format, v...), timeStr, fileName, levelW, line, colorRed)
|
||||
}
|
||||
|
||||
//Fatal
|
||||
func F(format string, v ...interface{}) {
|
||||
timeStr, fileName, line := getPrefixInfo()
|
||||
defaultLogger.write(fmt.Sprintf(format, v...), timeStr, fileName, levelF, line, colorRed)
|
||||
os.Exit(-1)
|
||||
}
|
||||
|
||||
func getPrefixInfo() (timeStr, fileName string, line int) {
|
||||
_, file, line, _ := runtime.Caller(1)
|
||||
fileName = shortFileName(file)
|
||||
timeStr = time.Now().Format("2006-01-02 15:04:05.0000")
|
||||
return timeStr, fileName, line
|
||||
}
|
||||
|
||||
func isExist(path string) bool {
|
||||
_, err := os.Stat(path)
|
||||
if err != nil {
|
||||
if os.IsExist(err) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func shortFileName(file string) string {
|
||||
short := file
|
||||
for i := len(file) - 1; i > 0; i-- {
|
||||
if file[i] == '/' {
|
||||
short = file[i+1:]
|
||||
break
|
||||
}
|
||||
}
|
||||
return short
|
||||
}
|
@@ -1,4 +1,4 @@
|
||||
package maps
|
||||
package mappkg
|
||||
|
||||
import "sync"
|
||||
|
@@ -1,9 +1,10 @@
|
||||
package maps
|
||||
package mappkg
|
||||
|
||||
import (
|
||||
"github.com/pyihe/go-pkg/errors"
|
||||
"reflect"
|
||||
"strconv"
|
||||
|
||||
"github.com/pyihe/go-pkg/errpkg"
|
||||
)
|
||||
|
||||
type Param map[string]interface{}
|
||||
@@ -38,7 +39,7 @@ func (p Param) Range(fn func(key string, value interface{}) (breakOut bool)) {
|
||||
func (p Param) GetString(key string) (string, error) {
|
||||
value, ok := p.Get(key)
|
||||
if !ok {
|
||||
return "", errors.New("not exist key: " + key)
|
||||
return "", errpkg.New("not exist key: " + key)
|
||||
}
|
||||
return reflect.ValueOf(value).String(), nil
|
||||
}
|
||||
@@ -46,7 +47,7 @@ func (p Param) GetString(key string) (string, error) {
|
||||
func (p Param) GetInt64(key string) (n int64, err error) {
|
||||
value, ok := p.Get(key)
|
||||
if !ok {
|
||||
return 0, errors.New("not exist key: " + key)
|
||||
return 0, errpkg.New("not exist key: " + key)
|
||||
}
|
||||
t := reflect.TypeOf(value)
|
||||
v := reflect.ValueOf(value)
|
||||
@@ -64,7 +65,7 @@ func (p Param) GetInt64(key string) (n int64, err error) {
|
||||
case reflect.Float32, reflect.Float64:
|
||||
n = int64(v.Float())
|
||||
default:
|
||||
err = errors.New("unknown type: " + t.String())
|
||||
err = errpkg.New("unknown type: " + t.String())
|
||||
}
|
||||
return
|
||||
}
|
@@ -1,4 +1,4 @@
|
||||
package maps
|
||||
package mappkg
|
||||
|
||||
import "sync"
|
||||
|
@@ -1,4 +1,4 @@
|
||||
package maths
|
||||
package mathpkg
|
||||
|
||||
func factorial(m int) (n int) {
|
||||
n = 1
|
@@ -1,4 +1,4 @@
|
||||
package maths
|
||||
package mathpkg
|
||||
|
||||
func MaxInt(a, b int) int {
|
||||
if a > b {
|
@@ -1,4 +1,4 @@
|
||||
package monitor
|
||||
package monitorpkg
|
||||
|
||||
import (
|
||||
"os"
|
@@ -1,4 +1,4 @@
|
||||
package monitor
|
||||
package monitorpkg
|
||||
|
||||
import (
|
||||
"errors"
|
@@ -1,4 +1,4 @@
|
||||
package nets
|
||||
package netpkg
|
||||
|
||||
import (
|
||||
"net"
|
@@ -1,4 +1,4 @@
|
||||
package ptr
|
||||
package pointerpkg
|
||||
|
||||
/*
|
||||
返回各种基本数据类型的指针
|
@@ -1,10 +1,10 @@
|
||||
package rands
|
||||
package randpkg
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"time"
|
||||
|
||||
"github.com/pyihe/go-pkg/bytes"
|
||||
"github.com/pyihe/go-pkg/bytepkg"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -74,7 +74,7 @@ func String(n int) string {
|
||||
cache >>= letterIdxBits
|
||||
remain--
|
||||
}
|
||||
return bytes.String(b)
|
||||
return bytepkg.String(b)
|
||||
}
|
||||
|
||||
// ShuffleBytes shuffle 随机算法
|
@@ -1,21 +1,21 @@
|
||||
package redis
|
||||
package redispkg
|
||||
|
||||
import (
|
||||
"github.com/garyburd/redigo/redis"
|
||||
"github.com/pyihe/go-pkg/encoding"
|
||||
"github.com/pyihe/go-pkg/errors"
|
||||
"github.com/pyihe/go-pkg/errpkg"
|
||||
"github.com/pyihe/go-pkg/serialize"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrInvalidKey = errors.New("invalid key")
|
||||
ErrInvalidEncoder = errors.New("not figure encoder")
|
||||
ErrInvalidConn = errors.New("invalid redis conn")
|
||||
ErrInvalidParamNum = errors.New("invalid param num")
|
||||
ErrInvalidKey = errpkg.New("invalid key")
|
||||
ErrInvalidEncoder = errpkg.New("not figure encoder")
|
||||
ErrInvalidConn = errpkg.New("invalid redis conn")
|
||||
ErrInvalidParamNum = errpkg.New("invalid param num")
|
||||
)
|
||||
|
||||
type myRedisConn struct {
|
||||
conn redis.Conn //redis连接池
|
||||
encoder encoding.Encoding
|
||||
encoder serialize.Serializer
|
||||
prefix string
|
||||
}
|
||||
|
||||
@@ -142,7 +142,7 @@ func (conn *myRedisConn) getStruct(key string, data interface{}) (err error) {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = conn.encoder.Unmarshal(bytes, data)
|
||||
err = conn.encoder.Decode(bytes, data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -157,7 +157,7 @@ func (conn *myRedisConn) setStruct(key string, data interface{}) error {
|
||||
if conn.encoder == nil {
|
||||
return ErrInvalidEncoder
|
||||
}
|
||||
bytes, err := conn.encoder.Marshal(data)
|
||||
bytes, err := conn.encoder.Encode(data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
@@ -1,4 +1,4 @@
|
||||
package redis
|
||||
package redispkg
|
||||
|
||||
func (conn *myRedisConn) HGet(key string, field string) ([]byte, error) {
|
||||
value, err := conn.hGet(key, field)
|
@@ -1,4 +1,4 @@
|
||||
package redis
|
||||
package redispkg
|
||||
|
||||
func (conn *myRedisConn) RPush(key string, values ...interface{}) error {
|
||||
err := conn.rpush(key, values...)
|
@@ -1,10 +1,10 @@
|
||||
package redis
|
||||
package redispkg
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/pyihe/go-pkg/encoding"
|
||||
"github.com/pyihe/go-pkg/serialize"
|
||||
|
||||
"github.com/garyburd/redigo/redis"
|
||||
)
|
||||
@@ -58,12 +58,12 @@ type myPool struct {
|
||||
pass string
|
||||
db int
|
||||
p *redis.Pool
|
||||
encoder encoding.Encoding
|
||||
encoder serialize.Serializer
|
||||
}
|
||||
|
||||
type InitOptions func(m *myPool)
|
||||
|
||||
func WithEncoding(encoder encoding.Encoding) InitOptions {
|
||||
func WithEncoding(encoder serialize.Serializer) InitOptions {
|
||||
return func(m *myPool) {
|
||||
m.encoder = encoder
|
||||
}
|
||||
@@ -105,7 +105,7 @@ func NewPool(opts ...InitOptions) (RedisPool, error) {
|
||||
op(defaultPool)
|
||||
}
|
||||
if defaultPool.addr == "" {
|
||||
return nil, fmt.Errorf("no redis address")
|
||||
return nil, fmt.Errorf("no redispkg address")
|
||||
}
|
||||
if defaultPool.db == 0 {
|
||||
defaultPool.db = 1
|
@@ -1,4 +1,4 @@
|
||||
package redis
|
||||
package redispkg
|
||||
|
||||
func (conn *myRedisConn) SADD(key string, members ...interface{}) error {
|
||||
err := conn.sAdd(key, members...)
|
@@ -1,4 +1,4 @@
|
||||
package redis
|
||||
package redispkg
|
||||
|
||||
func (conn *myRedisConn) GetKeys(pattern string) (keys []string, err error) {
|
||||
keys, err = conn.getKeys(pattern)
|
6
serialize/encoding.go
Normal file
6
serialize/encoding.go
Normal file
@@ -0,0 +1,6 @@
|
||||
package serialize
|
||||
|
||||
type Serializer interface {
|
||||
Encode(v interface{}) ([]byte, error)
|
||||
Decode(data []byte, v interface{}) error
|
||||
}
|
23
serialize/gob.go
Normal file
23
serialize/gob.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package serialize
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/gob"
|
||||
)
|
||||
|
||||
type gobSerializer struct{}
|
||||
|
||||
func Gob() Serializer {
|
||||
return gobSerializer{}
|
||||
}
|
||||
|
||||
func (gb gobSerializer) Encode(v interface{}) (data []byte, err error) {
|
||||
buff := bytes.NewBuffer([]byte{})
|
||||
err = gob.NewEncoder(buff).Encode(v)
|
||||
data = buff.Bytes()
|
||||
return
|
||||
}
|
||||
|
||||
func (gb gobSerializer) Decode(data []byte, v interface{}) error {
|
||||
return gob.NewDecoder(bytes.NewReader(data)).Decode(v)
|
||||
}
|
17
serialize/json.go
Normal file
17
serialize/json.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package serialize
|
||||
|
||||
import "encoding/json"
|
||||
|
||||
type jsonSerializer struct{}
|
||||
|
||||
func JSON() Serializer {
|
||||
return jsonSerializer{}
|
||||
}
|
||||
|
||||
func (js jsonSerializer) Encode(v interface{}) (data []byte, err error) {
|
||||
return json.Marshal(v)
|
||||
}
|
||||
|
||||
func (js jsonSerializer) Decode(data []byte, v interface{}) error {
|
||||
return json.Unmarshal(data, v)
|
||||
}
|
17
serialize/msgpack.go
Normal file
17
serialize/msgpack.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package serialize
|
||||
|
||||
import "github.com/vmihailenco/msgpack/v5"
|
||||
|
||||
type msgPack struct{}
|
||||
|
||||
func Msgpack() Serializer {
|
||||
return msgPack{}
|
||||
}
|
||||
|
||||
func (mp msgPack) Encode(v interface{}) (data []byte, err error) {
|
||||
return msgpack.Marshal(v)
|
||||
}
|
||||
|
||||
func (mp msgPack) Decode(data []byte, v interface{}) error {
|
||||
return msgpack.Unmarshal(data, v)
|
||||
}
|
29
serialize/proto.go
Normal file
29
serialize/proto.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package serialize
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/golang/protobuf/proto"
|
||||
)
|
||||
|
||||
type protoSerializer struct{}
|
||||
|
||||
func Proto() Serializer {
|
||||
return protoSerializer{}
|
||||
}
|
||||
|
||||
func (ps protoSerializer) Encode(v interface{}) (data []byte, err error) {
|
||||
if m, ok := v.(proto.Message); ok {
|
||||
return proto.Marshal(m)
|
||||
}
|
||||
err = errors.New("not proto.Message")
|
||||
return
|
||||
}
|
||||
|
||||
func (ps protoSerializer) Decode(data []byte, v interface{}) (err error) {
|
||||
if m, ok := v.(proto.Message); ok {
|
||||
return proto.Unmarshal(data, m)
|
||||
}
|
||||
err = errors.New("not proto.Message")
|
||||
return
|
||||
}
|
@@ -1,9 +1,9 @@
|
||||
package slice
|
||||
package slicepkg
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/pyihe/go-pkg/sorts"
|
||||
"github.com/pyihe/go-pkg/sortpkg"
|
||||
)
|
||||
|
||||
type float32Slice []float32
|
||||
@@ -38,7 +38,7 @@ func (f *float32Slice) Sort() {
|
||||
if f == nil {
|
||||
return
|
||||
}
|
||||
sorts.SortFloat32s(*f)
|
||||
sortpkg.SortFloat32s(*f)
|
||||
}
|
||||
|
||||
func (f *float32Slice) PushBack(x interface{}) (bool, int) {
|
@@ -1,9 +1,9 @@
|
||||
package slice
|
||||
package slicepkg
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/pyihe/go-pkg/sorts"
|
||||
"github.com/pyihe/go-pkg/sortpkg"
|
||||
)
|
||||
|
||||
type float64Slice []float64
|
||||
@@ -38,7 +38,7 @@ func (f *float64Slice) Sort() {
|
||||
if f == nil {
|
||||
return
|
||||
}
|
||||
sorts.SortFloat64s(*f)
|
||||
sortpkg.SortFloat64s(*f)
|
||||
}
|
||||
|
||||
func (f *float64Slice) PushBack(x interface{}) (bool, int) {
|
@@ -1,9 +1,9 @@
|
||||
package slice
|
||||
package slicepkg
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/pyihe/go-pkg/sorts"
|
||||
"github.com/pyihe/go-pkg/sortpkg"
|
||||
)
|
||||
|
||||
type intSlice []int
|
||||
@@ -38,7 +38,7 @@ func (is *intSlice) Sort() {
|
||||
if is == nil {
|
||||
return
|
||||
}
|
||||
sorts.SortInts(*is)
|
||||
sortpkg.SortInts(*is)
|
||||
}
|
||||
|
||||
func (is *intSlice) PushBack(x interface{}) (bool, int) {
|
@@ -1,9 +1,9 @@
|
||||
package slice
|
||||
package slicepkg
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/pyihe/go-pkg/sorts"
|
||||
"github.com/pyihe/go-pkg/sortpkg"
|
||||
)
|
||||
|
||||
type int16Slice []int16
|
||||
@@ -38,7 +38,7 @@ func (i16 *int16Slice) Sort() {
|
||||
if i16 == nil {
|
||||
return
|
||||
}
|
||||
sorts.SortInt16s(*i16)
|
||||
sortpkg.SortInt16s(*i16)
|
||||
}
|
||||
|
||||
func (i16 *int16Slice) PushBack(x interface{}) (bool, int) {
|
@@ -1,9 +1,9 @@
|
||||
package slice
|
||||
package slicepkg
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/pyihe/go-pkg/sorts"
|
||||
"github.com/pyihe/go-pkg/sortpkg"
|
||||
)
|
||||
|
||||
type int32Slice []int32
|
||||
@@ -38,7 +38,7 @@ func (i32 *int32Slice) Sort() {
|
||||
if i32 == nil {
|
||||
return
|
||||
}
|
||||
sorts.SortInt32s(*i32)
|
||||
sortpkg.SortInt32s(*i32)
|
||||
}
|
||||
|
||||
func (i32 *int32Slice) PushBack(x interface{}) (bool, int) {
|
@@ -1,9 +1,9 @@
|
||||
package slice
|
||||
package slicepkg
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/pyihe/go-pkg/sorts"
|
||||
"github.com/pyihe/go-pkg/sortpkg"
|
||||
)
|
||||
|
||||
type int64Slice []int64
|
||||
@@ -38,7 +38,7 @@ func (i64 *int64Slice) Sort() {
|
||||
if i64 == nil {
|
||||
return
|
||||
}
|
||||
sorts.SortInt64s(*i64)
|
||||
sortpkg.SortInt64s(*i64)
|
||||
}
|
||||
|
||||
func (i64 *int64Slice) PushBack(x interface{}) (bool, int) {
|
@@ -1,9 +1,9 @@
|
||||
package slice
|
||||
package slicepkg
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/pyihe/go-pkg/sorts"
|
||||
"github.com/pyihe/go-pkg/sortpkg"
|
||||
)
|
||||
|
||||
type int8Slice []int8
|
||||
@@ -38,7 +38,7 @@ func (i8 *int8Slice) Sort() {
|
||||
if i8 == nil {
|
||||
return
|
||||
}
|
||||
sorts.SortInt8s(*i8)
|
||||
sortpkg.SortInt8s(*i8)
|
||||
}
|
||||
|
||||
func (i8 *int8Slice) PushBack(x interface{}) (bool, int) {
|
@@ -1,4 +1,4 @@
|
||||
package slice
|
||||
package slicepkg
|
||||
|
||||
import (
|
||||
"fmt"
|
@@ -1,9 +1,9 @@
|
||||
package slice
|
||||
package slicepkg
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/pyihe/go-pkg/sorts"
|
||||
"github.com/pyihe/go-pkg/sortpkg"
|
||||
)
|
||||
|
||||
type stringSlice []string
|
||||
@@ -38,7 +38,7 @@ func (ss *stringSlice) Sort() {
|
||||
if ss == nil {
|
||||
return
|
||||
}
|
||||
sorts.SortStrings(*ss)
|
||||
sortpkg.SortStrings(*ss)
|
||||
}
|
||||
|
||||
func (ss *stringSlice) PushBack(x interface{}) (bool, int) {
|
@@ -1,9 +1,9 @@
|
||||
package slice
|
||||
package slicepkg
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/pyihe/go-pkg/sorts"
|
||||
"github.com/pyihe/go-pkg/sortpkg"
|
||||
)
|
||||
|
||||
type uintSlice []uint
|
||||
@@ -38,7 +38,7 @@ func (us *uintSlice) Sort() {
|
||||
if us == nil {
|
||||
return
|
||||
}
|
||||
sorts.SortUints(*us)
|
||||
sortpkg.SortUints(*us)
|
||||
}
|
||||
|
||||
func (us *uintSlice) PushBack(x interface{}) (bool, int) {
|
@@ -1,9 +1,9 @@
|
||||
package slice
|
||||
package slicepkg
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/pyihe/go-pkg/sorts"
|
||||
"github.com/pyihe/go-pkg/sortpkg"
|
||||
)
|
||||
|
||||
type uint16Slice []uint16
|
||||
@@ -38,7 +38,7 @@ func (u16 *uint16Slice) Sort() {
|
||||
if u16 == nil {
|
||||
return
|
||||
}
|
||||
sorts.SortUint16s(*u16)
|
||||
sortpkg.SortUint16s(*u16)
|
||||
}
|
||||
|
||||
func (u16 *uint16Slice) PushBack(x interface{}) (bool, int) {
|
@@ -1,9 +1,9 @@
|
||||
package slice
|
||||
package slicepkg
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/pyihe/go-pkg/sorts"
|
||||
"github.com/pyihe/go-pkg/sortpkg"
|
||||
)
|
||||
|
||||
type uint32Slice []uint32
|
||||
@@ -38,7 +38,7 @@ func (i32 *uint32Slice) Sort() {
|
||||
if i32 == nil {
|
||||
return
|
||||
}
|
||||
sorts.SortUint32s(*i32)
|
||||
sortpkg.SortUint32s(*i32)
|
||||
}
|
||||
|
||||
func (i32 *uint32Slice) PushBack(x interface{}) (bool, int) {
|
@@ -1,9 +1,9 @@
|
||||
package slice
|
||||
package slicepkg
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/pyihe/go-pkg/sorts"
|
||||
"github.com/pyihe/go-pkg/sortpkg"
|
||||
)
|
||||
|
||||
type uint64Slice []uint64
|
||||
@@ -38,7 +38,7 @@ func (i64 *uint64Slice) Sort() {
|
||||
if i64 == nil {
|
||||
return
|
||||
}
|
||||
sorts.SortUint64s(*i64)
|
||||
sortpkg.SortUint64s(*i64)
|
||||
}
|
||||
|
||||
func (i64 *uint64Slice) PushBack(x interface{}) (bool, int) {
|
@@ -1,9 +1,9 @@
|
||||
package slice
|
||||
package slicepkg
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/pyihe/go-pkg/sorts"
|
||||
"github.com/pyihe/go-pkg/sortpkg"
|
||||
)
|
||||
|
||||
type uint8Slice []uint8
|
||||
@@ -38,7 +38,7 @@ func (i8 *uint8Slice) Sort() {
|
||||
if i8 == nil {
|
||||
return
|
||||
}
|
||||
sorts.SortUint8s(*i8)
|
||||
sortpkg.SortUint8s(*i8)
|
||||
}
|
||||
|
||||
func (i8 *uint8Slice) PushBack(x interface{}) (bool, int) {
|
@@ -1,4 +1,4 @@
|
||||
package sorts
|
||||
package sortpkg
|
||||
|
||||
import "sort"
|
||||
|
@@ -1,4 +1,4 @@
|
||||
package sorts
|
||||
package sortpkg
|
||||
|
||||
//从小到大排序
|
||||
// BubbleSort 冒泡排序,每次找出最小的值放在最前面
|
@@ -1,4 +1,4 @@
|
||||
package strings
|
||||
package stringpkg
|
||||
|
||||
import (
|
||||
"reflect"
|
@@ -1,4 +1,4 @@
|
||||
package syncs
|
||||
package syncpkg
|
||||
|
||||
import "sync/atomic"
|
||||
|
@@ -1,4 +1,4 @@
|
||||
package times
|
||||
package timepkg
|
||||
|
||||
import "time"
|
||||
|
@@ -1,4 +1,4 @@
|
||||
package zips
|
||||
package zippkg
|
||||
|
||||
import (
|
||||
"bytes"
|
Reference in New Issue
Block a user