appendStruct
function is designed to run in multiple threads in order to collect and append DataItem
into DataContainer
. So far I can print the result from inner appendStruct
Q1: how to access and print container
from main
, Q2: save that struct data type to csv
from main
?
package main
import "fmt"
type DataItem struct {
name string
}
type DataContainer struct {
Items []DataItem
}
func (box *DataContainer) AddItem(item DataItem) []DataItem {
box.Items = append(box.Items, item)
return box.Items
}
func appendStruct() {
items := []DataItem{}
container := DataContainer{items}
item1 := DataItem{name: fmt.Sprintf("Item1")}
item2 := DataItem{name: fmt.Sprintf("Item2")}
container.AddItem(item1)
container.AddItem(item2)
var ss = fmt.Sprintf("", container)
fmt.Println(ss)
}
func main() {
appendStruct()
}
OUTPUT from go run test.go
is:
%!(EXTRA main.DataContainer={[{Item1} {Item2}]})
re Q1. "encoding/csv"
has to implement string interface [][]string
there is a hint how to approach it in Write struct to csv file
but lacks implementation example.