randstr

package module
v0.0.0-...-8a34c89 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 26, 2021 License: Apache-2.0 Imports: 4 Imported by: 0

README

Go Reference

randstr

generate random string very faster.

The code is based on this awesome StackOverflow answer by icza.

How to use

install

go get github.com/KennyChenFight/randstr

Example
package main

import (
	"fmt"
	"github.com/KennyChenFight/randstr"
)

func main() {
	const randomStrLength = 6
	generator := randstr.NewFastGenerator(randstr.CharSetEnglishAlphabet)
	fmt.Println(generator.GenerateRandomStr(randomStrLength))
}

You can reference example folder.

Benchmark

goos: windows
goarch: amd64
pkg: github.com/KennyChenFight/randstr
cpu: Intel(R) Core(TM) i7-9700 CPU @ 3.00GHz
BenchmarkFastGenerator_GenerateRandomStr_EnglishAlphabet
BenchmarkFastGenerator_GenerateRandomStr_EnglishAlphabet-8              27867314                42.19 ns/op            1 allocs/op
BenchmarkFastGenerator_GenerateRandomStr_EnglishAlphabetLowercase
BenchmarkFastGenerator_GenerateRandomStr_EnglishAlphabetLowercase-8     29626336                41.15 ns/op            1 allocs/op
BenchmarkFastGenerator_GenerateRandomStr_EnglishAlphabetUppercase
BenchmarkFastGenerator_GenerateRandomStr_EnglishAlphabetUppercase-8     29051328                42.44 ns/op            1 allocs/op
BenchmarkFastGenerator_GenerateRandomStr_EnglishAlphabetBase62
BenchmarkFastGenerator_GenerateRandomStr_EnglishAlphabetBase62-8        41708664                29.78 ns/op            1 allocs/op
BenchmarkFastGenerator_GenerateRandomStr_EnglishAlphabetBase64
BenchmarkFastGenerator_GenerateRandomStr_EnglishAlphabetBase64-8        14275873                86.11 ns/op            1 allocs/op

Original generate random string benchmark

You can reference original folder.

Documentation

Overview

Package randstr provides generating random string As a quick start for generator:

	const randomStrLength = 6
	generator := randstr.NewFastGenerator(randstr.CharSetEnglishAlphabet)
 generator.GenerateRandomStr(randomStrLength)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CharSetType

type CharSetType string
const (
	CharSetEnglishAlphabet          CharSetType = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
	CharSetEnglishAlphabetLowercase CharSetType = "abcdefghijklmnopqrstuvwxyz"
	CharSetEnglishAlphabetUppercase CharSetType = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	CharSetBase62                   CharSetType = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
	CharSetBase64                   CharSetType = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/"
)

type FastGenerator

type FastGenerator struct {
	// contains filtered or unexported fields
}

func NewFastGenerator

func NewFastGenerator(setType CharSetType) *FastGenerator

func (*FastGenerator) GenerateRandomStr

func (g *FastGenerator) GenerateRandomStr(n int) string

type RandomStrGenerator

type RandomStrGenerator interface {
	GenerateRandomStr(n int) string
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL