util

package
v0.0.0-...-548fb8d Latest Latest
Warning

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

Go to latest
Published: May 20, 2016 License: MPL-2.0 Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	TRACE    = NewLogger(os.Stderr, false, "[TRACE] ")
	DEBUG    = NewLogger(os.Stderr, false, "[DEBUG] ")
	WARN     = NewLogger(os.Stderr, true, "[WARN] ")
	ERROR    = NewLogger(os.Stderr, true, "[ERROR] ")
	INFO     = NewLogger(os.Stdout, true, "[INFO] ")
	CRITICAL = NewLogger(os.Stdout, true, "[CRITICAL] ")
)

默认四个日志级别

当前IP的缓存

Functions

func AES

func AES(byteKey, src []byte, isEncode bool) (rst []byte)

AES 返回binary结果

func AESCBC

func AESCBC(byteKey, src []byte, isEncode bool) (rst []byte)

AES-CBC 返回binary结果

func AESCBCString

func AESCBCString(byteKey, src []byte, isEncode bool) (rst string)

AES-CBC 返回 hex结果

func AESCBCStringX

func AESCBCStringX(byteKey, src []byte, isEncode bool) (rst string)

AES-CBC 返回 hex结果大写

func AESString

func AESString(byteKey, src []byte, isEncode bool) (rst string)

AES 返回 hex结果

func AESStringX

func AESStringX(byteKey, src []byte, isEncode bool) (rst string)

AES 返回 hex结果大写

func BEStringUint64

func BEStringUint64(src string) (v uint64)

转换BE bytes -> string到uint64

func Base62

func Base62(src int64) (rst string)

新浪微博base62编码

func Base62Url

func Base62Url(mid string) (url string)

新浪微博base62分组编码

func Catch

func Catch(err ...*error)

供给panic后恢复 (部分内部错误无法恢复) exmaple:

func xx() (err error){
  defer util.Catch(&err)
  ...
  util.Try(err)
}

func CheckIP

func CheckIP() (ip string, err error)

获取当前IP

func DeBase62

func DeBase62(src string) (rst int64)

新浪微博base62解码

func DeBase62Url

func DeBase62Url(url string) (mid string)

新浪微博base62分组解码

func DebugFlog

func DebugFlog(w io.Writer, args ...interface{})

调试时日志 输出到流

func DebugLog

func DebugLog(args ...interface{})

调试时日志 输出行号[调用深度还真头疼啊]

func DecodeJsHex

func DecodeJsHex(src string) []byte

解析js hex值 形如 \x00

func DecodeJsUint64BE

func DecodeJsUint64BE(src string) (uin uint64)

解析JS风格的64位长整形

func EncodeHexString

func EncodeHexString(src []byte) string

编码hex

func EncodeHexStringX

func EncodeHexStringX(src []byte) string

编码hex (大写)

func EncodeJsHex

func EncodeJsHex(src []byte) string

编码js hex值 形如 \x00

func EncodeJsUint64BE

func EncodeJsUint64BE(uin uint64) (src string)

返回JS风格的64位长整形 形如\x00\x00\x00\x00\x00\x00\x00\x01 (1)

func FetchImageAsStream

func FetchImageAsStream(url string) (r *bytes.Buffer)

下载图片(带缓存)

func FlushIP

func FlushIP() string

获取当前IP, 无缓存

func GetIP

func GetIP() string

获取当前IP, 有缓存

func GetIPLocal

func GetIPLocal() []string

获取当前内网IP

func HashCode

func HashCode(str string) (hash int)

快速Hash

func IsArrayDuplicate

func IsArrayDuplicate(arr []string, arr2 []string) bool

检查数组元素是否重复

func IsArrayDuplicateOpt

func IsArrayDuplicateOpt(arr []string, arr2 []string) bool

检查数组元素是否重复 - 性能优化版(空间换时间)

func IsInArray

func IsInArray(arr []string, sub string) bool

检查值是否在数组中

func IsNumber

func IsNumber(src string) bool

判断字符串时都是纯数字

func JsCurrentSecond

func JsCurrentSecond() int64

返回当前秒数

func JsCurrentTime

func JsCurrentTime() string

返回JS风格的当前时间字符串

func LineNumber

func LineNumber() string

func LineNumberAt

func LineNumberAt(deep int) string

func MD5

func MD5(src string) (rst []byte)

