34 lines
698 B
Go
34 lines
698 B
Go
|
package main
|
||
|
|
||
|
import "fmt"
|
||
|
|
||
|
// Dictionary is a simple map of names to words
|
||
|
type Dictionary map[string]Word
|
||
|
|
||
|
// A Word defines a subroutine
|
||
|
type Word struct {
|
||
|
Name string
|
||
|
Impl func() error
|
||
|
Source string
|
||
|
}
|
||
|
|
||
|
// AddWord inserts a new word into the dictonary, optionally overwriting the existing word
|
||
|
func (d Dictionary) AddWord(name string, impl func() error, source string) {
|
||
|
d[name] = Word{
|
||
|
Name: name,
|
||
|
Impl: impl,
|
||
|
Source: source,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// GetWord returns a word from the dictionary or an error if it's undefined
|
||
|
func (d Dictionary) GetWord(name string) (Word, error) {
|
||
|
w, ok := d[name]
|
||
|
if !ok {
|
||
|
return Word{
|
||
|
Name: name,
|
||
|
}, fmt.Errorf("no word found")
|
||
|
}
|
||
|
return w, nil
|
||
|
}
|