本文整理汇总了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
}
}
示例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()
}
示例3: writeOneRecordAtaTime
func writeOneRecordAtaTime(writer *csv.Writer, data [][]string) {
for _, row := range data {
writer.Write(row)
}
writer.Flush()
}
示例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()
}
示例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()
}
示例6: flush
func (p *csvProcessor) flush(writer *csv.Writer, err error) error {
if err != nil {
return err
}
writer.Flush()
return writer.Error()
}
示例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
}
示例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)
}
}
}
示例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)
}
}
示例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()
}
}
示例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()
}
示例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()
}
示例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
}
}
示例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()
}
示例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()
}