标签:%s break 接口 string 发送 UNC == temp port
代码只有自己懂了,
写得比较肿,以后再优化吧。
就是每天定期,或是每次生成软件包时,收集软件包的信息,
发送到go的api接口,
然后,再作数据分析。
utils.go
package main import ( "bufio" "fmt" "io" "io/ioutil" "os" "path/filepath" ) func GetAllFile(pathname string, files []string) ([]string, error) { fromSlash := filepath.FromSlash(pathname) //fmt.Println(fromSlash) rd, err := ioutil.ReadDir(fromSlash) if err != nil { //log.LOGGER("Error").Error("read dir fail %v\n", err) fmt.Println("read dir fail:", err) return files, err } for _, fi := range rd { if fi.IsDir() { fullDir := filepath.Join(fromSlash, fi.Name()) files, err = GetAllFile(fullDir, files) if err != nil { fmt.Println("read dir fail:", err) //log.LOGGER("Error").Error("read dir fail: %v\n", err) return files, err } } else { fullName := filepath.Join(fromSlash, fi.Name()) files = append(files, fullName) } } return files, nil } func WritetoFile(fileName string, content []string) error { f, err := os.OpenFile(fileName, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644) if err != nil { fmt.Printf("create map file error: %v\n", err) return err } defer f.Close() w := bufio.NewWriter(f) for _, item := range content { fmt.Fprintln(w, item) } return w.Flush() } func ReadFromFile(fileName string) ([]string, error) { content := []string{} fi, err := os.Open(fileName) if err != nil { fmt.Printf("Error: %s\n", err) return nil, err } defer fi.Close() br := bufio.NewReader(fi) for { item, _, c := br.ReadLine() if c == io.EOF { break } content = append(content, string(item)) } return content, nil }
main.go
package main import ( "fmt" ) func main() { var oldContent, newContent []string logFile := "D:\\temp\\log.txt" oldContent, err := ReadFromFile(logFile) if err != nil { panic(err) } for i, v := range oldContent { fmt.Println(i, v) } newContent, err = GetAllFile("D:\\software", newContent) if err != nil { panic(err) } err = WritetoFile(logFile, newContent) if err != nil { panic(err) } }
标签:%s break 接口 string 发送 UNC == temp port
原文地址:https://www.cnblogs.com/aguncn/p/12026381.html