com

package module
v0.0.0-...-6d6cf9e Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2015 License: MIT Imports: 27 Imported by: 7

README

com

常用方法

Documentation

Overview

Package profile provides a simple way to manage runtime/pprof profiling of your Go application.

Package randutil provides various convenience functions for dealing with random numbers and strings.

Index

Constants

View Source
const (
	Byte  = 1
	KByte = Byte * 1024
	MByte = KByte * 1024
	GByte = MByte * 1024
	TByte = GByte * 1024
	PByte = TByte * 1024
	EByte = PByte * 1024
)

Storage unit constants.

View Source
const (
	// Set of characters to use for generating random strings
	Alphabet     = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
	Numerals     = "1234567890"
	Alphanumeric = Alphabet + Numerals
	Ascii        = Alphanumeric + "~!@#$%^&*()-_+={}[]\\|<,>.?/\"';:`"
)

Variables

View Source
var (
	CPUProfile = &Config{
		CPUProfile: true,
	}

	MemProfile = &Config{
		MemProfile: true,
	}

	BlockProfile = &Config{
		BlockProfile: true,
	}
)
View Source
var MinMaxError = errors.New("Min cannot be greater than max.")

Functions

func AESDecrypt

func AESDecrypt(key, text []byte) ([]byte, error)

AESDecrypt decrypts text and given key with AES.

func AESEncrypt

func AESEncrypt(key, text []byte) ([]byte, error)

AESEncrypt encrypts text and given key with AES.

func AlphaString

func AlphaString(n int) (string, error)

AlphaString returns a random alphanumeric string n characters long.

func AlphaStringRange

func AlphaStringRange(min, max int) (string, error)

AlphaRange returns a random alphanumeric string at least min and no more than max characters long.

func ChoiceInt

func ChoiceInt(choices []int) (int, error)

ChoiceInt returns a random selection from an array of integers.

func ChoiceString

func ChoiceString(choices []string) (string, error)

ChoiceString returns a random selection from an array of strings.

func Copy

func Copy(src, dest string) error

Copy copies file from source to target path.

func CopyExportedStructFields

func CopyExportedStructFields(src, dstPtr interface{}) (copied int)

CopyExportedStructFields copies all exported struct fields from src that are assignable to their name siblings at dstPtr to dstPtr. src can be a struct or a pointer to a struct, dstPtr must be a pointer to a struct.

func CopyExportedStructFieldsVal

func CopyExportedStructFieldsVal(src, dst reflect.Value) (copied int)

func Expand

func Expand(template string, match map[string]string, subs ...string) string

Expand replaces {k} in template with match[k] or subs[atoi(k)] if k is not in match.

func ExportedStructFields

func ExportedStructFields(v reflect.Value) map[string]reflect.Value

func Fields

func Fields(obj interface{}) ([]string, error)

Fields returns the struct fields names list. obj can whether be a structure or pointer to structure.

func FileMTime

func FileMTime(file string) (int64, error)

FileMTime returns file modified time and possible error.

func FileSize

func FileSize(file string) (int64, error)

FileSize returns file size in bytes and possible error.

func GetField

func GetField(obj interface{}, name string) (interface{}, error)

GetField returns the value of the provided obj field. obj can whether be a structure or pointer to structure.

func GetFieldKind

func GetFieldKind(obj interface{}, name string) (reflect.Kind, error)

GetFieldKind returns the kind of the provided obj field. obj can whether be a structure or pointer to structure.

func GetFieldTag

func GetFieldTag(obj interface{}, fieldName, tagKey string) (string, error)

GetFieldTag returns the provided obj field tag value. obj can whether be a structure or pointer to structure.

func GetMethods

func GetMethods(v interface{}) map[string][]string

func GetMidnightSeconds

func GetMidnightSeconds() int64

返回距离午夜的秒数

func HasField

func HasField(obj interface{}, name string) (bool, error)

HasField checks if the provided field name is part of a struct. obj can whether be a structure or pointer to structure.

func HexStr2int

func HexStr2int(hexStr string) (int, error)

HexStr2int converts hex format string to decimal number.

func HumaneFileSize

func HumaneFileSize(s uint64) string

HumaneFileSize calculates the file size and generate user-friendly string.

func Int2HexStr

func Int2HexStr(num int) (hex string)

Int2HexStr converts decimal number to hex format string.

func Int64ToIp

func Int64ToIp(ip int64) string

func IntRange

func IntRange(min, max int) (int, error)

IntRange returns a random integer in the range from min to max.

func Invoke

func Invoke(any interface{}, name string, args ...interface{}) (result []reflect.Value)

func IpToInt64

func IpToInt64(ip string) (int64, error)

func IsDir

func IsDir(dir string) bool

IsDir returns true if given path is a directory, or returns false when it's a file or does not exist.

func IsEmptyValue

func IsEmptyValue(v interface{}) bool

func IsExist

func IsExist(path string) bool

IsExist checks whether a file or directory exists. It returns false when the file or directory does not exist.

func IsExportedField

func IsExportedField(structField reflect.StructField) bool

func IsExportedName

func IsExportedName(name string) bool

func IsFile

func IsFile(filePath string) bool

