commonutils

package
v0.0.0-...-bade87c Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2023 License: GPL-3.0 Imports: 29 Imported by: 0

README

常用的 golang 公共库

其他代码的来源

checkmail:https://github.com/badoux/checkmail

Documentation

Index

Constants

View Source
const (
	HashSHA1 = iota
	HashSHA256
	HashSHA512
	HashSHA512_384
	MD5New
	SatoshisPerBTC = 100000000
	SatoshisPerLTC = 100000000
	WeiPerEther    = 1000000000000000000
)

Const declarations for common.go operations

Variables

This section is empty.

Functions

func Base64Decode

func Base64Decode(input string) ([]byte, error)

Base64Decode takes in a Base64 string and returns a byte array and an error

func Base64Encode

func Base64Encode(input []byte) string

Base64Encode takes in a byte array then returns an encoded base64 string

func ByteArrayToString

func ByteArrayToString(input []byte) string

ByteArrayToString returns a string

func CalculateAmountWithFee

func CalculateAmountWithFee(amount, fee float64) float64

CalculateAmountWithFee returns a calculated fee included amount on fee

func CalculateFee

func CalculateFee(amount, fee float64) float64

CalculateFee returns a simple fee on amount

func CalculateNetProfit

func CalculateNetProfit(amount, priceThen, priceNow, costs float64) float64

CalculateNetProfit returns net profit

func CalculatePercentageDifference

func CalculatePercentageDifference(amount, secondAmount float64) float64

CalculatePercentageDifference returns the percentage of difference between multiple time periods

func CalculatePercentageGainOrLoss

func CalculatePercentageGainOrLoss(priceNow, priceThen float64) float64

CalculatePercentageGainOrLoss returns the percentage rise over a certain period

func CreateDir

func CreateDir(dir string) error

CreateDir creates a directory based on the supplied parameter

func EncodeURLValues

func EncodeURLValues(url string, values url.Values) string

EncodeURLValues concatenates url values onto a url string and returns a string

func ExtractHost

func ExtractHost(address string) string

ExtractHost returns the hostname out of a string

func ExtractPort

func ExtractPort(host string) int

ExtractPort returns the port name out of a string

func FloatFromString

func FloatFromString(raw interface{}) (float64, error)

FloatFromString format

func FloatFromStringDontRound

func FloatFromStringDontRound(num float64, exp int) (result float64, err error)

FloatFromStringDontRound 不需要小数点四舍五入,直接取位数

func FormatDecimalFloat64

func FormatDecimalFloat64(value float64, exp int32) float64

FormatDecimalFloat64 获取精度计算后的数量String @param {[type]} this [description] @return {[type]} [description]

func FormatDecimalString

func FormatDecimalString(value float64, exp int32) string

FormatDecimalString 格式化后的字符串 @param {[type]} this [description] @return {[type]} [description] formatDecimal_String("0.000163786", -6) //"0.000164"

func FormatMapStringToString

func FormatMapStringToString(msg string, fileds map[string]interface{}) string

FormatMapStringToString 将 map[string]interface{} 格式化成字符串 commonutils.FormatMapStringToString("买进补仓", logFileds)

func GetCaller

func GetCaller() (filePath, fileName, funcName string, lineNum int)

func GetExecutablePath

func GetExecutablePath() (string, error)

GetExecutablePath returns the executables launch path

func GetFuncLine

func GetFuncLine() int

GetFuncLine 获取调用方法的行号

func GetFuncName

func GetFuncName() string

GetFuncName 获取调用方法的名称

func GetHMAC

func GetHMAC(hashType int, input, key []byte) []byte

GetHMAC returns a keyed-hash message authentication code using the desired hashtype

func GetMD5

func GetMD5(input []byte) []byte

GetMD5 returns a MD5 hash of a byte array

func GetOSPathSlash

func GetOSPathSlash() string

GetOSPathSlash returns the slash used by the operating systems file system

func GetRandomSalt

func GetRandomSalt(input []byte, saltLen int) ([]byte, error)

GetRandomSalt returns a random salt

func GetSHA256

func GetSHA256(input []byte) []byte

GetSHA256 returns a SHA256 hash of a byte array

func GetSHA512

func GetSHA512(input []byte) []byte

GetSHA512 returns a SHA512 hash of a byte array

func GetURIPath

func GetURIPath(uri string) string

GetURIPath returns the path of a URL given a URI

func HexDecodeToBytes

func HexDecodeToBytes(input string) ([]byte, error)

HexDecodeToBytes takes in a hexadecimal string and returns a byte array

func HexEncodeToString

func HexEncodeToString(input []byte) string

HexEncodeToString takes in a hexadecimal byte array and returns a string

func Int32FromString

func Int32FromString(raw interface{}) (int32, error)

Int32FromString format

func Int32ToString

func Int32ToString(n int32) string

Int32ToString format

func Int64DataCompare

func Int64DataCompare(haystack []int64, needle int64) bool

Int64DataCompare data checks the substring array with an input and returns a bool

func Int64FromString

func Int64FromString(raw interface{}) (int64, error)

Int64FromString format

func IntDataCompare

func IntDataCompare(haystack []int, needle int) bool

IntDataCompare data checks the substring array with an input and returns a bool

func IntFromString

func IntFromString(raw interface{}) (int, error)

IntFromString format

func IsEnabled

func IsEnabled(isEnabled bool) string

