diff options
| author | Felix Hanley <felix@userspace.com.au> | 2016-12-05 08:16:58 +0000 |
|---|---|---|
| committer | Felix Hanley <felix@userspace.com.au> | 2016-12-05 08:16:58 +0000 |
| commit | b049991a46a2f619344bd6e915745703864d0134 (patch) | |
| tree | ec1d3897a7b69c7c63a3774d4c42dfbb8cb46432 /dict.go | |
| parent | e1c3d6f7db06d592538f1162b2b6b9f1b6efa330 (diff) | |
| download | go-dict2rest-master.tar.gz go-dict2rest-master.tar.bz2 | |
Diffstat (limited to 'dict.go')
| -rw-r--r-- | dict.go | 47 |
1 files changed, 47 insertions, 0 deletions
@@ -0,0 +1,47 @@ +package main + +import ( + "golang.org/x/net/dict" + "log" +) + +type definition struct { + Dictionary string `json:"dictionary"` + Word string `json:"word"` + Definition string `json:"definition"` +} +type dictionary struct { + Name string `json:"name"` + Desc string `json:"description"` +} + +func getDictClient() (*dict.Client, error) { + client, err := dict.Dial("tcp", dictServer) + if err != nil { + log.Printf("Unable to connect to dict server at %s", dictServer) + return nil, err + } + log.Println("Connected to", dictServer) + return client, nil +} + +func getDictionaries(*dict.Client) ([]dictionary, error) { + client, err := getDictClient() + if err != nil { + log.Printf("Unable to connect to dict server at %s", dictServer) + return nil, err + } + + defer client.Close() + + dictArr, err := client.Dicts() + if err != nil { + return nil, err + } + + var dicts []dictionary + for _, d := range dictArr { + dicts = append(dicts, dictionary{d.Name, d.Desc}) + } + return dicts, nil +} |
