fix csv Flush;

This commit is contained in:
Balakrishnan Balasubramanian 2022-05-02 12:40:38 -04:00
parent d284956268
commit 8637252785
3 changed files with 61 additions and 8 deletions

View File

@ -104,5 +104,6 @@ func (d *db) Save(records []Record) error {
strings.ReplaceAll(r.FeedEntry.Content, "\n", " "), strings.ReplaceAll(r.FeedEntry.Content, "\n", " "),
}) })
} }
csvw.Flush()
return nil return nil
} }

30
exp/csvw/main.go Normal file
View File

@ -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")
}

View File

@ -1,9 +1,11 @@
package main package main
import ( import (
"bytes"
"encoding/csv" "encoding/csv"
"encoding/xml" "encoding/xml"
"fmt" "fmt"
"io"
"log" "log"
"os" "os"
"strings" "strings"
@ -27,7 +29,7 @@ type Item struct {
func main() { func main() {
fmt.Println("hello go") 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 { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -48,25 +50,45 @@ func main() {
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
csvw := csv.NewWriter(f) var b bytes.Buffer
b.WriteString("hello world")
csvw := csv.NewWriter(&b)
csvw.Write([]string{ csvw.Write([]string{
"date", "date",
"link", "link",
"info", "info",
}) })
for index, item := range v.Items { for index, item := range v.Items[:3] {
fmt.Println(index, item.Title)
//fmt.Println(item.Content)
c := item.Content c := item.Content
csvw.Write([]string{ fmt.Println(index, len(c), item.Title)
//fmt.Println(item.Content)
err = csvw.Write([]string{
time.Now().String(), time.Now().String(),
item.Link, item.Link,
strings.Replace(c, "\n", " ", -1), 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() { func foo() {
x := ` x := `