golang的缓存io简单的使用
生活随笔
收集整理的這篇文章主要介紹了
golang的缓存io简单的使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
地址:https://github.com/polaris1119/The-Golang-Standard-Library-by-Example/blob/master/chapter01/01.4.md
上面的地址介紹了基本的使用,以及在readslice的坑上面做了解釋。
使用的場景中代碼如下,mapreduce中對文件根據map數分塊:
// 執行mapreduce前會先將分塊來進行處理 func (mr *MapReduce) Split(fileName string) {fmt.Printf("Split %s\n", fileName)infile, err := os.Open(fileName)if err != nil {log.Fatal("Split: ", err)}defer infile.Close()// 返回文件的基本信息fi, err := infile.Stat()if err != nil {log.Fatal("Split: ", err)}// 返回文件的大小size := fi.Size()// 得到文件每塊的大小,按照map數來分塊nchunk := size / int64(mr.nMap)nchunk += 1//kjv12.txt-0outfile, err := os.Create(MapName(fileName, 0))if err != nil {log.Fatal("Split: ", err)}//緩存的io的寫操作writer := bufio.NewWriter(outfile)m := 1i := 0//一塊一塊的進行讀取;創建一個讀取文件的scannerscanner := bufio.NewScanner(infile)// 存在數據就會一直掃描下去for scanner.Scan() {if int64(i) > nchunk*int64(m) {// 清除原來的緩存writer.Flush()outfile.Close()// 創建第二個writeoutfile, err = os.Create(MapName(fileName, m))writer = bufio.NewWriter(outfile)m += 1}// 一行一行的讀取數據line := scanner.Text() + "\n"writer.WriteString(line)i += len(line)}writer.Flush()outfile.Close() }
?
轉載于:https://www.cnblogs.com/52zjj/p/5814877.html
總結
以上是生活随笔為你收集整理的golang的缓存io简单的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【腾讯Bugly干货分享】动态链接库加载
- 下一篇: mybatis 一对一与一对多colle