// Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. package str import ( "math/rand" "strings" "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's keys as array. func MapsKeys(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's keys and join it to string. func MapsKeysString(imap interface{}) string { keys := MapsKeys(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) //// } //// }