util

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Dec 11, 2023 License: BSD-2-Clause Imports: 30 Imported by: 0

Documentation

Overview

数据转换

Index

Constants

View Source
const (
	CodeOk        int32 = 0
	CodeUniversal       = 1
	CodeUnknown         = 2
	CodeInvalid         = 3
	CodeExists          = 4

	CodeStarted  = 90
	CodeFinished = 91
	CodeOther    = 99

	// HTTP Status Code (100 ~ 600)
	CodeUnauthorized = 401
	CodeForbidden    = 403
	CodeNotFound     = 404
	CodeTimeout      = 408

	CodeServerError        = 500
	CodeServiceUnavailable = 503

	ErrNewPlayer    // 创建玩家失败
	ErrTokenEmpty   // token is empty
	ErrTokenDecrypt // token decrypt failed
	ErrTokenFormat  // token format is invalid
	ErrTokenExpired // token expired
	UnknownError    // 未知错误
	ErrParam        // 参数错误
	ErrParamNil     // 请求参数为空
	ErrParse        // 解析失败
	ErrDB           // 数据库操作失败
	ErrRedis        // 缓存操作失败
	ErrConnect      // 连接失败
)

Game System Error Code (1 ~ 99)

View Source
const (
	MyTimeInterval = 60 * 10
	MyTimeExpired  = 60 * 60 * 24
)

Variables

View Source
var (
	RefTypeStr     = reflect.TypeOf("")
	RefTypeInt     = reflect.TypeOf(0)
	RefTypeInt32   = reflect.TypeOf(int32(0))
	RefTypeInt64   = reflect.TypeOf(int64(0))
	RefTypeFloat32 = reflect.TypeOf(float32(0))
	RefTypeFloat64 = reflect.TypeOf(float64(0))
	RefTypeBool    = reflect.TypeOf(false)
)
View Source
var (
	Lang = map[string]*Status{
		"en-us": NewStatus("en-us"),
		"zh-cn": NewStatus("zh-cn"),
	}
)

Functions

func AddStatus

func AddStatus(status *Status)

AddStatus 注册语言环境

func AddStatusText

func AddStatusText(lang string, code int32, text string)

AddStatusText 注册语言环境信息

func AppendPrivateBlocks

func AppendPrivateBlocks(bs ...string)

AppendPrivateBlocks append private network blocks

func BoolElem

func BoolElem() reflect.Value

func BoolSliceElem

func BoolSliceElem() reflect.Value

func Cause

func Cause(err error) error

Cause returns the underlying cause of the error, if possible. An error value has a cause if it implements the following interface:

type causer interface {
       Cause() error
}

If the error does not implement Cause, the original error will be returned. If the error is nil, nil will be returned without further investigation.

func Certificate

func Certificate(host ...string) (tls.Certificate, error)

func DeepCopy

func DeepCopy(dst, src interface{}) error

深度拷贝对象

func Elem

func Elem(v reflect.Type) reflect.Value

func Extract

func Extract(addr string) (string, error)

Extract returns a real ip

func Float32Elem

func Float32Elem() reflect.Value

func Float32SliceElem

func Float32SliceElem() reflect.Value

func Float64Elem

func Float64Elem() reflect.Value

func Float64SliceElem

func Float64SliceElem() reflect.Value

func GenTK

func GenTK(account string) string

GenTK 生成一个加密的token

func GetText

func GetText(lang string, code int32) string

GetText 获取语言环境信息

func HostPort

func HostPort(addr string, port interface{}) string

HostPort format addr and port suitable for dial

func IPs

func IPs() []string

IPs returns all known ips

func InFloat32Slice added in v0.0.4

func InFloat32Slice(v float32, sl []float32) bool

InInt64Slice checks given int64 in int64 slice or not.

func InFloat64Slice added in v0.0.4

func InFloat64Slice(v float64, sl []float64) bool

InInt64Slice checks given int64 in int64 slice or not.

func InInt32Slice added in v0.0.4

func InInt32Slice(v int32, sl []int32) bool

InInt32Slice checks given int in int slice or not.

func InInt64Slice added in v0.0.4

func InInt64Slice(v int64, sl []int64) bool

