golang——写文件和读文件

          之前聊过,操作文件——读写文件,直接调用接口即可. 如果是一直写入操作,写入操作一直进行的,免不了会有,有时一大批数据过来,有时没有一条数据。

鉴于此场景,选择用select....channel 的模型处理。 代码如下:

package main

import (
    "fmt"
    "time"
    "os"
    "strconv"
    "strings"
)


func main() {

  fd , _ := os.OpenFile("a.txt",os.O_RDWR|os.O_CREATE|os.O_APPEND,0644)
    var a chan string
    a =make(chan string)
    go sendDataTo(a)

    getAchan(10*time.Second,a, fd)
    fd.Close()
}
 
func sendDataTo(a chan string)  {
    // for {
    //      a <- "我是a通道的数据"
    //     time.Sleep(1e9 *3)
    // }
        for i:=0; i<10; i++{
            time.Sleep(1e9 *2)
            a <- strconv.Itoa(i)
        }
}
 
//在一定时间内接收不到a的数据则超时
func getAchan(timeout time.Duration, a chan string, fd *os.File)  {

   var after <-chan time.Time
   loop:

   var BuffItems  []string
   after = time.After(timeout)
   
    for{
        fmt.Println("等待a中的数据,10秒后没有数据则再重新接收")
        select {
        case x :=<- a:
            BuffItems = append(BuffItems,x)if  len(BuffItems) <5 {continue                      
               }else{
                        dd :=     strings.Join(BuffItems, ",")
                        fmt.Println("@@@......",dd)
                        content := "写入的文件内容" + dd
                        fd.WriteString(content)
                        fd.WriteString("\n")        
               }
                     
            goto loop
        case <-after:
            fmt.Println("timeout.")

                dd :=  strings.Join(BuffItems, ",")
                fmt.Println("@@@...timeout1111...",dd)

                if len(dd) <1{
                    goto loop
                }else{
                content := "写入的文件内容timeout" + dd
                fd.WriteString(content)
                fd.WriteString("\n")      
              }
            goto loop
           // return
       
        }
    }
}

可根据 超时时间和接收数据的数量,两个维度处理。没有数据在channel接收到的时候,则等待。

 下面这种是传统的对文件操作的方式,并统计写文件的时间。

package main

import (
    "fmt"
    "time"
    "reflect"
    "os"
    "strconv"
    "strings"
)


//耗时统计函数
func timeCost() func() {
    start := time.Now()
    return func() {
        tc := time.Since(start)
        fmt.Printf("time cost = %v\n", tc)
    }
}

func sum(n int, fd *os.File)  {

    defer timeCost()()        //注意,是对 timeCost()返回的函数进行调用,因此需要加两对小括号

    for i:=1; i <= n; i++ {
        stri := strconv.Itoa(i)
           content := "写入的文件内容" + stri
        fd.WriteString(content)
        fd.WriteString("\n")
  }
}

func main() {

    fd , _ := os.OpenFile("a1.txt",os.O_RDWR|os.O_CREATE|os.O_APPEND,0644)
    sum(10000,fd)
    time.Sleep(1000* time.Second)
    fd.Close()
}

相关推荐