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


Golang Writer.Flush方法代碼示例

本文整理匯總了Golang中encoding/csv.Writer.Flush方法的典型用法代碼示例。如果您正苦於以下問題:Golang Writer.Flush方法的具體用法?Golang Writer.Flush怎麽用?Golang Writer.Flush使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在encoding/csv.Writer的用法示例。


在下文中一共展示了Writer.Flush方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: 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

示例2: 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

示例3: 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

示例4: writeTo

func writeTo(writer *csv.Writer, in interface{}) error {
	inValue, inType := getConcreteReflectValueAndType(in) // Get the concrete type (not pointer) (Slice<?> or Array<?>)
	if err := ensureInType(inType); err != nil {
		return err
	}
	inInnerWasPointer, inInnerType := getConcreteContainerInnerType(inType) // Get the concrete inner type (not pointer) (Container<"?">)
	if err := ensureInInnerType(inInnerType); err != nil {
		return err
	}
	inInnerStructInfo := getStructInfo(inInnerType) // Get the inner struct info to get CSV annotations
	csvHeadersLabels := make([]string, len(inInnerStructInfo.Fields))
	for i, fieldInfo := range inInnerStructInfo.Fields { // Used to write the header (first line) in CSV
		csvHeadersLabels[i] = fieldInfo.getFirstKey()
	}
	if err := writer.Write(csvHeadersLabels); err != nil {
		return err
	}
	inLen := inValue.Len()
	for i := 0; i < inLen; i++ { // Iterate over container rows
		for j, fieldInfo := range inInnerStructInfo.Fields {
			csvHeadersLabels[j] = ""
			inInnerFieldValue, err := getInnerField(inValue.Index(i), inInnerWasPointer, fieldInfo.IndexChain) // Get the correct field header <-> position
			if err != nil {
				return err
			}
			csvHeadersLabels[j] = inInnerFieldValue
		}
		if err := writer.Write(csvHeadersLabels); err != nil {
			return err
		}
	}
	writer.Flush()
	return writer.Error()
}
開發者ID:YaSuenag,項目名稱:hsbeat,代碼行數:34,代碼來源:encode.go

示例5: writeCsv

func writeCsv(ch chan []string) {
	var writer *csv.Writer
	if outputFn == "" {
		writer = csv.NewWriter(os.Stdout)
	} else {
		file, err := os.Create(outputFn)
		if err != nil {
			fmt.Println("Error:", err)
			os.Exit(1)
		}
		defer file.Close()
		writer = csv.NewWriter(file)
	}

	r, _ := utf8.DecodeRuneInString(outputSep)
	writer.Comma = r

	for row := range ch {
		err := writer.Write(row)
		if err != nil {
			fmt.Println("Error:", err)
			close(ch)
			return
		}
	}
	writer.Flush()
}
開發者ID:hlawrenz,項目名稱:csvmung,代碼行數:27,代碼來源:csvmung.go

示例6: 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

示例7: 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

示例8: 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

示例9: webQuitHandler

func webQuitHandler(diskwriter *csv.Writer, gzipwriter *gzip.Writer, csvfile *os.File) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(w, "flushing to disk and shutting down")
		diskwriter.Flush()
		if gzipwriter != nil {
			gzipwriter.Flush()
			gzipwriter.Close()
		}
		csvfile.Close()
		os.Exit(0)
	}
}
開發者ID:MPParsley,項目名稱:plugctl,代碼行數:12,代碼來源:helpers.go

示例10: csvWriter

func (c Tweet) csvWriter(writer *csv.Writer, m chan Tweet) {
	var mutex = &sync.Mutex{}
	for i := range m {
		c = i
		//fmt.Println(c)
		data := []string{c.User, c.Post_date, c.Message}
		mutex.Lock()
		writer.Write(data)
		writer.Flush()
		mutex.Unlock()
	}
}
開發者ID:starVader,項目名稱:elastic-report-golang,代碼行數:12,代碼來源:report1.2.go

示例11: 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

示例12: writeSif

func writeSif(nodes map[int64]string, edges []cx.Edge, w csv.Writer) {
	for i := range edges {

		edge := edges[i]

		if edge.I == "" {
			w.Write([]string{nodes[edge.S], "i", nodes[edge.T]})
		} else {
			w.Write([]string{nodes[edge.S], edge.I, nodes[edge.T]})
		}
	}
	w.Flush()
}
開發者ID:CyService,項目名稱:cxtool,代碼行數:13,代碼來源:cx2sif.go

示例13: CsvWriter

func (c Tweet) CsvWriter(writer *csv.Writer, m chan Tweet) {
	var mutex = &sync.Mutex{}
	for i := range m {
		c = i
		//fmt.Println(c)
		data := []string{c.User, c.Post_date, c.Message}
		//Introduced locks for write to csv file
		mutex.Lock()
		writer.Write(data)
		writer.Flush()
		mutex.Unlock()
		//lock closed
	}
}
開發者ID:starVader,項目名稱:elastic-report-golang,代碼行數:14,代碼來源:report-2-1.go

示例14: Summary

func (t TeamStats) Summary(w *csv.Writer) {
	err := w.Write([]string{
		fmt.Sprintf("%v", t.Scored),
		fmt.Sprintf("%v", t.OppScore),
		fmt.Sprintf("%v", t.Possesions),
		fmt.Sprintf("%.2f", 100*float64(t.Scored)/float64(t.Possesions)),
		fmt.Sprintf("%v", t.Ds),
		fmt.Sprintf("%v", t.OpponentTurns),
		fmt.Sprintf("%.2f", 100*float64(t.Ds)/float64(t.OpponentTurns)),
	})
	if err != nil {
		log.Fatal(err)
	}
	w.Flush()
}
開發者ID:Arrow,項目名稱:ulti_stats,代碼行數:15,代碼來源:ulti_stats.go

示例15: TeamHeader

func TeamHeader(w *csv.Writer) {
	err := w.Write([]string{
		"Scored",
		"Opponent Scored",
		"Possesions",
		"Efficiency",
		"D's",
		"Opponent Turns",
		"Percent D's",
	})
	if err != nil {
		log.Fatal(err)
	}
	w.Flush()
}
開發者ID:Arrow,項目名稱:ulti_stats,代碼行數:15,代碼來源:ulti_stats.go


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