aboutsummaryrefslogtreecommitdiff
path: root/dict.go
blob: fa9690e62d468b1c8f2db926103cf6f057c3eab7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
}