Created
September 18, 2017 22:20
-
-
Save ooola/39800bb9c017682b38310c07fee99011 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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