IsFile returns true if given path is a file, or returns false when it's a directory or does not exist.

func IsLetter

func IsLetter(l uint8) bool

IsLetter returns true if the 'l' is an English letter.

func IsSliceContainsStr

func IsSliceContainsStr(slice []string, str string) bool

func Items

func Items(obj interface{}) (map[string]interface{}, error)

Items returns the field - value struct pairs as a map. obj can whether be a structure or pointer to structure.

func PowInt

func PowInt(x int, y int) int

PowInt is int type of math.Pow function.

func RandomCreateBytes

func RandomCreateBytes(n int, alphabets ...byte) []byte

RandomCreateBytes generate random []byte by specify chars.

func Reverse

func Reverse(s string) string

Reverse s string, support unicode

func SetField

func SetField(obj interface{}, name string, value interface{}) error

SetField sets the provided obj field with provided value. obj param has to be a pointer to a struct, otherwise it will soundly fail. Provided value type should match with the struct field you're trying to set.

func Start

func Start(cfg *Config) interface {
	Stop()
}

Start starts a new profiling session configured using *Config. The caller should call the Stop method on the value returned to cleanly stop profiling. Passing a nil *Config is the same as passing a *Config with defaults chosen.

func String

func String(n int, charset string) (string, error)

String returns a random string n characters long, composed of entities from charset.

func StringRange

func StringRange(min, max int, charset string) (string, error)

StringRange returns a random string at least min and no more than max characters long, composed of entitites from charset.

func Tags

func Tags(obj interface{}, key string) (map[string]string, error)

Tags lists the struct tag fields. obj can whether be a structure or pointer to structure.

func ToSnakeCase

func ToSnakeCase(str string) string

ToSnakeCase can convert all upper case characters in a string to underscore format.

Some samples.

"FirstName"  => "first_name"
"HTTPServer" => "http_server"
"NoHTTPS"    => "no_https"
"GO_PATH"    => "go_path"
"GO PATH"    => "go_path"      // space is converted to underscore.
"GO-PATH"    => "go_path"      // hyphen is converted to underscore.

From https://github.com/huandu/xstrings

func ToStr

func ToStr(value interface{}, args ...int) (s string)

Convert any type to string.

func WriteFile

func WriteFile(filename string, data []byte) error

WriteFile writes data to a file named by filename. If the file does not exist, WriteFile creates it and its upper level paths.

Types

type Choice

type Choice struct {
	Weight int
	Item   interface{}
}

A Choice contains a generic item and a weight controlling the frequency with which it will be selected.

func WeightedChoice

func WeightedChoice(choices []Choice) (Choice, error)

WeightedChoice used weighted random selection to return one of the supplied choices. Weights of 0 are never selected. All other weight values are relative. E.g. if you have two choices both weighted 3, they will be returned equally often; and each will be returned 3 times as often as a choice weighted 1.

type Config

type Config struct {
	// Quiet suppresses informational messages during profiling.
	Quiet bool

	// CPUProfile controls if cpu profiling will be enabled.
	// It defaults to false.
	CPUProfile bool

	// MemProfile controls if memory profiling will be enabled.
	// It defaults to false.
	MemProfile bool

	// BlockProfile controls if block (contention) profiling will
	// be enabled.
	// It defaults to false.
	BlockProfile bool

	// ProfilePath controls the base path where various profiling
	// files are written. If blank, the base path will be generated
	// by ioutil.TempDir.
	ProfilePath string

	// NoShutdownHook controls whether the profiling package should
	// hook SIGINT to write profiles cleanly.
	// Programs with more sophisticated signal handling should set
	// this to true and ensure the Stop() function returned from Start()
	// is called during shutdown.
	NoShutdownHook bool
}

Config controls the operation of the profile package.

type StrTo

type StrTo string

Convert string to specify type.

func (StrTo) Exist

func (f StrTo) Exist() bool

func (StrTo) Int

func (f StrTo) Int() (int, error)

func (StrTo) Int64

func (f StrTo) Int64() (int64, error)

func (StrTo) MustInt

func (f StrTo) MustInt() int

func (StrTo) MustInt64

func (f StrTo) MustInt64() int64

func (StrTo) MustUint8

func (f StrTo) MustUint8() uint8

func (StrTo) String

func (f StrTo) String() string

func (StrTo) Uint8

func (f StrTo) Uint8() (uint8, error)

type Trie

type Trie struct {
	Root *TrieNode
}

func NewTrie

func NewTrie() *Trie

func (*Trie) Add

func (t *Trie) Add(txt string)

新增要过滤的词

func (*Trie) Find

func (t *Trie) Find(txt string) bool

屏蔽字搜索查询

func (*Trie) Replace

func (t *Trie) Replace(txt string) (string, []string)

屏蔽字搜索替换

type TrieNode

type TrieNode struct {
	Children map[rune]*TrieNode
	End      bool
}

func NewTrieNode

func NewTrieNode() *TrieNode

Directories

Path Synopsis
Package randutil provides various convenience functions for dealing with random numbers and strings.
Package randutil provides various convenience functions for dealing with random numbers and strings.

Jump to

Keyboard shortcuts

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