util

package module
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2024 License: Apache-2.0 Imports: 31 Imported by: 0

README

Util Build Status GoDoc Go Report Card

Golang util tools

Install

go get github.com/haormj/util

Usage

package main

import (
	"log"

	"github.com/haormj/util"
)

func main() {
	dst, err := util.ByteToUint32([]byte{1, 2, 3, 4})
	if err != nil {
		log.Fatalln(err)
	}
	log.Println(dst)
}

Documentation

Overview

Package util random string https://stackoverflow.com/questions/22892120/how-to-generate-a-random-string-of-a-fixed-length-in-go

Index

Constants

View Source
const (
	SO_ORIGINAL_DST      = 80 // from linux/include/uapi/linux/netfilter_ipv4.h
	IP6T_SO_ORIGINAL_DST = 80 // from linux/include/uapi/linux/netfilter_ipv6/ip6_tables.h
)
View Source
const (
	KB
	MB
	GB
	TB
	PB
)

Variables

View Source
var (
	ErrPeriodInvalid    = errors.New("invalid period")
	ErrPeriodStNegative = errors.New("start time is negative")
	ErrPeriodIllegalArg = errors.New("illegal argument")
)

Functions

func AnyToDecimal

func AnyToDecimal(b []int64, base int) int64

AnyToDecimal any to decimal

func ArrayContainsString

func ArrayContainsString(a []string, e string) bool

ArrayContainsString string contains

func ArrayDistinctInt

func ArrayDistinctInt(src []int) []int

ArrayDistinctInt array distinct

func ArrayIntersectString

func ArrayIntersectString(a []string, b []string) []string

ArrayIntersectString string intersect

func ArrayJoinInt

func ArrayJoinInt(a []int, sep string) string

ArrayJoinInt join int array

func ArrayJoinUint32

func ArrayJoinUint32(a []uint32, sep string) string

ArrayJoinUint32 join uint32 array

func ArrayReverseInt64

func ArrayReverseInt64(a []int64)

ArrayReverseInt64 int64 array reverse

func ArrayReverseString

func ArrayReverseString(a []string)

ArrayReverseString stirng array reverse

func ArraySortAscInt

func ArraySortAscInt(src []int)

ArraySortAscInt sort int array asc

func ArraySortDescInt

func ArraySortDescInt(src []int)

ArraySortDescInt sort int array desc

func ArraySymDiffString

func ArraySymDiffString(a []string, b []string) []string

ArraySymDiffString symmetric difference

func ArrayUnionString

func ArrayUnionString(a []string, b []string) []string

ArrayUnionString string union

func BytesCombine

func BytesCombine(pBytes ...[]byte) []byte

BytesCombine combine bytes

func BytesToHex

func BytesToHex(src []byte) string

BytesToHex bytes to hexadecimal

func BytesToUint32

func BytesToUint32(src []byte) (dst uint32, err error)

BytesToUint32 byte to uint32

func CST

func CST() *time.Location

CST get china location if error will get local location

func CSTE

func CSTE() (*time.Location, error)

CSTE Get CST location with error

func CurGoroutineID

func CurGoroutineID() uint64

CurGoroutineID get current go routine ID ref https://github.com/golang/net/blob/master/http2/gotrack.go#L51 not recommended http://lanlingzi.cn/post/technical/2016/0813_go_gls/

func DailyTask

func DailyTask(ctx context.Context, hour, minute, second int, loc *time.Location,
	callback CallbackFunc)

DailyTask daily execution

func DailyTaskCST

func DailyTaskCST(ctx context.Context, hour, minute, second int, callback CallbackFunc)

DailyTaskCST DailyTask for CST

func DailyTaskUTC

func DailyTaskUTC(ctx context.Context, hour, minute, second int, callback CallbackFunc)

DailyTaskUTC DailyTask for UTC

func DecimalToAny

func DecimalToAny(i int64, base int) []int64

DecimalToAny decimal to any ignore positive and negative

func GbkToUtf8 added in v1.3.0

func GbkToUtf8(in []byte) ([]byte, error)

GbkToUtf8 convert gbk to utf-8

func GetOriginalDst

func GetOriginalDst(c *net.TCPConn) (string, uint16, error)

GetOriginalDst Get the original destination of a TCP connection. inspired by github.com/missdeer/avege

func GetUUIDV1

func GetUUIDV1() string

func GetUUIDV1WithoutLine

func GetUUIDV1WithoutLine() string

func HmacSha1

func HmacSha1(p, key []byte) ([]byte, error)

HmacSha1 call golang hmac

func HmacSha256

func HmacSha256(p, key []byte) ([]byte, error)

HmacSha256 hmac sha256

func InitInterface

func InitInterface(i interface{}) interface{}

InitInterface init interface to zero value

func InitPointer

func InitPointer(src reflect.Type) (dst reflect.Value)

InitPointer with value

func InitWithZeroValue

func InitWithZeroValue(src reflect.Type) (dst reflect.Value)

InitWithZeroValue init type with default value

func InitWithZeroValueExcludePtr

func InitWithZeroValueExcludePtr(src reflect.Type) (dst reflect.Value)

