go's io

作者: bocsoft | 来源:发表于2018-12-25 14:24 被阅读0次

    大端序就是把数据高字节存贮到内存低地址中,小端序反之。

    package main
    
    import (
        "bufio"
        "encoding/binary"
        "fmt"
        "io"
        "math/rand"
        "os"
    )
    
    func main() {
        //TestIo()
        largeIo()
    
    }
    
    func largeIo()  {
        const filename = "large.in"
        const n = 100000000 // 8*100*1000*100 = 800M
        file, err := os.Create(filename)
        if err != nil {
            panic(err)
        }
        defer file.Close()
        p := randomSource(n)
        //io buffer 加快文件读写
        writer := bufio.NewWriter(file)
        writerSink(writer,p)
        writer.Flush() // flush掉 io buffer 中的数据
        //读
        file, err = os.Open(filename)
        if err != nil{
            panic(err)
        }
        defer file.Close()
        p = readerSource(bufio.NewReader(file))
        count := 0
        for v := range p{
            fmt.Println(v)
            count++
            if count >= 10{
                break
            }
        }
    
    }
    //文件读写测试
    func TestIo() {
        //写
        file, err := os.Create("small.in") //二进制数据
        if err != nil {
            panic(err) //遇到错误,暂时不处理
        }
        defer file.Close()    //运行结束后,自动关闭文件
        p := randomSource(50) // smaill.in 的大小为 8*50,400字节
        writerSink(file, p)
        //读
        file, err = os.Open("small.in")
        if err != nil {
            panic(err)
        }
        defer file.Close()
        p = readerSource(file)
        for v := range p {
            fmt.Println(v)
        }
    }
    
    func readerSource(reader io.Reader) <-chan int {
        out := make(chan int)
        go func() {
            buffer := make([]byte, 8) //int: 64bit -> 8byte
            for {
                n, err := reader.Read(buffer)
                if n > 0 {
                    v := int(binary.BigEndian.Uint64(buffer)) // Uint64 -> int
                    out <- v
                }
                if err != nil {
                    break
                }
            }
            close(out)
        }()
        return out
    }
    
    func writerSink(writer io.Writer, in <-chan int) {
        for v := range in {
            buffer := make([]byte, 8)
            binary.BigEndian.PutUint64(buffer, uint64(v))
            writer.Write(buffer)
        }
    }
    func randomSource(count int) <-chan int {
        out := make(chan int)
        go func() {
            for i := 0; i < count; i++ {
                out <- rand.Int()
            }
            close(out)
        }()
        return out
    }
    
    
    

    相关文章

      网友评论

          本文标题:go's io

          本文链接:https://www.haomeiwen.com/subject/xzrflqtx.html