init version

This commit is contained in:
admin 2022-10-12 20:52:27 +08:00
commit 099a33cccf
3 changed files with 117 additions and 0 deletions

3
README.md Normal file
View File

@ -0,0 +1,3 @@
# personal str functions

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module qoobing.com/gomod/str
go 1.16

111
str.go Normal file
View File

@ -0,0 +1,111 @@
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
package str
import (
"container/list"
"encoding/json"
"errors"
"fmt"
"math/rand"
"os"
"regexp"
"runtime"
"strings"
"syscall"
"time"
)
const (
NUMBERSTRING = "0123456789"
CHARSTRING = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
)
// get random number string with length.
func GetRandomString(l int) string {
str := NUMBERSTRING
bytes := []byte(str)
result := []byte{}
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < l; i++ {
result = append(result, bytes[r.Intn(len(bytes))])
}
return string(result)
}
// get random number+alphabate string with length.
func GetRandomCharString(l int) string {
str := CHARSTRING
bytes := []byte(str)
result := []byte{}
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < l; i++ {
result = append(result, bytes[r.Intn(len(bytes))])
}
return string(result)
}
//skip
func Skip(s string, sep string, n int) string {
seplen := len(sep)
if seplen == 0 || n < 0 {
return s
}
for {
i := strings.Index(s, sep)
if i < 0 {
return s
} else {
s = s[i+seplen:]
n = n - 1
if n <= 0 {
return s
}
}
}
return s
}
// skip line
func SkipLine(s string, n int) string {
return Skip(s, "\n", n)
}
// get a map keys as array.
func MapKeys(imap interface{}) (keys []string) {
switch imap.(type) {
case map[string]string:
m := imap.(map[string]string)
for k, _ := range m {
keys = append(keys, k)
}
case map[string]interface{}:
m := imap.(map[string]interface{})
for k, _ := range m {
keys = append(keys, k)
}
default:
panic("Unkown Type")
}
return keys
}
// get a map keys and join to string.
func KeysString(imap interface{}) string {
keys := Keys(imap)
return strings.Join(keys, ",")
}
//// CHARSTRING string = "" //will initialize by init function
//// func init() {
//// NUMBERSTRING = "0123456789"
//// CHARSTRING = NUMBERSTRING
//// for i := 'a'; i <= 'z'; i++ {
//// CHARSTRING = CHARSTRING + string(i)
//// }
//// for i := 'A'; i <= 'Z'; i++ {
//// CHARSTRING = CHARSTRING + string(i)
//// }
//// }