當前位置: 首頁>>代碼示例>>Golang>>正文


Golang csv.Writer類代碼示例

本文整理匯總了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)
	}
}
開發者ID:toisin,項目名稱:astro-world,代碼行數:27,代碼來源:workflow.go

示例2: writeOneRecordAtaTime

func writeOneRecordAtaTime(writer *csv.Writer, data [][]string) {

	for _, row := range data {
		writer.Write(row)
	}
	writer.Flush()
}
開發者ID:robertojrojas,項目名稱:go-database-frameworks-and-tools,代碼行數:7,代碼來源:main.go

示例3: flush

func (p *csvProcessor) flush(writer *csv.Writer, err error) error {
	if err != nil {
		return err
	}
	writer.Flush()
	return writer.Error()
}
開發者ID:jchannon,項目名稱:negotiator,代碼行數:7,代碼來源:csvprocessor.go

示例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()
}
開發者ID:callmegarrus,項目名稱:some_task,代碼行數:26,代碼來源:modified_solution.go

示例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:
	}
}
開發者ID:nkts,項目名稱:golang-devops-stuff,代碼行數:27,代碼來源:export.go

示例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)
}
開發者ID:jchannon,項目名稱:negotiator,代碼行數:7,代碼來源:csvprocessor.go

示例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)
}
開發者ID:jchannon,項目名稱:negotiator,代碼行數:7,代碼來源:csvprocessor.go

示例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
	}
}
開發者ID:prakashsanker,項目名稱:optimus,代碼行數:30,代碼來源:csv.go

示例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
}
開發者ID:pombredanne,項目名稱:kveer,代碼行數:59,代碼來源:append.go

示例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)
}
開發者ID:jchannon,項目名稱:negotiator,代碼行數:8,代碼來源:csvprocessor.go

示例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)
		}
	}
}
開發者ID:VeryFatBoy,項目名稱:dbbench,代碼行數:55,代碼來源:process.go

示例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
}
開發者ID:MattSPalmer,項目名稱:objcsv,代碼行數:11,代碼來源:csv.go

示例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)
}
開發者ID:kjk,項目名稱:contentstore,代碼行數:11,代碼來源:store.go

示例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())})
}
開發者ID:marcsauter,項目名稱:flightstat,代碼行數:13,代碼來源:flightstat.go

示例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()
}
開發者ID:stone,項目名稱:qhttp,代碼行數:13,代碼來源:qhttp.go


注:本文中的encoding/csv.Writer類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。