Documentation ¶
Index ¶
- func Apr2Apy(apr any) float64
- func Apr2ApyWithCompoundTimes[T types.Number](apr any, compoundTimes T) float64
- func Apy2Apr(apy any) float64
- func Apy2AprWithCompoundTimes[T types.Number](apy any, compoundTimes T) float64
- func Celcius2Fahrenheit[T types.OrderedNumber](degree T) float64
- func ChecksumEthereumAddress(addr string) string
- func ConnectArray(strList []string, connector string) string
- func ContainAnyOfStrs(str string, subStrs []string) bool
- func ContainInArray[T comparable](target T, array []T) bool
- func ContainInArrayX(target string, array []string) bool
- func CreateKey() (privateKey string, address string)
- func CryptoRandBelow[T types.Integer](length T) int
- func CryptoRandBetween(start int, end int, times int) []int
- func Fahrenheit2Celcius[T types.OrderedNumber](degree T) float64
- func FindBytesBetween(fullBytes []byte, bytesBefore []byte, bytesAfter []byte) (target [][]byte)
- func FindStringBetween(fullStr string, strBefore string, strAfter string) (target []string)
- func FormatJson(obj any) string
- func FormatJsonByte(obj any) []byte
- func GetHashAsPriv(input string) (string, string)
- func GetMonthStart(location string) time.Time
- func GetStringHash(input string) string
- func GetTimeByStr(str string, location string) time.Time
- func GetTimeRangeByType(t string, location string) (start int64, end int64)
- func GetTodayStart(location string) time.Time
- func GetWeekStart(location string) time.Time
- func GetYearStart(location string) time.Time
- func LowerFirst(str string) string
- func MathRandBelow[T types.Integer](length T) int
- func Max[T types.OrderedNumber](numbers ...T) T
- func Min[T types.OrderedNumber](numbers ...T) T
- func PrettifyJson(raw string) string
- func PrettyJsonPrintln(obj any)
- func Private2Address(key string) string
- func ReadCsv(filePath string) ([][]string, error)
- func ReadJsonSlice(path string) ([]string, error)
- func RemoveFromArray[T comparable](array []T, toRemove T) []T
- func RemoveFromString(str string, subStr string) string
- func RemoveRepetitionInArray[T comparable](array []T) []T
- func Restart(functionToRecover func(), functionName string)
- func RestartAndSleep(functionToRecover func(), functionName string, sleepTime time.Duration)
- func RunFuncName() string
- func SendMail(sender MailSender, info MailInfo) error
- func SendTelegramSimple(botToken string, chatId int64, text string) error
- func SeperateIntoArray(str string, seperateBy string) (res []string)
- func SimpleMarshal(obj any) []byte
- func SimpleStringify(obj any) string
- func StrCamel2Underline(str string) string
- func StrUnderline2Camel(str string) string
- func StrUnderline2CamelUpperFirst(str string) string
- func StrUnderline2Seperate(str string) string
- func StrUnderline2SeperateTitle(str string) string
- func StringToTrueLocations(str string) []int64
- func Stringify(obj any) string
- func TimeFormat(timestamp int64, location string) string
- func TimeFormatTime(timestamp int64, location string) time.Time
- func TimeNowString() string
- func TimestampNow() int
- func TrueLocationsToString(trueLocs []int64) string
- func UpperFirst(str string) string
- func WriteNewCsv(filePath string, title []string, text [][]string) error
- func WriteToCsv(filePath string, title []string, text []string) error
- func WriteToCsvResume(filePath string, title []string, text []string, noRepeatTitle string) error
- type MailInfo
- type MailSender
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Apr2Apy ¶ added in v0.0.5
Calculate the apy by given apr. Compound times = 365.
Example:
Apr2Apy(0.1) = 0.10515578161622718 // 10% ==> 10.52% Apr2Apy(1) = 1.7145674820220145 // 100% ==> 171.46%
NOTE:
decimal.Decimal method `Pow()` can not use float number as the input parameter, and therefore using decimal.Decimal is meaningless here.
Example ¶
fmt.Println(Apr2Apy(0.1)) fmt.Println(Apr2Apy(1))
Output: 0.10515578161622718 1.7145674820220145
func Apr2ApyWithCompoundTimes ¶ added in v0.0.5
Calculate the apy by given apr.
Example:
Apr2ApyWithCompoundTimes(0.1, 100) = 0.10511569772075302 // 10% ==> 10.51% Apr2ApyWithCompoundTimes(1, 100) = 1.704813829421529 // 100% ==> 170.48%
func Apy2Apr ¶ added in v0.0.5
Calculate the apr by given apy. Compound times = 365.
Example:
Apy2Apr(0.1) = 0.09532262476476205 // 10% ==> 9.53% Apy2Apr(1) = 0.693805752190747 // 100% ==> 69.38%
Example ¶
fmt.Println(Apy2Apr(0.1)) fmt.Println(Apy2Apr(1))
Output: 0.09532262476476205 0.693805752190747
func Apy2AprWithCompoundTimes ¶ added in v0.0.5
Calculate the apr by given apy.
Example:
Apy2AprWithCompoundTimes(0.1, 100) = 0.09535561438964724 // 10% ==> 9.54% Apy2AprWithCompoundTimes(1, 100) = 0.6955550056718884 // 100% ==> 69.56%
func Celcius2Fahrenheit ¶ added in v0.0.9
func Celcius2Fahrenheit[T types.OrderedNumber](degree T) float64
func ChecksumEthereumAddress ¶
Return checksummed ethereum address.
Example ¶
fmt.Println(ChecksumEthereumAddress("0x5750bd75ADC81367D1b7510A57D79ACF777950a1")) // checksumed fmt.Println(ChecksumEthereumAddress("0x5750bd75adc81367d1b7510a57d79acf777950a1")) // all lower case fmt.Println(ChecksumEthereumAddress("0X5750BD75ADC81367D1B7510A57D79ACF777950A1")) // all upper case
Output: 0x5750bd75ADC81367D1b7510A57D79ACF777950a1 0x5750bd75ADC81367D1b7510A57D79ACF777950a1 0x5750bd75ADC81367D1b7510A57D79ACF777950a1
func ConnectArray ¶
Connect the items in an array with "connector".
Example:
ConnectArray([]string{"one", "year", "old"}, "-") = "one-year-old"
func ContainAnyOfStrs ¶
Example:
ContainAnyOfStrs("abracadabra", []string{"bra", "tnt"}) = true ContainAnyOfStrs("abracadabra", []string{"abc", "tnt"}) = false
func ContainInArray ¶
func ContainInArray[T comparable](target T, array []T) bool
If target contains in array.
Example:
ContainInArray(1,[]int{1, 2, 3}) = true ContainInArray("", []string{"1", "2", "3"}) = false ContainInArray("0", []string{"1", "2", "3"}) = false
func ContainInArrayX ¶ added in v0.0.6
If target contains in string array, ignore lower or upper.
Example:
ContainInArrayX("aa", []string{"aa", "bb", "cc"}) = true ContainInArrayX("aA", []string{"aa", "bb", "cc"}) = true
func CryptoRandBelow ¶
Real random/ Crypto random.
Range:
0 ~ length-1
NOTE:
You will receive exactly the same series of results if you call this function several times in a period of time.
func CryptoRandBetween ¶
Choose "times" different numbers between "start" and "end".
Example:
CryptoRandBetween(1, 100, 5) = []int{1, 68, 69, 78, 79, 98, 100}
NOTE:
1st. You will not get two same points in the array. 2nd. You will receive exactly the same series of results if you call this function several times in a short period of time.
func Fahrenheit2Celcius ¶ added in v0.0.9
func Fahrenheit2Celcius[T types.OrderedNumber](degree T) float64
func FindBytesBetween ¶
Find all []byte between "bytesBefore" and "bytesAfter".
Used to seperate a long []byte into ideal array.
func FindStringBetween ¶
Find all string between "strBefore" and "strAfter".
Used to seperate a long string into ideal array.
Example:
FindStringBetween("aaaaaa", "a", "a") = []string{"", "", ""} FindStringBetween("abcabc", "a", "c") = []string{"b", "b"}
func FormatJson ¶
func FormatJsonByte ¶
func GetHashAsPriv ¶ added in v0.0.9
Get hash and use it as private key, and generate an eth address.
Return:
1st. string: The hash. If "0x" deleted, it is the private key. 2nd. string: The ethereum address generated by the hash.
Example:
GetHashAsPriv("8") = "0xe4b1702d9298fee62dfeccc57d322a463ad55ca201256d01f62b45b2e1c21c10", "0xe0FC04FA2d34a66B779fd5CEe748268032a146c0"
Example ¶
priv, addr := GetHashAsPriv("100") fmt.Println("priv:", priv) fmt.Println("addr:", addr)
Output: priv: 0x8c18210df0d9514f2d2e5d8ca7c100978219ee80d3968ad850ab5ead208287b3 addr: 0xef3CDbFf03CE88F5041C82131b0cF15061b6015E
func GetMonthStart ¶
If location is "" or "UTC", use UTC time.
If location is "Local", use local time.
Location can also be "America/New_York", "Asia/Shanghai", etc.
func GetStringHash ¶
Get the hash of a string.
Example:
GetStringHash("TraderJoeSupplyEvent(address,address,uint256,address)") = "0x222da511b15d564e69f7e3832eaefbe7fb9875c94fbeecdc358917f0fd0a4f9f"
func GetTimeByStr ¶
If location is "" or "UTC", use UTC time.
If location is "Local", use local time.
Location can also be "America/New_York", "Asia/Shanghai", etc.
func GetTimeRangeByType ¶
If location is "" or "UTC", use UTC time.
If location is "Local", use local time.
Location can also be "America/New_York", "Asia/Shanghai", etc.
func GetTodayStart ¶
If location is "" or "UTC", use UTC time.
If location is "Local", use local time.
Location can also be "America/New_York", "Asia/Shanghai", etc.
func GetWeekStart ¶
If location is "" or "UTC", use UTC time.
If location is "Local", use local time.
Location can also be "America/New_York", "Asia/Shanghai", etc.
func GetYearStart ¶
If location is "" or "UTC", use UTC time.
If location is "Local", use local time.
Location can also be "America/New_York", "Asia/Shanghai", etc.
func LowerFirst ¶ added in v0.0.9
The first letter to lower case.
Example:
LowerFirst("Lower") = "lower"
func MathRandBelow ¶
Random from timestamp.
Range:
0 ~ length-1
NOTE:
You will receive exactly the same one result if you call this function in one second.
func Max ¶ added in v0.0.4
func Max[T types.OrderedNumber](numbers ...T) T
Get the maximum one of the input numbers.
Example:
Max(1, 2, 3) = 3 Max[int]() = 0
NOTE:
Can not use array as input param.
Example ¶
fmt.Println(Max[int]()) fmt.Println(Max[float64](1, 2)) fmt.Println(Max(1, 2, 3, 4, 5))
Output: 0 2 5
func Min ¶ added in v0.0.4
func Min[T types.OrderedNumber](numbers ...T) T
Get the minimum one of the input numbers.
Example:
Min(1, 2, 3) = 1 Min[int]() = 0
NOTE:
Can not use array as input param.
Example ¶
fmt.Println(Min[int]()) fmt.Println(Min[float64](1, 2)) fmt.Println(Min(1, 2, 3, 4, 5))
Output: 0 1 1
func PrettifyJson ¶
func PrettyJsonPrintln ¶
func PrettyJsonPrintln(obj any)
func Private2Address ¶ added in v0.0.9
From private key to address.
Example ¶
fmt.Println(Private2Address("3fe74272f214ddb0d182a9ae68e3ee844d6f61d04f6e7827aad972e071a31f81"))
Output: 0x5750bd75ADC81367D1b7510A57D79ACF777950a1
func ReadJsonSlice ¶
func RemoveFromArray ¶
func RemoveFromArray[T comparable](array []T, toRemove T) []T
Remove sth from an array and return the new one.
Example:
RemoveFromArray([]string{"aa", "bb"}, "aa") = []string{"bb"} RemoveFromArray([]uint64{1}, 1) = []uint64{}
func RemoveFromString ¶
Remove the first subStr in str.
func RemoveRepetitionInArray ¶
func RemoveRepetitionInArray[T comparable](array []T) []T
Remove repetiton in an array and return the new one.
Example:
RemoveRepetitionInArray([]string{"aa", "bb", "bb", "BB"}) = []string{"aa", "bb", "BB"} RemoveRepetitionInArray([]int{1, 2, 3, 3}) = []int{1, 2, 3}
func Restart ¶
func Restart(functionToRecover func(), functionName string)
Recover from panic and restart.
func RestartAndSleep ¶
Recover from panic and restart after waiting for a while.
func RunFuncName ¶ added in v0.0.8
func RunFuncName() string
Get the function name running now.
Example ¶
fmt.Println(RunFuncName())
Output: github.com/0xVanfer/utils.ExampleRunFuncName
func SendMail ¶ added in v0.0.7
func SendMail(sender MailSender, info MailInfo) error
func SendTelegramSimple ¶ added in v0.0.7
Send simple telegram message.
Param:
botToken: Telegram bot token, you can search for `BotFather` in telegram and generate your own bot. chatId: If you are sending a message to a user, chatId is his id, a possitive integer, like 12345678, which the user can get by starting `userinfobot`. If you are sending a message to a group, chatId is a negative integer.
NOTE:
Can only send simple text. Not support `\` like `\n`.
func SeperateIntoArray ¶
Seperate a string into an array, the seperater will be deleted.
Example:
SeperateIntoArray("one-year-old", "-") = []string{"one", "year", "old"}
func SimpleMarshal ¶
func SimpleStringify ¶
func StrCamel2Underline ¶ added in v0.0.9
Return underline case of str.
Example:
StrUnderline2Camel("aBC") = "a_b_c" StrUnderline2Camel("ABC") = "a_b_c"
NOTE:
Using both `StrUnderline2Camel()` and `StrCamel2Underline()` (regardless of order) will probably not get the original string!
Example ¶
fmt.Println(StrCamel2Underline("aaBBC")) fmt.Println(StrCamel2Underline("A")) fmt.Println(StrCamel2Underline("bASccccSS"))
Output: aa_b_b_c a b_a_scccc_s_s
func StrUnderline2Camel ¶ added in v0.0.9
Return camel case of str. The first letter is lower case.
Example:
StrUnderline2Camel("a_b_c") = "aBC"
NOTE:
Using both `StrUnderline2Camel()` and `StrCamel2Underline()` (regardless of order) will probably not get the original string!
Example ¶
fmt.Println(StrUnderline2Camel("")) fmt.Println(StrUnderline2Camel("a")) fmt.Println(StrUnderline2Camel("a_b")) fmt.Println(StrUnderline2Camel("a_b_ccc_ddd")) // Output // // a // aB // aBCccDdd
Output:
func StrUnderline2CamelUpperFirst ¶ added in v0.0.9
Return camel case of str. The first letter is upper case.
Example:
StrUnderline2Camel("a_b_c") = "ABC"
NOTE:
Using both `StrUnderline2Camel()` and `StrCamel2Underline()` (regardless of order) will probably not get the original string!
func StrUnderline2Seperate ¶ added in v0.0.9
Return seperated words of str.
Example:
StrUnderline2Seperate("a_b_c") = "a b c"
Example ¶
fmt.Println(StrUnderline2Seperate("")) fmt.Println(StrUnderline2Seperate("a")) fmt.Println(StrUnderline2Seperate("a_b")) fmt.Println(StrUnderline2Seperate("a_b_ccc_ddd"))
Output: a a b a b ccc ddd
func StrUnderline2SeperateTitle ¶ added in v0.0.9
Return title case seperated words of str.
Example:
StrUnderline2SeperateTitle("a_b_c") = "A B C"
Example ¶
fmt.Println(StrUnderline2SeperateTitle("")) fmt.Println(StrUnderline2SeperateTitle("a")) fmt.Println(StrUnderline2SeperateTitle("a_b")) fmt.Println(StrUnderline2SeperateTitle("a_b_ccc_ddd")) // Output // // A // A B // A B Ccc Ddd
Output:
func StringToTrueLocations ¶
Consider a map: map[loc]=bool.
Convert the coded string into locations.
Example:
StringToTrueLocations("1432351") = []int64{0, 1, 2, 3, 4, 8, 9, 11, 12, 14, 15, 16, 18, 20}
Example ¶
fmt.Println(StringToTrueLocations("1432351"))
Output: [0 1 2 3 4 8 9 11 12 14 15 16 18 20]
func TimeFormat ¶
If location is "" or "UTC", use UTC time.
If location is "Local", use local time.
Location can also be "America/New_York", "Asia/Shanghai", etc.
func TimeFormatTime ¶
If location is "" or "UTC", use UTC time.
If location is "Local", use local time.
Location can also be "America/New_York", "Asia/Shanghai", etc.
func TimeNowString ¶
func TimeNowString() string
func TimestampNow ¶
func TimestampNow() int
func TrueLocationsToString ¶
Consider a map: map[loc]=bool.
Use a bigInt to locate all true locations and convert into string.
Example:
TrueLocationsToString([]int64{1, 2, 333, 999}) = "17498005798264095394980017816940970922825355447145699491406164851279623993595007385788105416184430598"
Example ¶
fmt.Println(TrueLocationsToString([]int64{1, 2, 33}))
Output: 8589934598
func UpperFirst ¶ added in v0.0.9
The first letter to upper case.
Example:
UpperFirst("upper") = "Upper"
func WriteNewCsv ¶
If file exist, will cover the old file.
func WriteToCsv ¶
Write to csv line by line.
Types ¶
type MailSender ¶ added in v0.0.7
type MailSender struct { Address string `gorm:"column:address" json:"address"` // Sender email address. Nickname string `gorm:"column:nickname" json:"nickname"` // Define sender nickname. Password string `gorm:"column:password" json:"password"` // Mail password or token. Host string `gorm:"column:host" json:"host"` // smtp.xxx.com Port string `gorm:"column:port" json:"port"` // 465 }
Sender info.