/
pinyin.go
50 lines (40 loc) · 998 Bytes
/
pinyin.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
package pinyin
import (
"bytes"
"slices"
"strings"
)
type Entry struct {
Word string
Pinyin []string
Freq int
}
func (e *Entry) GetWord() string { return e.Word }
type Format interface {
GetName() string
GetID() string
GetCanMarshal() bool
Marshal([]*Entry) []byte
Unmarshal(*bytes.Reader) []*Entry
GetKind() int
}
type Template struct {
Name string // 名称
ID string // 唯一 ID
CanMarshal bool // 是否可以序列化
}
func (t *Template) GetName() string { return t.Name }
func (t *Template) GetID() string { return t.ID }
func (t *Template) GetCanMarshal() bool { return t.CanMarshal }
func (t *Template) Marshal([]*Entry) []byte { return nil }
func (t *Template) GetKind() int { return PINYIN }
const PINYIN = 1
var FormatList = make([]Format, 0, 20)
func New(format string) Format {
for _, v := range FormatList {
if slices.Contains(strings.Split(v.GetID(), ","), format) {
return v
}
}
return nil
}