InInt64Slice checks given int64 in int64 slice or not.

func InIntSlice added in v0.0.4

func InIntSlice(v int, sl []int) bool

InIntSlice checks given int in int slice or not.

func InSlice added in v0.0.4

func InSlice(v interface{}, slice interface{}) bool

InSlice checks given interface in interface slice.

func InStrSlice added in v0.0.4

func InStrSlice(v string, sl []string) bool

InStrSlice checks given string in string slice or not.

func Int32Elem

func Int32Elem() reflect.Value

func Int32MapElem

func Int32MapElem(v reflect.Type) reflect.Value

func Int32SliceElem

func Int32SliceElem() reflect.Value

func Int32SliceUnique added in v0.0.4

func Int32SliceUnique(slice []int32) (uniqueslice []int32)

Int32SliceUnique cleans repeated values in slice.

func Int64Elem

func Int64Elem() reflect.Value

func Int64MapElem

func Int64MapElem(v reflect.Type) reflect.Value

func Int64SliceElem

func Int64SliceElem() reflect.Value

func Int64SliceIface added in v0.0.4

func Int64SliceIface(slice []int64) (result []interface{})

Int64SliceIface 数字切片转 []interface{}

func Int64SliceUnique added in v0.0.4

func Int64SliceUnique(slice []int64) (uniqueslice []int64)

Int64SliceUnique cleans repeated values in slice.

func IntElem

func IntElem() reflect.Value

func IntMapElem

func IntMapElem(v reflect.Type) reflect.Value

Int Map

func IntSliceElem

func IntSliceElem() reflect.Value

func IntSliceIface added in v0.0.4

func IntSliceIface(slice []int) (result []interface{})

IntSliceIface 数字切片转 []interface{}

func Is

func Is(a, b error) bool

Is 比较两个错误

func IsCode

func IsCode(err error, code int32) bool

IsCode 检查错误码

func IsLocal

func IsLocal(addr string) bool

IsLocal tells us whether an ip is local

func IsStack

func IsStack() bool

func Listen

func Listen(addr string, fn func(string) (net.Listener, error)) (net.Listener, error)

Listen takes addr:portmin-portmax and binds to the first available port Example: Listen("localhost:5000-6000", fn)

func MapElem

func MapElem(k, v reflect.Type) reflect.Value

反射创建MAP (以 .Addr.Interface() 获取指针数据)

func ParseBool

func ParseBool(v interface{}) bool

ParseBool convert interface to bool.

func ParseFloat32

func ParseFloat32(v interface{}) float32

ParseFloat32 convert interface to float32.

func ParseFloat64

func ParseFloat64(v interface{}) float64

ParseFloat64 convert interface to float64.

func ParseInt

func ParseInt(v interface{}) int

ParseInt 转为int

func ParseInt32

func ParseInt32(v interface{}) int32

ParseInt32 convert interface to int32.

func ParseInt64

func ParseInt64(v interface{}) int64

ParseInt64 convert interface to int64.

func ParseSlice

func ParseSlice(v interface{}) []interface{}

func ParseStr

func ParseStr(v interface{}) string

ParseStr 转为字符串

func ParseStrSlice

func ParseStrSlice(v []interface{}) []string

ParseStrSlice convert []interface to []string

func ParseTK

func ParseTK(token string) (string, error)

func ParseUint

func ParseUint(v interface{}) uint

ParseUint convert interface to int.

func ParseUint32

func ParseUint32(v interface{}) uint32

ParseUint32 convert interface to int32.

func ParseUint64

func ParseUint64(v interface{}) uint64

ParseUint64 convert interface to int64.

func Proxy

func Proxy(service string, address []string) (string, []string, bool)

Proxy returns the proxy and the address if it exits

func Ptr

func Ptr(v reflect.Type) reflect.Value

反射创建结构体 (以 .Addr.Interface() 获取指针数据)

func RandSlice added in v0.0.4

func RandSlice(slice interface{})

切片乱序(在原对象的基础上)

func SetDefaultLang

func SetDefaultLang(lang string)

SetDefaultLang 设置默认语言环境

func SliceChunk added in v0.0.4

func SliceChunk(slice []interface{}, size int) (chunkslice [][]interface{})

