-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
141 lines (111 loc) · 2.42 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package main
import (
"bufio"
"fmt"
"io"
"log"
"os"
"path"
"time"
)
var TEMPLATEFILE string = "./0_Daily_Prac/template.go"
func main() {
template, err := readTemplate()
if err != nil {
fmt.Println(err)
return
}
err = createDailyPracticeFile(template)
if err != nil {
fmt.Println(err)
}
}
func readTemplate() ([]string, error) {
f, err := os.Open(TEMPLATEFILE)
if err != nil {
return nil, err
}
defer f.Close()
r := bufio.NewReader(f)
data := make([]string, 0)
for {
line, err := r.ReadString('\n')
if err == io.EOF {
break
} else if err != nil {
fmt.Println(err)
return nil, err
}
data = append(data, line)
}
for i := 0; i < len(data); i++ {
if data[i] == "// COPY FROM HERE\n" {
data = data[i+1:]
break
}
}
return data, nil
}
func createDailyPracticeFile(template []string) error {
today := time.Now().Format("20060102")
dirName := "./0_Daily_Prac/" + today
err := os.Mkdir(dirName, os.ModePerm)
if err != nil {
return err
}
f, err := os.Create(dirName + "/" + today + ".go")
if err != nil {
return err
}
defer f.Close()
// insert package name to template
packageName := "package " + "_" + string([]byte(today)[1:]) + "\n\n"
template = append([]string{packageName}, template...)
sum := 0
for i := 0; i < len(template); i++ {
n, err := io.WriteString(f, template[i])
if err != nil {
return err
}
sum += n
}
fmt.Printf("Create Daily practice success with %d data\n", sum)
defer cleanUp(err)
return nil
}
func cleanUp(err error) {
if err != nil {
fmt.Println("need remove file")
}
}
func myLog(input interface{}) {
LOGFILE := path.Join(os.TempDir(), "mGo.log")
f, err := os.OpenFile(LOGFILE, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
fmt.Println(err)
return
}
defer f.Close()
LstFlags := log.Ldate | log.Lshortfile
iLog := log.New(f, "LNum ", LstFlags)
iLog.Println(input)
iLog.SetFlags(log.Lshortfile | log.LstdFlags)
}
func printSubsequence(s string) {
res := make([]string, 0)
combinationOfSubSequence(s, 0, &res, make([]byte, 0))
fmt.Println(res)
}
func combinationOfSubSequence(s string, currentIndex int, res *[]string, current []byte) {
if currentIndex > len(s) {
return
}
if len(current) > 0 {
*res = append(*res, string(current))
}
for i := currentIndex; i < len(s); i++ {
current = append(current, s[i])
combinationOfSubSequence(s, i+1, res, current)
current = current[:len(current)-1]
}
}