IsEnabled takes in a boolean param and returns a string if it is enabled or disabled

func IsValidCryptoAddress

func IsValidCryptoAddress(address, crypto string) (bool, error)

IsValidCryptoAddress validates your cryptocurrency address string using the regexp package // Validation issues occurring because "3" is contained in litecoin and Bitcoin addresses - non-fatal

func JSONDecode

func JSONDecode(data []byte, to interface{}) error

JSONDecode decodes JSON data into a structure

func JSONEncode

func JSONEncode(v interface{}) ([]byte, error)

JSONEncode encodes structure data into JSON

func JoinStrings

func JoinStrings(input []string, separator string) string

JoinStrings joins an array together with the required separator and returns it as a string

func NewHTTPClientWithTimeout

func NewHTTPClientWithTimeout(t time.Duration) *http.Client

NewHTTPClientWithTimeout initialises a new HTTP client with the specified timeout duration

func OutputCSV

func OutputCSV(path string, data [][]string) error

OutputCSV dumps data into a file as comma-separated values

func PathExists

func PathExists(path string) bool

PathExists 判断文件或文件夹是否存在的方法

func ReadFile

func ReadFile(path string) ([]byte, error)

ReadFile reads a file and returns read data as byte array.

func RecvWindow

func RecvWindow(d time.Duration) int64

RecvWindow 格式化时间

func RemoveFile

func RemoveFile(file string) error

RemoveFile removes a file

func ReplaceString

func ReplaceString(input, old, new string, n int) string

ReplaceString replaces a string with another 返回将s中前n个不重叠old子串都替换为new的新字符串,如果n<0会替换所有old子串。

func RoundFloat

func RoundFloat(x float64, prec int) float64

RoundFloat rounds your floating point number to the desired decimal place

func Sha1ToHex

func Sha1ToHex(data string) string

Sha1ToHex Sign signs provided payload and returns encoded string sum.

func SplitStrings

func SplitStrings(input, separator string) []string

SplitStrings splits blocks of strings from string into a string array using a separator ie "," or "_"

func StringContains

func StringContains(input, substring string) bool

StringContains checks a substring if it contains your input then returns a bool

func StringDataCompare

func StringDataCompare(haystack []string, needle string) bool

StringDataCompare data checks the substring array with an input and returns a bool

func StringDataCompareInsensitive

func StringDataCompareInsensitive(haystack []string, needle string) bool

StringDataCompareInsensitive data checks the substring array with an input and returns a bool irrespective of lower or upper case strings

func StringDataCompareUpper

func StringDataCompareUpper(haystack []string, needle string) bool

StringDataCompareUpper data checks the substring array with an input and returns a bool irrespective of lower or upper case strings

func StringDataContains

func StringDataContains(haystack []string, needle string) bool

StringDataContains checks the substring array with an input and returns a bool

func StringDataContainsInsensitive

func StringDataContainsInsensitive(haystack []string, needle string) bool

StringDataContainsInsensitive checks the substring array with an input and returns a bool irrespective of lower or upper case strings

func StringDataContainsUpper

func StringDataContainsUpper(haystack []string, needle string) bool

StringDataContainsUpper checks the substring array with an input and returns a bool irrespective of lower or upper case strings

func StringSliceDifference

func StringSliceDifference(slice1 []string, slice2 []string) []string

StringSliceDifference concatenates slices together based on its index and returns an individual string array

func StringToLower

func StringToLower(input string) string

StringToLower changes strings to lowercase

func StringToUpper

func StringToUpper(input string) string

StringToUpper changes strings to uppercase

func TimeDataCompare

func TimeDataCompare(haystack []time.Time, needle time.Time) bool

TimeDataCompare data checks the substring array with an input and returns a bool

func TimeFromUnixEscInt64

func TimeFromUnixEscInt64(i int64) time.Time

TimeFromUnixEscInt64 format i := int64(1530854162) fmt.Println(TimeFromUnixNescInt64(i)) //2018-07-06 13:16:02

func TimeFromUnixNEscInt64

func TimeFromUnixNEscInt64(i int64) time.Time

TimeFromUnixNEscInt64 format i := int64(1532246192000) t := commonutils.TimeFromUnixEscInt64(i) fmt.Println(t.Format("2006-01-02 15:04:05")) //2018-07-22 15:56:32

func TimeFromUnixTimestampFloat

func TimeFromUnixTimestampFloat(raw interface{}) (time.Time, error)

TimeFromUnixTimestampFloat format

func TrimString

func TrimString(input, cutset string) string

TrimString trims unwanted prefixes or postfixes

func UnixNesc

func UnixNesc(t time.Time) int64

UnixNesc 格式化时间 i := int64(1532246192000) t := commonutils.TimeFromUnixNEscInt64(i) fmt.Println(t.Format("2006-01-02 15:04:05")) //2018-07-22 15:56:32 fmt.Println(commonutils.UnixNesc(t)) // 1532246192000

func UnixTimestampStrToTime

func UnixTimestampStrToTime(timeStr string) (time.Time, error)

UnixTimestampStrToTime returns a time.time and an error

func UnixTimestampToTime

func UnixTimestampToTime(timeint64 int64) time.Time

UnixTimestampToTime returns time.time

func WriteFile

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

WriteFile writes selected data to a file and returns an error

func YesOrNo

func YesOrNo(input string) bool

YesOrNo returns a boolean variable to check if input is "y" or "yes"

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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