本文整理汇总了Golang中encoding/csv.Writer类的典型用法代码示例。如果您正苦于以下问题:Golang Writer类的具体用法?Golang Writer怎么用?Golang Writer使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Writer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: writePromptInText
func writePromptInText(pId string, pc PromptConfig, writer *csv.Writer, level int, evalue ExpectedResponseValue) {
indent := ""
pPromptType := ""
for i := 0; i < level; i++ {
indent = indent + " "
}
if pId == "" {
pId = pc.Id
pPromptType = pc.PromptType
}
pId = indent + pId
pText := pc.Text
supportPromptId := pc.SupportPromptRef.Id
var value = []string{strconv.Itoa(level), pId, pPromptType, evalue.Id, supportPromptId, "", "", ""}
for i, v := range pText {
value[5+i] = v
}
err := writer.Write(value)
if err != nil {
log.Fatal("Cannot write file", err)
}
level++
ecs := pc.ExpectedResponses.Values
for _, v := range ecs {
writePromptInText(v.NextPromptRef.Id, v.NextPrompt, writer, level, v)
}
}
示例2: writeOneRecordAtaTime
func writeOneRecordAtaTime(writer *csv.Writer, data [][]string) {
for _, row := range data {
writer.Write(row)
}
writer.Flush()
}
示例3: flush
func (p *csvProcessor) flush(writer *csv.Writer, err error) error {
if err != nil {
return err
}
writer.Flush()
return writer.Error()
}
示例4: writeSalesData
func writeSalesData(conn *sql.DB, writer *csv.Writer, first int, last int, wg *sync.WaitGroup) {
defer wg.Done()
rows, err := conn.Query("select * from sales where id between ? and ?", first, last)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
var orderId int
var userId int
var orderAmount float64
for rows.Next() {
rows.Scan(&orderId, &userId, &orderAmount)
writer.Write([]string{
strconv.Itoa(orderId),
strconv.Itoa(userId),
strconv.FormatFloat(orderAmount, 'f', 6, 64),
},
)
}
writer.Flush()
}
示例5: csvExporter
func csvExporter(c *csv.Writer, ms []*Metric) {
lc := make(chan *LabelSet)
quit := make(chan bool)
for _, m := range ms {
go m.EmitLabelSets(lc, quit)
for {
select {
case l := <-lc:
record := []string{m.Program,
m.Name,
m.Kind.String()}
for k, v := range l.labels {
record = append(record, k, v)
}
record = append(record, fmt.Sprintf("%s", l.datum.Time))
record = append(record, fmt.Sprintf("%d", l.datum.Get()))
err := c.Write(record)
if err != nil {
log.Printf("Failed to write csv record %q: %s\n", record, err)
}
case <-quit:
goto next
}
}
next:
}
}
示例6: writeArrayOfScalars
func writeArrayOfScalars(writer *csv.Writer, vj reflect.Value) error {
sa := make([]string, vj.Len())
for i := 0; i < vj.Len(); i++ {
sa[i] = fmt.Sprintf("%v", reflect.Indirect(vj.Index(i)))
}
return writer.Write(sa)
}
示例7: writeStructFields
func writeStructFields(writer *csv.Writer, str reflect.Value, dataModel interface{}) error {
sa := make([]string, str.NumField())
for i := 0; i < str.NumField(); i++ {
sa[i] = fmt.Sprintf("%v", reflect.Indirect(str.Field(i)))
}
return writer.Write(sa)
}
示例8: NewWithCsvWriter
// NewWithCsvWriter writes all of the Rows in a Table to a CSV file using the options in the CSV writer.
// It assumes that all Rows have the same headers. Columns are written in alphabetical order.
func NewWithCsvWriter(writer *csv.Writer) optimus.Sink {
return func(source optimus.Table) error {
defer source.Stop()
headers := []string{}
wroteHeader := false
for row := range source.Rows() {
if !wroteHeader {
headers = convertRowToHeader(row)
sort.Strings(headers)
if err := writer.Write(headers); err != nil {
return err
}
wroteHeader = true
}
if err := writer.Write(convertRowToRecord(row, headers)); err != nil {
return err
}
}
if source.Err() != nil {
return source.Err()
}
writer.Flush()
if writer.Error() != nil {
return writer.Error()
}
return nil
}
}
示例9: NewAppend
func NewAppend(path string) *kvAppend {
kv := &kvAppend{
kvMem: NewMemory(),
queue: make(chan *record),
}
var lastErr error
f, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0644)
if err != nil {
lastErr = err
} else {
r := csv.NewReader(f)
for {
rec, err := r.Read()
if err != nil {
if err != io.EOF {
lastErr = err
}
break
}
kv.kvMem.m[rec[0]] = []byte(rec[1])
}
}
var w *csv.Writer
if lastErr == nil {
w = csv.NewWriter(f)
}
go func() {
for r := range kv.queue {
if lastErr == nil {
var rec []string
if r.Value != nil {
rec = []string{r.Key, string(r.Value)}
} else {
rec = []string{r.Key}
}
if err = w.Write(rec); err != nil {
lastErr = err
}
if r != nil && r.Err != nil {
w.Flush()
f.Sync()
r.Err <- nil
close(r.Err)
}
} else if r.Err != nil {
r.Err <- lastErr
}
}
if f != nil {
f.Close()
}
}()
return kv
}
示例10: writeArrayOfStringers
func writeArrayOfStringers(writer *csv.Writer, value reflect.Value) error {
debug(" -- writeArrayOfStringers %d\n", value.Len())
sa := make([]string, value.Len())
for i := 0; i < value.Len(); i++ {
sa[i] = fmt.Sprintf("%v", reflect.Indirect(value.Index(i)).Interface().(fmt.Stringer))
}
return writer.Write(sa)
}
示例11: processResults
func processResults(config *Config, resultChan <-chan *JobResult) map[string]*JobStats {
var resultFile *csv.Writer
var allTestStats = make(map[string]*JobStats)
var recentTestStats = make(map[string]*JobStats)
if len(*queryStatsFile) > 0 {
if file, err := os.Create(*queryStatsFile); err != nil {
log.Fatalf("Could not open result file %s: %v",
*queryStatsFile, err)
} else {
defer file.Close()
resultFile = csv.NewWriter(file)
defer resultFile.Flush()
}
}
ticker := time.NewTicker(*updateInterval)
if !*intermediateUpdates {
ticker.Stop()
}
defer ticker.Stop()
for {
select {
case jr, ok := <-resultChan:
if !ok {
return allTestStats
}
if resultFile != nil {
resultFile.Write([]string{
jr.Name,
strconv.FormatInt(jr.Start.Nanoseconds()/1000, 10),
strconv.FormatInt(jr.Elapsed.Nanoseconds()/1000, 10),
strconv.FormatInt(jr.RowsAffected, 10),
})
}
if _, ok := allTestStats[jr.Name]; !ok {
allTestStats[jr.Name] = new(JobStats)
}
if _, ok := recentTestStats[jr.Name]; !ok {
recentTestStats[jr.Name] = new(JobStats)
}
allTestStats[jr.Name].Update(jr)
recentTestStats[jr.Name].Update(jr)
case <-ticker.C:
for name, stats := range recentTestStats {
log.Printf("%s: %v", name, stats)
}
recentTestStats = make(map[string]*JobStats)
}
}
}
示例12: writeHeader
func writeHeader(w *csv.Writer, d interface{}) error {
v := reflect.ValueOf(d)
header := make([]string, v.NumField())
for i := 0; i < v.NumField(); i++ {
header[i] = v.Type().Field(i).Name
}
if err := w.Write(header); err != nil {
return err
}
return nil
}
示例13: writeBlobRec
func writeBlobRec(csvWriter *csv.Writer, blob *blob) error {
sha1Str := hex.EncodeToString(blob.sha1[:])
rec := [][]string{
{
sha1Str,
strconv.Itoa(blob.nSegment),
strconv.Itoa(blob.offset),
strconv.Itoa(blob.size),
}}
return csvWriter.WriteAll(rec)
}
示例14: Csv
// Csv calls each day and then writes the month statistics in csv format
func (fsm *FlightStatMonth) Csv(w *csv.Writer) {
days := []int{}
for d, _ := range fsm.Day {
days = append(days, d)
}
sort.Ints(days)
for _, d := range days {
fsd := fsm.Day[d]
fsd.Csv(w)
}
w.Write([]string{fmt.Sprintf("Total %s", fsm.Date.Format("January 2006")), fmt.Sprintf("%d", fsm.Flights), fmt.Sprintf("%.2f", fsm.Airtime.Minutes())})
}
示例15: writeCsvLine
// Takes a *result struct and writes out lines to *csv.Writer
func writeCsvLine(w *csv.Writer, res *result) {
headers_joined := strings.Join(res.headers, ",")
// When we save to CSV duration is always in seconds
duration_seconds := fmt.Sprintf("%v", res.time.Seconds())
// We need a array of strings for the csv package.
record := []string{res.url, res.httpStatus, headers_joined, duration_seconds}
err := w.Write(record)
if err != nil {
fmt.Println("Problems writing to csv file")
}
w.Flush()
}