InitWithZeroValueExcludePtr init type with default value if v.Kind() == reflect.Ptr, will init by zero value which point to example var a *int, a is not nil, *a = 0

func IsExported

func IsExported(name string) bool

IsExported Is this an exported - upper case - name?

func IsExportedOrBuiltinType

func IsExportedOrBuiltinType(t reflect.Type) bool

IsExportedOrBuiltinType Is this type exported or a builtin?

func Md5

func Md5(p []byte) ([]byte, error)

Md5 call golang md5

func Md5ToHex

func Md5ToHex(p []byte) (string, error)

Md5ToHex result to hex

func Millisecond

func Millisecond() int64

Millisecond get current milli second

func PeriodContains

func PeriodContains(t int64, p Period) bool

PeriodContains judge is t in period

func PeriodPartition

func PeriodPartition(p Period, interval int64) map[int64]Period

PeriodPartition split period by interval period not support et is negative

func PeriodsContains

func PeriodsContains(t int64, ps []Period) bool

PeriodsContains judge is t in periods

func PeriodsPartition

func PeriodsPartition(ps []Period, interval int64) map[int64][]Period

PeriodsPartition split periods by interval

func PeriodsSortAsc

func PeriodsSortAsc(ps []Period)

PeriodsSortAsc sort periods by asc

func PeriodsSortDesc

func PeriodsSortDesc(ps []Period)

PeriodsSortDesc sort periods by desc

func RandStringBytes

func RandStringBytes(n int) string

func RandStringBytesMask

func RandStringBytesMask(n int) string

func RandStringBytesMaskImpr

func RandStringBytesMaskImpr(n int) string

func RandStringBytesMaskImprSrc

func RandStringBytesMaskImprSrc(n int) string

func RandStringBytesRmndr

func RandStringBytesRmndr(n int) string

func RandStringRunes

func RandStringRunes(n int) string

func RandomBytes

func RandomBytes(n int) []byte

func RandomBytesE

func RandomBytesE(n int) ([]byte, error)

RandomBytesE random by rand.Reader it will use cpu more

func RandomString

func RandomString(source string, n int) string

func RandomStringByDigital added in v1.1.0

func RandomStringByDigital(n int) string

func RandomStringByLetter

func RandomStringByLetter(n int) string

func RandomStringByLetterAndDigital

func RandomStringByLetterAndDigital(n int) string

func SetStructField

func SetStructField(v interface{}, fieldValue interface{}, fieldNames ...string) bool

SetStructField v is ptr to struct

func Sha256

func Sha256(p []byte) ([]byte, error)

Sha256 golang sha256

func Sha256ToHex

func Sha256ToHex(p []byte) (string, error)

Sha256ToHex result to hex

func TrimSpaceAndSlash

func TrimSpaceAndSlash(s string) string

TrimSpaceAndSlash trim space first, then trim slash

func Uint32ToByte

func Uint32ToByte(src uint32) (dst []byte, err error)

Uint32ToByte uint32 to byte

func Utf8ToGbk added in v1.3.0

func Utf8ToGbk(in []byte) ([]byte, error)

Utf8ToGbk convert utf-8 to gbk

Types

type CallbackFunc

type CallbackFunc func()

CallbackFunc callback function

type Node

type Node struct {
	Parent   *Node
	Data     map[string]interface{}
	Children []*Node
	Format   func(*Node) string
}

Node definition

func NewNode

func NewNode() *Node

func (*Node) AddChild

func (n *Node) AddChild(c *Node)

AddChild -

func (*Node) BFS

func (n *Node) BFS(fn func(*Node))

BFS Breadth-First Search (BFS) is an algorithm for traversing or searching tree data structure. It starts at the tree root and explores the neighbor nodes first, before moving to the next level neighbors.

func (*Node) DFSPostOrder

func (n *Node) DFSPostOrder(fn func(*Node))

DFSPostOrder Depth-First Search (DFS) is an algorithm for traversing or searching tree data structure. One starts at the root and explores as far as possible along each branch before backtracking. parent comes after children; overall root last

func (*Node) DFSPreOrder

func (n *Node) DFSPreOrder(fn func(*Node))

DFSPreOrder Depth-First Search (DFS) is an algorithm for traversing or searching tree data structure. One starts at the root and explores as far as possible along each branch before backtracking. parent comes before children; overall root first

func (*Node) Degree

func (n *Node) Degree() int

Degree the number of children. A leaf is necessarily degree zero.

func (*Node) Depth

func (n *Node) Depth() int

Depth the number of edges between the node and the root.

func (*Node) Get

func (n *Node) Get(key string) (interface{}, bool)

func (*Node) Height

func (n *Node) Height() int

Height the number of edges on the longest path between that node and a leaf.

func (*Node) Insert

func (n *Node) Insert(key string, compare func(interface{}, interface{}) bool,
	values ...string) *Node

func (*Node) Level

func (n *Node) Level() int

Level 1 + the number of edges between the Node and the root

func (*Node) Put

func (n *Node) Put(key string, value interface{})

func (*Node) Search

func (n *Node) Search(key string, compare func(interface{}, interface{}) bool,
	values ...string) (*Node, bool)