MD5 返回binary结果

func MD5String

func MD5String(src string) (rst string)

MD5 返回 hex结果

func MD5StringX

func MD5StringX(src string) (rst string)

MD5 返回 hex结果大写

func MustParseUrl

func MustParseUrl(rawurl string) *url.URL

无错返回ParseUrl

func MustReadAll

func MustReadAll(r io.Reader) []byte

无错返回ReadAll

func PKCS5Padding

func PKCS5Padding(ciphertext []byte, blockSize int) []byte

func PKCS5UnPadding

func PKCS5UnPadding(origData []byte) []byte

func ParseHost

func ParseHost(host string) (ip net.IP, port int)

解析Host 到IP+端口

func Random

func Random(n int) (result string)

生成N位随机数字

func RemoveBlock

func RemoveBlock(src, perfix, suffix string) (rst string)

使用前缀和后缀移除可嵌套的区块

func Sw

func Sw(condition bool, a, b interface{}) interface{}

三元选择

func Throw

func Throw(msg string)

抛出错误

func ToInt

func ToInt(src string) (rst int)

转换字符串到int

func ToInt64

func ToInt64(src string) (rst int64)

转换字符串到int64

func ToJson

func ToJson(args ...interface{}) string

unsafe

func ToString

func ToString(src ...interface{}) string

转换到字符串

func Try

func Try(err error)

出错崩溃

func Uint64BEString

func Uint64BEString(v uint64) string

转换uint64到BE bytes -> string

Types

type ArrayList

type ArrayList []interface{}

动态数组封装

func (ArrayList) Append

func (this ArrayList) Append(v interface{})

追加元素

func (ArrayList) AppendArray

func (this ArrayList) AppendArray(v []interface{})

追加数组

func (ArrayList) IndexOf

func (this ArrayList) IndexOf(val interface{}) int

返回数组元素位置

func (ArrayList) Len

func (this ArrayList) Len() int

返回长度

type FuncPool

type FuncPool map[string]interface{}

动态模式函数调用

func (FuncPool) Call

func (m FuncPool) Call(name string, args ...interface{}) (out []interface{}, err error)

调用

func (FuncPool) MustCall

func (m FuncPool) MustCall(name string, args ...interface{}) (out []interface{})

出错则panic

type IPInfo

type IPInfo struct {
	IP          string `json:"ip"`
	Pro         string `json:"pro"`
	ProCode     string `json:"proCode"`
	City        string `json:"city"`
	CityCode    string `json:"cityCode"`
	Region      string `json:"region"`
	RegionCode  string `json:"regionCode"`
	Addr        string `json:"addr"`
	RegionNames string `json:"regionNames"`
}

IP信息

func GetIPInfo

func GetIPInfo() *IPInfo

获取当前IP信息

type Key

type Key interface{}

type Logger

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

日志对象

func NewLogger

func NewLogger(w io.Writer, enable bool, perfix string) *Logger

创建新日志对象

func (*Logger) Enable

func (l *Logger) Enable() bool

返回启用状态

func (*Logger) Err

func (l *Logger) Err(s ...interface{})

输出日志和调试信息

func (*Logger) Errf

func (l *Logger) Errf(format string, s ...interface{})

func (*Logger) Log

func (l *Logger) Log(s ...interface{})

输出日志

func (*Logger) Logf

func (l *Logger) Logf(format string, s ...interface{})

func (*Logger) Output

func (l *Logger) Output() io.Writer

返回输出

func (*Logger) SetEnable

func (l *Logger) SetEnable(v bool)

设置启用状态

func (*Logger) SetOutput

func (l *Logger) SetOutput(v io.Writer)

设置输出

type SafeMap

type SafeMap struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Thread-safe map 线程安全的Map

func NewSafeMap

func NewSafeMap() (m *SafeMap)

func (*SafeMap) Bool

func (this *SafeMap) Bool(key Key) (v bool)

取值返回byte类型

func (*SafeMap) Byte

func (this *SafeMap) Byte(key Key) (v byte)

取值返回byte类型

func (*SafeMap) ContainsKey

func (this *SafeMap) ContainsKey(key Key) (v bool)

检查是否存在某键

func (*SafeMap) ContainsKeys

func (this *SafeMap) ContainsKeys(keys []Key) (v bool)

检查是否存在某几个键

