-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathonline.go
61 lines (57 loc) · 1.48 KB
/
online.go
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package mydictionary
import (
"os"
"path/filepath"
"strings"
)
// quary vocabulary online
func requestOnline(vocabularyAsk VocabularyAskStruct) (vocabularyAnswerList []VocabularyAnswerStruct) {
var (
vocabularyAnswerChannel chan VocabularyAnswerStruct
vocabularyAnswer VocabularyAnswerStruct
)
// prepare
vocabularyAnswerChannel = make(chan VocabularyAnswerStruct, len(onlineList))
// query
for i := 0; i < len(onlineList); i++ {
go func(index int) {
vocabularyAnswerChannel <- onlineList[index].Query(vocabularyAsk)
}(i)
}
// add to answer list
for i := 0; i < len(onlineList); i++ {
vocabularyAnswer = <-vocabularyAnswerChannel
if Setting.Online.Debug {
vocabularyAnswerList = append(vocabularyAnswerList, vocabularyAnswer)
} else if strings.Compare(vocabularyAnswer.Status, Basic) == 0 {
vocabularyAnswerList = append(vocabularyAnswerList, vocabularyAnswer)
}
}
return
}
func loadCache() (err error) {
os.Mkdir(cachePath, 0755)
for i := 0; i < len(onlineList); i++ {
err = onlineList[i].GetCache().Read(cachePath+string(filepath.Separator)+onlineList[i].GetServiceName()+".json", Setting.Online.Cache.ShelfLifeDay)
if err != nil {
return
}
}
return
}
func saveCache() (success bool, information string) {
var (
err error
temp string
)
success = true
for i := 0; i < len(onlineList); i++ {
temp, err = onlineList[i].GetCache().Write()
if err != nil {
temp = err.Error() + "\n\n"
success = false
}
information += temp
}
return
}