SliceChunk separates one slice to some sized slice.

func SliceDiff added in v0.0.4

func SliceDiff(slice1, slice2 []interface{}) (diffslice []interface{})

SliceDiff returns diff slice of slice1 - slice2.

func SliceElem

func SliceElem(v reflect.Type) reflect.Value

反射创建切片 (以 .Addr.Interface() 获取指针数据)

func SliceFilter added in v0.0.4

func SliceFilter(slice []interface{}, a filtertype) (ftslice []interface{})

SliceFilter generates a new slice after filter function.

func SliceIntersect added in v0.0.4

func SliceIntersect(slice1, slice2 []interface{}) (diffslice []interface{})

SliceIntersect returns slice that are present in all the slice1 and slice2.

func SliceMerge added in v0.0.4

func SliceMerge(slice1, slice2 []interface{}) (c []interface{})

SliceMerge merges interface slices to one slice.

func SlicePad added in v0.0.4

func SlicePad(slice []interface{}, size int, val interface{}) []interface{}

SlicePad prepends size number of val into slice.

func SliceRand added in v0.0.4

func SliceRand(a []interface{}) (b interface{})

SliceRand returns random one from slice.

func SliceRandList added in v0.0.4

func SliceRandList(min, max int) []int

SliceRandList generate an int slice from min to max.

func SliceRandStr added in v0.0.4

func SliceRandStr(a []string) (b string)

SliceRand returns random one from slice.

func SliceRange added in v0.0.4

func SliceRange(start, end, step int64) (intslice []int64)

SliceRange generates a new slice from begin to end with step duration of int64 number.

func SliceReduce added in v0.0.4

func SliceReduce(slice []interface{}, a reducetype) (dslice []interface{})

SliceReduce generates a new slice after parsing every value by reduce function

func SliceShuffle added in v0.0.4

func SliceShuffle(slice []interface{}) []interface{}

SliceShuffle shuffles a slice.

func SliceShuffleInt added in v0.0.4

func SliceShuffleInt(slice []int) []int

SliceShuffleInt shuffles a int slice.

func SliceSum added in v0.0.4

func SliceSum(intslice []int64) (sum int64)

SliceSum sums all values in int64 slice.

func SliceTwoElem

func SliceTwoElem(v reflect.Type) reflect.Value

反射创建切片 (以 .Addr.Interface() 获取指针数据)

func SliceUnique added in v0.0.4

func SliceUnique(slice []interface{}) (uniqueslice []interface{})

SliceUnique cleans repeated values in slice.

func SplitStrSlice added in v0.0.4

func SplitStrSlice(str string, sep string) []interface{}

SplitStrSlice 获取字符串切片

func StatusText

func StatusText(code int32) string

StatusText 获取默认语言环境

func StrElem

func StrElem() reflect.Value

func StrMapElem

func StrMapElem(v reflect.Type) reflect.Value

Str Map

func StrSliceElem

func StrSliceElem() reflect.Value

func StrSliceIface added in v0.0.4

func StrSliceIface(slice []string) (result []interface{})

StrSliceIface 字符串切片转 []interface{}

func StrSliceUnique added in v0.0.4

func StrSliceUnique(slice []string) (uniqueslice []string)

StrSliceUnique cleans repeated values in slice.

func StructToDict

func StructToDict(data interface{}) map[string]interface{}

StructToDict 结构体转字典 (不支持嵌套)

func ToDict

func ToDict(data interface{}) map[string]interface{}

ToDict 转换为字典数据(MAP)

func ToJson

func ToJson(data interface{}) []byte

ToJson 转换为JSON数据

func ToStruct

func ToStruct(data interface{}, obj interface{}) error

ToStruct 转换结构体数据

func Wrap

func Wrap(err error, args ...interface{}) error

WithStack annotates err with a stack trace at the point WithStack was called. If err is nil, WithStack returns nil.

Types

type Buffer

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

Buffer is ring buffer

func NewBuffer

func NewBuffer(i int) *Buffer

New returns a new buffer of the given size

func (*Buffer) Get

func (b *Buffer) Get(n int) []*Entry

Get returns the last n entries

func (*Buffer) Put

