random

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2023 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package random implements some basic functions to generate random int and string.

Index

Examples

Constants

View Source
const (
	NUMERAL       = "0123456789"
	LOWER_LETTERS = "abcdefghijklmnopqrstuvwxyz"
	UPPER_LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	LETTERS       = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
)

Variables

This section is empty.

Functions

func RandBytes

func RandBytes(length int) []byte

RandBytes generate random byte slice. Play: https://go.dev/play/p/EkiLESeXf8d

Example
bytes := RandBytes(4)

fmt.Println(len(bytes))
Output:

4

func RandInt

func RandInt(min, max int) int

RandInt generate random int between min and max, maybe min, not be max. Play: https://go.dev/play/p/pXyyAAI5YxD

Example
result := RandInt(1, 10)

if result >= 1 && result < 10 {
	fmt.Println("ok")
}
Output:

ok

func RandLower

func RandLower(length int) string

RandLower generate a random lower case string. Play: https://go.dev/play/p/XJtZ471cmtI

Example
pattern := `^[a-z]+$`
reg := regexp.MustCompile(pattern)

s := RandLower(6)

result1 := reg.MatchString(s)
result2 := len(s)

fmt.Println(result1)
fmt.Println(result2)
Output:

true
6

func RandNumeral

func RandNumeral(length int) string

RandNumeral generate a random numeral string of specified length. Play: https://go.dev/play/p/g4JWVpHsJcf

Example
pattern := `^[0-9]+$`
reg := regexp.MustCompile(pattern)

s := RandNumeral(6)

result1 := reg.MatchString(s)
result2 := len(s)

fmt.Println(result1)
fmt.Println(result2)
Output:

true
6

func RandNumeralOrLetter

func RandNumeralOrLetter(length int) string

RandNumeralOrLetter generate a random numeral or letter string. Play: https://go.dev/play/p/19CEQvpx2jD

Example
pattern := `^[0-9a-zA-Z]+$`
reg := regexp.MustCompile(pattern)

s := RandNumeralOrLetter(6)

result1 := reg.MatchString(s)
result2 := len(s)

fmt.Println(result1)
fmt.Println(result2)
Output:

true
6

func RandString

func RandString(length int) string

RandString generate random string of specified length. Play: https://go.dev/play/p/W2xvRUXA7Mi

Example
pattern := `^[a-zA-Z]+$`
reg := regexp.MustCompile(pattern)

s := RandString(6)

result1 := reg.MatchString(s)
result2 := len(s)

fmt.Println(result1)
fmt.Println(result2)
Output:

true
6

func RandUpper

func RandUpper(length int) string

RandUpper generate a random upper case string. Play: https://go.dev/play/p/29QfOh0DVuh

Example
pattern := `^[A-Z]+$`
reg := regexp.MustCompile(pattern)

s := RandUpper(6)

result1 := reg.MatchString(s)
result2 := len(s)

fmt.Println(result1)
fmt.Println(result2)
Output:

true
6

func UUIdV4

func UUIdV4() (string, error)

UUIdV4 generate a random UUID of version 4 according to RFC 4122. Play: https://go.dev/play/p/_Z9SFmr28ft

Example
pattern := `^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$`
reg := regexp.MustCompile(pattern)

s, _ := UUIdV4()

result := reg.MatchString(s)

fmt.Println(result)
Output:

true

Types

This section is empty.

Jump to

Keyboard shortcuts

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