RandomString
Overview
RandomString returns a URL-safe base64 string with roughly n bytes of entropy (uses n random bytes, encoded without padding).
Part of the crypto package — Node.js analog: crypto, bcrypt.
Signature
go
func RandomString(n int) (string, error)Compare: Node.js · Standard Go · gox
js
// Typical crypto, bcrypt pattern in Node.jsgo
b := make([]byte, n)
_, err := rand.Read(b)go
import "github.com/sahilkhaire/gox/crypto"
token, err := crypto.RandomString(32)Example
go
import "github.com/sahilkhaire/gox/crypto"
token, err := crypto.RandomString(32)Tips
Import github.com/sahilkhaire/gox/crypto and call RandomString directly. See the comparison below for the standard library equivalent.
Standard library alternative
Use the standard library directly:
go
b := make([]byte, n)
_, err := rand.Read(b)