func (b *Buffer) Put(v interface{})

Put adds a new value to ring buffer

func (*Buffer) Since

func (b *Buffer) Since(t time.Time) []*Entry

Return the entries since a specific time

func (*Buffer) Size

func (b *Buffer) Size() int

Size returns the size of the ring buffer

func (*Buffer) Stream

func (b *Buffer) Stream() (<-chan *Entry, chan bool)

Stream logs from the buffer Close the channel when you want to stop

type Entry

type Entry struct {
	Value     interface{}
	Timestamp time.Time
}

Entry is ring buffer data entry

type Error

type Error struct {
	Code   int32  `json:"code"`
	Status string `json:"status"`
	Detail string `json:"detail"`
	// contains filtered or unexported fields
}

func Exists

func Exists(args ...interface{}) *Error

Exists 已存在

func Finished

func Finished(args ...interface{}) *Error

Finished 已结束

func Forbidden

func Forbidden(args ...interface{}) *Error

Forbidden 拒绝访问

func Invalid

func Invalid(args ...interface{}) *Error

Invalid 无效

func New

func New(code int32, a ...interface{}) *Error

New returns an error with the supplied proto. New also records the stack trace at the point it was called.

func NotFound

func NotFound(args ...interface{}) *Error

NotFound 不存在

func Other

func Other(args ...interface{}) *Error

Other 其它错误

func Parse

func Parse(err error) *Error

func Server

func Server(args ...interface{}) *Error

Server 服务器错误

func Started

func Started(args ...interface{}) *Error

Started 已开始

func Timeout

func Timeout(args ...interface{}) *Error

Timeout 请求超时

func Unauthorized

func Unauthorized(args ...interface{}) *Error

Unauthorized 未授权

func Unavailable

func Unavailable(args ...interface{}) *Error

Unavailable 不可用

func Universal

func Universal(args ...interface{}) *Error

Universal 通用错误

func Unknown

func Unknown(args ...interface{}) *Error

Unknown 未知错误

func (*Error) Error

func (e *Error) Error() string

func (*Error) Format

func (e *Error) Format(s fmt.State, verb rune)

func (Error) StackTrace

func (s Error) StackTrace() StackTrace

type Frame

type Frame uintptr

Frame represents a program counter inside a stack frame. For historical reasons if Frame is interpreted as a uintptr its value represents the program counter + 1.

func (Frame) Format

func (f Frame) Format(s fmt.State, verb rune)

Format formats the frame according to the fmt.Formatter interface.

%s    source file
%d    source line
%n    function name
%v    equivalent to %s:%d

Format accepts flags.go that alter the printing of some verbs, as follows:

%+s   function name and path of source file relative to the compile time
      GOPATH separated by \n\t (<funcname>\n\t<path>)
%+v   equivalent to %+s:%d

func (Frame) MarshalText

func (f Frame) MarshalText() ([]byte, error)

MarshalText formats a stacktrace Frame as a text string. The output is the same as that of fmt.Sprintf("%+v", f), but without newlines or tabs.

type StackTrace

type StackTrace []Frame

StackTrace is stack of Frames from innermost (newest) to outermost (oldest).

func (StackTrace) Format

func (st StackTrace) Format(s fmt.State, verb rune)

Format formats the stack of Frames according to the fmt.Formatter interface.

%s	lists source files for each Frame in the stack
%v	lists the source file and line number for each Frame in the stack

Format accepts flags.go that alter the printing of some verbs, as follows:

%+v   Prints filename, function, and line number for each Frame in the stack.

type Status

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

Status 语言包

func GetStatus

func GetStatus(lang string) *Status

GetStatus 注册语言环境

func NewStatus

func NewStatus(lang string) *Status

func (*Status) Add

func (s *Status) Add(code int32, text string)

func (*Status) Lang

func (s *Status) Lang() string

func (*Status) Status

func (s *Status) Status() map[int32]string

func (*Status) Text

func (s *Status) Text(code int32) string

type Stream

type Stream struct {
	// Id of the stream
	Id string
	// Buffered entries
	Entries chan *Entry
	// Stop channel
	Stop chan bool
}

Stream is used to stream the buffer

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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