func (*SafeMap) ContainsValue

func (this *SafeMap) ContainsValue(val Value) (v bool)

无索引下是O(n)遍历 慎用

func (*SafeMap) Delete

func (this *SafeMap) Delete(key Key)

删除

func (*SafeMap) DirtyContainsKey

func (this *SafeMap) DirtyContainsKey(key Key) (v bool)

检查是否存在某键

func (*SafeMap) DirtyContainsKeys

func (this *SafeMap) DirtyContainsKeys(keys []Key) (v bool)

检查是否存在某几个键

func (*SafeMap) DirtyDelete

func (this *SafeMap) DirtyDelete(key Key)

脏读 请勿在多线程下使用

func (*SafeMap) DirtyGet

func (this *SafeMap) DirtyGet(key Key) Value

脏读 请勿在多线程下使用

func (*SafeMap) DirtyItems

func (this *SafeMap) DirtyItems() map[Key]Value

全部返回 供range使用 在多线程下使用请手动进行 RLock(); defer RUnlock()

func (*SafeMap) DirtyMakeReverseIndex

func (this *SafeMap) DirtyMakeReverseIndex()

[脏的]对Value建立索引 原理是建立一个反向 map[Value]Key 建立索引后ContainValue()效率将变为O(1)

func (*SafeMap) DirtyPut

func (this *SafeMap) DirtyPut(key Key, v Value)

脏写 请勿在多线程下使用

func (*SafeMap) Float32

func (this *SafeMap) Float32(key Key) (v float32)

取值返回float32类型

func (*SafeMap) Float64

func (this *SafeMap) Float64(key Key) (v float64)

取值返回float64类型

func (*SafeMap) Get

func (this *SafeMap) Get(key Key) (v Value)

取出

func (*SafeMap) HaveReverseIndex

func (this *SafeMap) HaveReverseIndex() bool

检测是否建立Value反向索引

func (*SafeMap) Int

func (this *SafeMap) Int(key Key) (v int)

取值返回int类型

func (*SafeMap) Int16

func (this *SafeMap) Int16(key Key) (v int16)

取值返回int16类型

func (*SafeMap) Int32

func (this *SafeMap) Int32(key Key) (v int32)

取值返回int32类型

func (*SafeMap) Int64

func (this *SafeMap) Int64(key Key) (v int64)

取值返回int64类型

func (*SafeMap) Int8

func (this *SafeMap) Int8(key Key) (v int8)

取值返回int8类型

func (*SafeMap) Items

func (this *SafeMap) Items() (nv map[Key]Value)

返回全部数据的副本[很慢] 一般情况下请使用 DirtyItems()

func (*SafeMap) MakeReverseIndex

func (this *SafeMap) MakeReverseIndex()

[线程安全的]对Value建立索引 原理是建立一个反向 map[Value]Key 建立索引后ContainValue()效率将变为O(1) 但索引不会自动变化(因为影响效率) 需要手动重建

func (*SafeMap) Put

func (this *SafeMap) Put(key Key, v Value)

放入数据

func (*SafeMap) String

func (this *SafeMap) String(key Key) (v string)

取值返回string类型

func (*SafeMap) UInt

func (this *SafeMap) UInt(key Key) (v uint)

取值返回uint类型

func (*SafeMap) UInt16

func (this *SafeMap) UInt16(key Key) (v uint16)

取值返回int16类型

func (*SafeMap) UInt32

func (this *SafeMap) UInt32(key Key) (v uint32)

取值返回uint32类型

func (*SafeMap) UInt64

func (this *SafeMap) UInt64(key Key) (v uint64)

取值返回uint64类型

func (*SafeMap) UInt8

func (this *SafeMap) UInt8(key Key) (v uint8)

取值返回int8类型

type SimpleCookieJar

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

无域简单Jar

func NewSimpleCookieJar

func NewSimpleCookieJar() *SimpleCookieJar

创建新的简单Jar

func (*SimpleCookieJar) Cookies

func (this *SimpleCookieJar) Cookies(_ *url.URL) []*http.Cookie

实现接口 http.CookieJar.Cookies(u)

func (*SimpleCookieJar) SetCookies

func (this *SimpleCookieJar) SetCookies(_ *url.URL, cookies []*http.Cookie)

实现接口 http.CookieJar.SetCookies(u, cookies)

type Value

type Value interface{}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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