func (*Node) String

func (n *Node) String() string

String

type Period

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

Period a length or portion of time. [StartTime, EndTime] StartTime must be >= 0 EndTime negative values represent positive infinity

func AddPeriodToResultSet

func AddPeriodToResultSet(p Period, rs []Period) []Period

AddPeriodToResultSet add a period to result set ResultSet is a set of period, all elements has been union

func NewPeriod

func NewPeriod(st, et int64) (Period, error)

NewPeriod create a period

func NewPeriods

func NewPeriods(se ...int64) ([]Period, error)

NewPeriods create a set of period

func PeriodDifference

func PeriodDifference(b Period, a Period) []Period

PeriodDifference b - a

func PeriodIntersection

func PeriodIntersection(p1 Period, p2 Period) []Period

PeriodIntersection intersect two period

func PeriodMinSuperSet

func PeriodMinSuperSet(p Period, interval int64) Period

PeriodMinSuperSet period mininum super set

func PeriodUnion

func PeriodUnion(p1 Period, p2 Period) []Period

PeriodUnion union two period

func PeriodsDifference

func PeriodsDifference(b []Period, a []Period) []Period

PeriodsDifference b - a

func PeriodsIntersection

func PeriodsIntersection(a []Period, b []Period) []Period

PeriodsIntersection intersect two periods

func PeriodsMinSuperSet

func PeriodsMinSuperSet(ps []Period, interval int64) []Period

PeriodsMinSuperSet periods mininum super set

func PeriodsUnion

func PeriodsUnion(a []Period, b []Period) []Period

PeriodsUnion union two periods

func (Period) Et

func (p Period) Et() int64

func (Period) St

func (p Period) St() int64

type SecurityString

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

func NewSecurityString

func NewSecurityString() *SecurityString

func (*SecurityString) Get

func (ss *SecurityString) Get() (data string)

func (*SecurityString) Set

func (ss *SecurityString) Set(data string)

type StructField

type StructField struct {
	Field      reflect.StructField
	FieldName  string
	FieldValue reflect.Value
	IsExist    bool
}

StructField struct filed

func GetStructField

func GetStructField(v interface{}, fieldNames ...string) *StructField

GetStructField get struct field

func (*StructField) Bool

func (sf *StructField) Bool() (bool, bool)

Bool convert StructField to bool

func (*StructField) BoolE

func (sf *StructField) BoolE() (bool, bool, error)

BoolE convert StructField to bool with error

func (*StructField) Float64

func (sf *StructField) Float64() (float64, bool)

Float64 convert StructField to float64

func (*StructField) Float64E

func (sf *StructField) Float64E() (float64, bool, error)

Float64E convert StructField to float64 with error

func (*StructField) Int

func (sf *StructField) Int() (int, bool)

Int convert StructField to int

func (*StructField) IntE

func (sf *StructField) IntE() (int, bool, error)

IntE convert StructField to int with error

func (*StructField) Interface

func (sf *StructField) Interface() (interface{}, bool)

Interface return StructField value

func (*StructField) String

func (sf *StructField) String() (string, bool)

String convert StructField to string

func (*StructField) StringE

func (sf *StructField) StringE() (string, bool, error)

StringE convert StructField to string with error

type Tree

type Tree struct {
	Root *Node
}

Tree definition

func (*Tree) BFS

func (t *Tree) BFS(fn func(*Node))

BFS Breadth-First Search (BFS) is an algorithm for traversing or searching tree data structure. It starts at the tree root and explores the neighbor nodes first, before moving to the next level neighbors.

func (*Tree) DFSPostOrder

func (t *Tree) DFSPostOrder(fn func(*Node))

DFSPostOrder Depth-First Search (DFS) is an algorithm for traversing or searching tree data structure. One starts at the root and explores as far as possible along each branch before backtracking. parent comes after children; overall root last

func (*Tree) DFSPreOrder

func (t *Tree) DFSPreOrder(fn func(*Node))

DFSPreOrder Depth-First Search (DFS) is an algorithm for traversing or searching tree data structure. One starts at the root and explores as far as possible along each branch before backtracking. parent comes before children; overall root first

func (*Tree) Degree

func (t *Tree) Degree() int

Degree the largest degree of the node

func (*Tree) Depth

func (t *Tree) Depth() int

Depth the largest depth of the node

func (*Tree) Height

func (t *Tree) Height() int

Height is the length of the longest path to a leaf

func (*Tree) Insert

func (t *Tree) Insert(key string, compare func(interface{}, interface{}) bool,
	values ...string) *Node

func (*Tree) Level

func (t *Tree) Level() int

Level 1 + the number of edges between the node and the root

func (*Tree) Search

func (t *Tree) Search(key string, compare func(interface{}, interface{}) bool,
	values ...string) (*Node, bool)

Search node by key

func (*Tree) String

func (t *Tree) String() string

Directories

Path Synopsis
lru
ttl
aes
Package aes doc pkcs #7 https://my.oschina.net/u/1023800/blog/500546
Package aes doc pkcs #7 https://my.oschina.net/u/1023800/blog/500546
example

Jump to

Keyboard shortcuts

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