-
Notifications
You must be signed in to change notification settings - Fork 80
/
main.go
125 lines (112 loc) · 3.18 KB
/
main.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package main
import (
"bufio"
"fmt"
"os"
"path/filepath"
"strings"
"time"
)
type books struct {
name string
size string
path string
bytes int64
}
const (
_ = iota
KB float64 = 1 << (10 * iota)
MB
GB
)
func fileSize(i int64) string {
b := float64(i)
switch {
case b >= GB:
return fmt.Sprintf("%.2fGB", b/GB)
case b >= MB:
return fmt.Sprintf("%.2fMB", b/MB)
case b >= KB:
return fmt.Sprintf("%.2fKB", b/KB)
}
return fmt.Sprintf("%.2fBytes", b)
}
func walkDir(dirPth, suffix string) (files []books, content string, err error) {
files = make([]books, 0, 30)
suffix = strings.ToUpper(suffix)
err = filepath.Walk(dirPth, func(filename string, fi os.FileInfo, err error) error {
// 忽略目录
if fi.IsDir() {
switch len(strings.Split(filename, "\\")) {
case 1:
content += "### "
case 2:
content += "* "
case 3:
content += " * "
}
content += fi.Name() + "\r\n"
return nil
}
if strings.HasSuffix(strings.ToUpper(fi.Name()), suffix) {
input := books{
name: fi.Name(),
size: fileSize(fi.Size()),
path: strings.Replace(filename, "\\", "/", -1),
bytes: fi.Size(),
}
switch len(strings.Split(filename, "\\")) {
case 3:
content += " * "
case 4:
content += " * "
}
//[静态资源](/静态资源/README.md)
content += fmt.Sprintf("[%s [%s]](./%s)", strings.Replace(strings.Split(input.name, ".")[0], "_", " ", -1), input.size, input.path) + "\r\n"
files = append(files, input)
}
return nil
})
return files, content, err
}
func writeMarkDown(fileName, content string) {
// open output file
fo, err := os.Create(fileName + ".md")
if err != nil {
panic(err)
}
// close fo on exit and check for its returned error
defer func() {
if err := fo.Close(); err != nil {
panic(err)
}
}()
// make a write buffer
w := bufio.NewWriter(fo)
w.WriteString(content)
w.Flush()
}
func main() {
var count int
var sum int64
var content string
content = `# Books
把自己读过的一些书分享出来给大家,涉及新思想、新科技、算法、人工智能、语言编程类等等,每本书都是严格挑选,这个库也会持续不断的更新。
这些资源来自于互联网共享于互联网(from the Internet, for the Internet)。如果您有好书也不妨提交一个issue,共享出来;如果涉嫌侵权,您也可以提交一个issue告知,我会及时删除。在此,对这些书的作者(或译者)们表示感谢!
如果您觉得这个repository还不错,不妨点个star。如果您还愿意给我来杯咖啡,我会更加感谢,并把您列入下列赞助者名单!您的支持是我继续前进的动力!
![微信支付](./weixin.jpg)
`
if f, c, e := walkDir("./图书目录", ""); e == nil {
count = len(f)
for _, v := range f {
sum += v.bytes
}
content += c
}
content += "\r\n" + fmt.Sprint("共 ", count, " 本书,计 ", fileSize(sum)) + ",最后更新时间:" + time.Now().Format("2006年1月2日 15:04:05") + "\r\n"
content += `
## 鸣谢
项目赞助者:
`
writeMarkDown("README", content)
}