package main import ( "bytes" "encoding/csv" "encoding/xml" "fmt" "io" "log" "os" "strings" "time" ) /* XMLName xml.Name `xml:"person"` FirstName string `xml:"firstname"` Parents interface{} `xml:"parent"` */ type Item struct { Title string `xml:"title"` Link string `xml:"link"` Author string `xml:"author"` Guid string `xml:"guid"` Description string `xml:"description"` Content string `xml:",innerxml"` } func main() { fmt.Println("hello go") data, err := os.ReadFile("/home/balki/projects/tss/w.tmp/ounapuu.xml") if err != nil { log.Fatal(err) } v := struct { Items []Item `xml:"channel>item"` }{} err = xml.Unmarshal(data, &v) if err != nil { log.Fatal(err) } f, err := os.OpenFile("./data.csv", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) defer f.Close() if err != nil { log.Fatal(err) } var b bytes.Buffer b.WriteString("hello world") csvw := csv.NewWriter(&b) csvw.Write([]string{ "date", "link", "info", }) for index, item := range v.Items[:3] { c := item.Content 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 := ` slkdfjdslk ` s := struct { Bar struct { Content string `xml:",innerxml"` } `xml:"bar"` }{} err := xml.Unmarshal([]byte(x), &s) if err != nil { log.Fatal(err) } fmt.Println(s.Bar.Content) }