-
Notifications
You must be signed in to change notification settings - Fork 0
/
articales.go
57 lines (49 loc) · 1.21 KB
/
articales.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path"
)
type ArticleType int
const (
articleTypeVide = 0
articleTypeYouTube = 1
)
type ArticleData struct {
ID int `json:"-,"`
Title string `json:"title"`
Type ArticleType `json:"type"`
IsGenuine bool `json:"isGenuine"`
Src string `json:"src"`
ShortDesc string `json:"shortDescription"`
LongDesc string `json:"longDescription"`
Warning string `json:"warning"`
}
var articles = make([]*ArticleData, 0)
func loadArticles(dir string) (int, error) {
i := 1
for ; ; i++ {
filename := path.Join(dir, fmt.Sprintf("%d.json", i))
fileInfo, err := os.Stat(filename)
if err != nil {
if os.IsNotExist(err) {
break
}
return 0, fmt.Errorf("error while checking article %d: %s", i, err)
}
dat, err := ioutil.ReadFile(path.Join(dir, fileInfo.Name()))
if err != nil {
return 0, fmt.Errorf("error while loading article %d: %s", i, err)
}
article := ArticleData{}
err = json.Unmarshal(dat, &article)
if err != nil {
return 0, fmt.Errorf("error while unmarshalling article %d: %s", i, err)
}
article.ID = i
articles = append(articles, &article)
}
return i - 1, nil
}