-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add new method to generate random string
- Loading branch information
1 parent
e9fae58
commit 2b0b848
Showing
2 changed files
with
33 additions
and
1 deletion.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package generate | ||
|
||
import ( | ||
"math/rand" | ||
"time" | ||
) | ||
|
||
const ( | ||
charSet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" | ||
) | ||
|
||
var seedRand *rand.Rand = rand.New(rand.NewSource(time.Now().UnixNano())) | ||
|
||
func stringWithCharset(length int, charset string) string { | ||
var b []byte | ||
for i := 0; i < length; i++ { | ||
b = append( | ||
b, | ||
charset[seedRand.Intn(len(charset))], | ||
) | ||
} | ||
return string(b) | ||
} | ||
|
||
// RandString generates a random string of a specific length | ||
func RandString(length int) string { | ||
return stringWithCharset(length, charSet) | ||
} |
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