maps

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2022 License: MIT Imports: 0 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const (
	ErrNotFound         = DictionaryErr("could not find the word you were looking for")
	ErrWordExists       = DictionaryErr("cannot add word because it already exists")
	ErrWordDoesNotExist = DictionaryErr("cannot update the word because it does not exist")
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Dictionary

type Dictionary map[string]string
Example
dictionary := Dictionary{"test": "test definition"}

fmt.Print(dictionary)
Output:
map[test:test definition]

func (Dictionary) Add

func (d Dictionary) Add(word, definition string) error

Add word if it does not exist. Return error if the word already exists.

Example
dictionary := Dictionary{}
err := dictionary.Add("test", "test definition")

if err != nil {
	fmt.Print("word already exists")
}

definition, _ := dictionary.Search("test")

fmt.Print(definition)
Output:
test definition

func (Dictionary) Delete

func (d Dictionary) Delete(word string)

Delete the word from the dictionary

Example
dictionary := Dictionary{"test": "test definition"}
dictionary.Delete("test")

// Throw an error if the word does not exist
definition, _ := dictionary.Search("test")

fmt.Print(definition)

func (Dictionary) Search

func (d Dictionary) Search(word string) (string, error)

Search definition by the given word. Throw an error if the word does not exist.

Example
dictionary := Dictionary{"test": "test definition"}
definition, err := dictionary.Search("test")

if err != nil {
	fmt.Print("word not found")
}

fmt.Print(definition)
Output:
test definition

func (Dictionary) Update

func (d Dictionary) Update(word, definition string) error

Update definition of the given word to new given definition. Return error if the word does not exist.

Example
dictionary := Dictionary{"test": "test definition"}

err := dictionary.Update("test", "new definition")

if err != nil {
	fmt.Print("word does not exist")
}

definition, _ := dictionary.Search("test")
fmt.Print(definition)
Output:
new definition

type DictionaryErr

type DictionaryErr string

func (DictionaryErr) Error

func (e DictionaryErr) Error() string

Jump to

Keyboard shortcuts

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