stash/pkg/utils/strings.go

35 lines
635 B
Go
Raw Normal View History

package utils
import (
"fmt"
"math/rand"
"strings"
"time"
)
var characters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890")
func RandomSequence(n int) string {
b := make([]rune, n)
rand.Seed(time.Now().UnixNano())
for i := range b {
b[i] = characters[rand.Intn(len(characters))]
}
return string(b)
}
type StrFormatMap map[string]interface{}
func StrFormat(format string, m StrFormatMap) string {
args := make([]string, len(m)*2)
i := 0
for k, v := range m {
args[i] = fmt.Sprintf("{%s}", k)
args[i+1] = fmt.Sprint(v)
i += 2
}
return strings.NewReplacer(args...).Replace(format)
}