string

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2026 License: BSD-3-Clause Imports: 0 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

go-string

Go package providing methods for creating and manipulating common string types.

The random package uses cryptographically secure randomness (crypto/rand) for generated values.

For secrets, prefer random.Token(bits) (URL-safe base64 with a known amount of entropy) or random.Bytes(n). random.String counts bytes, not characters, so the default (emoji-heavy) alphabet has less entropy than its length suggests.

The dsn package escapes spaces, = (in keys) and backslashes with \ when serializing, so values containing spaces or key=value text cannot inject extra keys. Existing plain DSNs are unaffected.

There are many string libraries. This one is mine.

Documentation

Go Reference

Example

package main

import (
	"fmt"
	"github.com/aaronland/go-string/random"
)

func main() {

	opts := random.DefaultOptions()
	opts.Length = 40
	opts.Chars = 20

	s, _ := random.String(opts)

	fmt.Println(s)
}

Error handling omitted for the sake of brevity.

Tools

randomstr
./bin/randomstr -h
Usage of ./bin/randomstr:
  -alphanumeric
    	Only include alpha-numeric characters (this causes the -ascii flag to be set to true)
  -ascii
    	Only include ASCII characters
  -base32
    	Encode as a base32 string
  -chars int
    	Length of the random string, in characters (takes precedence over -length)
  -emoji
    	Only include emoji (length is rounded down to a multiple of 4 bytes)
  -length int
    	Length of the random string, in bytes (default 32)

Most of the default alphabet is made up of multi-byte (emoji) characters so a string of 32 bytes contains far fewer characters, and far less entropy, than a 32 byte ASCII string. Use -ascii or -alphanumeric (or ASCII and AlphaNumeric in random.Options) when generating secrets. A length (or number of characters) of 0 or less, or more than 1048576, is an error.

Set Emoji in random.Options (or pass -emoji) to generate a string made up only of emoji. Every emoji is 4 bytes long so a byte length is rounded down to a multiple of 4 and must be at least 4. Emoji can not be combined with ASCII or AlphaNumeric.

Documentation

Overview

package string provides methods for creating and manipulating common string types.

Directories

Path Synopsis
cmd
randomstr command
package dsn provides methods for working with data source name (DSN) strings.
package dsn provides methods for working with data source name (DSN) strings.
package random provides methods for generating random strings.
package random provides methods for generating random strings.
package salt provides methods for generating string values that can be used as (hashing) salts.
package salt provides methods for generating string values that can be used as (hashing) salts.

Jump to

Keyboard shortcuts

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