From d0f5a8fa0d35a21329d7621cf87db3d14922eb10 Mon Sep 17 00:00:00 2001 From: Thorsten Ball Date: Wed, 6 Aug 2014 15:27:43 +0200 Subject: [PATCH] Cache modulo operator in New Instead of recreating the operator in each iterator, we can cache it before the loop. --- pwgen.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pwgen.go b/pwgen.go index e127a07..ddb8b10 100644 --- a/pwgen.go +++ b/pwgen.go @@ -10,9 +10,11 @@ const chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_-? // New() generates a random string of the given length func New(length int) string { var bytes = make([]byte, length) + var op = byte(len(chars)) + rand.Read(bytes) for i, b := range bytes { - bytes[i] = chars[b%byte(len(chars))] + bytes[i] = chars[b%op] } return string(bytes) }