From 8637252785219b8949656780a3b1dee79aca2850 Mon Sep 17 00:00:00 2001 From: balki <3070606-balki@users.noreply.gitlab.com> Date: Mon, 2 May 2022 12:40:38 -0400 Subject: [PATCH] fix csv Flush; --- app/db.go | 1 + exp/csvw/main.go | 30 ++++++++++++++++++++++++++++++ exp/xml/xml.go | 38 ++++++++++++++++++++++++++++++-------- 3 files changed, 61 insertions(+), 8 deletions(-) create mode 100644 exp/csvw/main.go diff --git a/app/db.go b/app/db.go index 2746406..8afe5d7 100644 --- a/app/db.go +++ b/app/db.go @@ -104,5 +104,6 @@ func (d *db) Save(records []Record) error { strings.ReplaceAll(r.FeedEntry.Content, "\n", " "), }) } + csvw.Flush() return nil } diff --git a/exp/csvw/main.go b/exp/csvw/main.go new file mode 100644 index 0000000..3380d8f --- /dev/null +++ b/exp/csvw/main.go @@ -0,0 +1,30 @@ +package main + +import ( + "bytes" + "encoding/csv" + "fmt" + "io" +) + +func main() { + fmt.Println("vim-go") + var b bytes.Buffer + writer := csv.NewWriter(&b) + err := writer.Write([]string{ + "foo", + "bar", + "blah", + }) + if err != nil { + panic(err) + } + writer.Flush() + b.WriteString("Hello World") + content, err := io.ReadAll(&b) + if err != nil { + panic(err) + } + fmt.Printf("%s\n", content) + fmt.Println("bye") +} diff --git a/exp/xml/xml.go b/exp/xml/xml.go index 3c8d252..b114150 100644 --- a/exp/xml/xml.go +++ b/exp/xml/xml.go @@ -1,9 +1,11 @@ package main import ( + "bytes" "encoding/csv" "encoding/xml" "fmt" + "io" "log" "os" "strings" @@ -27,7 +29,7 @@ type Item struct { func main() { fmt.Println("hello go") - data, err := os.ReadFile("./ounapuu.xml") + data, err := os.ReadFile("/home/balki/projects/tss/w.tmp/ounapuu.xml") if err != nil { log.Fatal(err) } @@ -48,25 +50,45 @@ func main() { if err != nil { log.Fatal(err) } - csvw := csv.NewWriter(f) + var b bytes.Buffer + b.WriteString("hello world") + csvw := csv.NewWriter(&b) csvw.Write([]string{ "date", "link", "info", }) - for index, item := range v.Items { - fmt.Println(index, item.Title) - //fmt.Println(item.Content) + for index, item := range v.Items[:3] { c := item.Content - csvw.Write([]string{ + fmt.Println(index, len(c), item.Title) + //fmt.Println(item.Content) + err = csvw.Write([]string{ time.Now().String(), item.Link, strings.Replace(c, "\n", " ", -1), }) - + if err != nil { + panic(err) + } + /* + err = csvw.Write([]string{ + time.Now().String(), + "dummy.link", + "dummy content, lskdjf lksjdf lkjk", + }) + if err != nil { + panic(err) + } + */ } - + csvw.Flush() + err = f.Sync() + fmt.Println(err) + data2, err := io.ReadAll(&b) + fmt.Printf("data2: %s\n", data2) + fmt.Println(err) + // fmt.Println(v.Items[len(v.Items)-1].Content) } func foo() { x := `