12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- package strutil
- import (
- "crypto/rand"
- "math/big"
- "unicode"
- )
- func ToUpperFirst(s string) string {
- for i, v := range s {
- return string(unicode.ToUpper(v)) + s[i+1:]
- }
- return ""
- }
- func RandomChars(n int) (string, error) {
- const alphanum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
- randomInt := func(max *big.Int) (int, error) {
- r, err := rand.Int(rand.Reader, max)
- if err != nil {
- return 0, err
- }
- return int(r.Int64()), nil
- }
- buffer := make([]byte, n)
- max := big.NewInt(int64(len(alphanum)))
- for i := 0; i < n; i++ {
- index, err := randomInt(max)
- if err != nil {
- return "", err
- }
- buffer[i] = alphanum[index]
- }
- return string(buffer), nil
- }
|