Skip to content

Commit f84ed5a

Browse files
committed
feat: maps
1 parent afc2424 commit f84ed5a

File tree

3 files changed

+154
-0
lines changed

3 files changed

+154
-0
lines changed

languages/go.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,18 @@ Deleting from a map:
257257
delete()
258258
```
259259
260+
Never initialize a nil map - writing to a nil map will cause runtime panic. This is because the variable `m` is initialized to `nil`:
261+
262+
```go
263+
var m map[string]string
264+
```
265+
266+
Instead initialize an empty map:
267+
268+
```go
269+
m := make(map[string]string)
270+
```
271+
260272
## Iteration
261273
262274
There are only `for` loops in Go (no `while`).

learn-go-with-tests/7-maps/dict.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package dict
2+
3+
const (
4+
ErrNotFound = DictionaryError("could not find the word you were looking for")
5+
ErrWordExists = DictionaryError("word already exists")
6+
ErrWordDoesNotExist = DictionaryError("word doesn't exist")
7+
)
8+
9+
type DictionaryError string
10+
11+
func (e DictionaryError) Error() string {
12+
return string(e)
13+
}
14+
15+
type Dictionary map[string]string
16+
17+
func (d Dictionary) Search(word string) (string, error) {
18+
definition, ok := d[word]
19+
if !ok {
20+
return "", ErrNotFound
21+
}
22+
return definition, nil
23+
}
24+
25+
func (d Dictionary) Add(word, definition string) error {
26+
_, err := d.Search(word)
27+
28+
switch err {
29+
case ErrNotFound:
30+
d[word] = definition
31+
case nil:
32+
return ErrWordExists
33+
default:
34+
return err
35+
}
36+
return nil
37+
}
38+
39+
func (d Dictionary) Update(word, definition string) error {
40+
_, err := d.Search(word)
41+
switch err {
42+
case ErrNotFound:
43+
return ErrWordDoesNotExist
44+
case nil:
45+
d[word] = definition
46+
default:
47+
return err
48+
}
49+
return nil
50+
}
51+
52+
func (d Dictionary) Delete(word string) {
53+
delete(d, word)
54+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package dict
2+
3+
import "testing"
4+
5+
func assertString(t *testing.T, got, want string) {
6+
if got != want {
7+
t.Errorf("got %q want %q", got, want)
8+
}
9+
}
10+
11+
func assertError(t *testing.T, got, want error) {
12+
if got != want {
13+
t.Errorf("got %q want %q", got, want)
14+
}
15+
}
16+
17+
func TestSearch(t *testing.T) {
18+
19+
dict := Dictionary{"test": "a test"}
20+
t.Run("known word", func(t *testing.T) {
21+
got, _ := dict.Search("test")
22+
want := "a test"
23+
assertString(t, got, want)
24+
})
25+
26+
t.Run("unknown word", func(t *testing.T) {
27+
_, err := dict.Search("unknown")
28+
if err == nil {
29+
t.Fatal("expected to get an error")
30+
}
31+
assertError(t, err, ErrNotFound)
32+
})
33+
}
34+
35+
func TestAdd(t *testing.T) {
36+
t.Run("new word", func(t *testing.T) {
37+
dict := Dictionary{}
38+
dict.Add("neu", "word")
39+
want := "word"
40+
got, err := dict.Search("neu")
41+
if err != nil {
42+
t.Fatal("should find added word: ", err)
43+
}
44+
assertString(t, want, got)
45+
})
46+
47+
t.Run("existing word", func(t *testing.T) {
48+
dict := Dictionary{}
49+
dict.Add("neu", "word")
50+
got, _ := dict.Search("neu")
51+
err := dict.Add("neu", "word again")
52+
53+
assertString(t, got, "word")
54+
assertError(t, err, ErrWordExists)
55+
})
56+
}
57+
58+
func TestUpdate(t *testing.T) {
59+
t.Run("existing word", func(t *testing.T) {
60+
word := "test"
61+
dict := Dictionary{}
62+
dict.Add(word, "definition")
63+
dict.Update(word, "new definition")
64+
got, _ := dict.Search(word)
65+
assertString(t, got, "new definition")
66+
})
67+
68+
t.Run("new word", func(t *testing.T) {
69+
word := "test"
70+
dict := Dictionary{}
71+
err := dict.Update(word, "new definition")
72+
assertError(t, err, ErrWordDoesNotExist)
73+
})
74+
}
75+
76+
func TestDelete(t *testing.T) {
77+
t.Run("existing word", func(t *testing.T) {
78+
word := "test"
79+
dict := Dictionary{}
80+
dict.Add(word, "definition")
81+
dict.Delete(word)
82+
_, err := dict.Search(word)
83+
if err == nil {
84+
t.Fatal("expected to get an error")
85+
}
86+
assertError(t, err, ErrNotFound)
87+
})
88+
}

0 commit comments

Comments
 (0)