ZeroBot-Plugin/plugin_diana/tools/convert.go
2021-08-06 18:52:41 +08:00

53 lines
925 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Package convert 转换txt到pb
package main
import (
"bufio"
"fmt"
"os"
"github.com/FloatTech/ZeroBot-Plugin/plugin_diana/data"
)
var (
compo data.Composition
)
func init() {
compo.Array = make([]string, 0, 64)
}
// 参数txt文件位置 pb文件位置
func main() {
file, err := os.Open(os.Args[1])
if err != nil {
panic(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
// optionally, resize scanner's capacity for lines over 64K, see next example
for scanner.Scan() {
// fmt.Println(scanner.Text())
compo.Array = append(compo.Array, scanner.Text())
}
if err := scanner.Err(); err != nil {
panic(err)
}
data, _ := compo.Marshal()
f, err1 := os.OpenFile(os.Args[2], os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644)
if err1 == nil {
defer f.Close()
_, err2 := f.Write(data)
if err2 == nil {
fmt.Println("成功")
} else {
panic(err2)
}
} else {
panic(err1)
}
}