Skip to content

Instantly share code, notes, and snippets.

@ooola
Created September 18, 2017 22:20
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ooola/39800bb9c017682b38310c07fee99011 to your computer and use it in GitHub Desktop.
Save ooola/39800bb9c017682b38310c07fee99011 to your computer and use it in GitHub Desktop.
package main
import (
"math/rand"
"time"
"fmt"
)
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
var seededRand *rand.Rand = rand.New(
rand.NewSource(time.Now().UnixNano()))
func StringWithCharset(length int, charset string) string {
b := make([]byte, length)
for i := range b {
b[i] = charset[seededRand.Intn(len(charset))]
}
return string(b)
}
func String(length int) string {
return StringWithCharset(length, charset)
}
func main() {
fmt.Println(String(10))
}
// For higher performance see
// https://stackoverflow.com/questions/22892120/how-to-generate-a-random-string-of-a-fixed-length-in-golang
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment