rword

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2024 License: BSD-3-Clause Imports: 6 Imported by: 1

README

rword

rword is a golang random word generator.

Can generate words using a saved dict containing over 370_000 words.
Or you can use a custom dictionary.
For more information, see the section "How to use it" below.

Installation

You must use golang version 1.21 or higher. 0 dependencies, except for goland stdlib

How to use it

package main

import (
	"fmt"

	"github.com/kpechenenko/rword"
)

func main() {
	var g rword.GenerateRandom
	var err error
	// Create a random word generator using saved dict with 370_000+ words.
	g, err = rword.New()
	if err != nil {
		panic(err)
	}
	// Get a random word.
	word := g.Word()
	fmt.Println(word)
	// Get a list of 10 random words.
	words := g.WordList(10)
	for _, w := range words {
		fmt.Println(w)
	}
	// Get random string with a length of 10.
	s := g.Str(10)
	fmt.Println(s)
	
	// Create a random word generator using your dictionary. Dictionary is a text file, 1 line - 1 word.
	// Returns an error, if dictionary is empty.
	g2, err := rword.NewWithDict("path/to/your/dict")
	if err != nil {
		panic(err)
	}
	// Get a random word.
	word = g2.Word()
	fmt.Println(word)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrEmptyDict = errors.New("empty dictionary")

Functions

func GetPathToDefaultDict

func GetPathToDefaultDict() string

GetPathToDefaultDict with 370_000+ words.

Types

type Dictionary

type Dictionary []string

Dictionary to select random words.

func LoadDictFromFile

func LoadDictFromFile(name string) (Dictionary, error)

LoadDictFromFile load dictionary from file. 1 line - 1 word, skip empty lines.

type GenerateRandom

type GenerateRandom interface {
	// Word random word.
	Word() string
	// WordList of random words.
	WordList(n int) []string
	// Str get random string of n length.
	Str(n int) string
}

GenerateRandom a random word/string generator.

type Generator

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

Generator generates random words using a saved dictionary.

func New

func New() (*Generator, error)

New create a word generator with saved dictionary.

func NewWithDict

func NewWithDict(file string) (*Generator, error)

NewWithDict create a word generator using custom dictionary. dictionary - text file, 1 line = 1 word in dictionary.

func (*Generator) Str

func (g *Generator) Str(n int) string

Str get random string of n length.

func (*Generator) Word

func (g *Generator) Word() string

Word get a random word.

func (*Generator) WordList

func (g *Generator) WordList(n int) []string

WordList get a list of random words.

Jump to

Keyboard shortcuts

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