<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                帶有緩沖的讀寫拷貝IO === 帶有緩存讀取文件 ~~~ package main import ( "bufio" "fmt" "io" "os" ) func main() { // 獲得文件句柄 file, e := os.OpenFile("./test.file", os.O_RDONLY, 0666) if e != nil { panic(e.Error()) } // 創建讀取器 reader := bufio.NewReader(file) // 創建緩沖區 bytes := make([]byte, 1024) // 1024為1K for { n, e := reader.Read(bytes) fmt.Printf("%s",bytes[:n]) if e == io.EOF{ break } } } ~~~ # 注意這里只說一次 ``` n, e := reader.Read(bytes) fmt.Printf("%s",bytes[:n]) bytes可能沒有裝滿 就這樣才能不然會出bug ``` 帶有緩沖寫入 ~~~ func write() { // 獲取文件句柄 (O_CREATE) 不存在就創建 (O_WRONLY) 寫入 (O_APPEND) 追加 file, e := os.OpenFile("./new.text", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 00666) defer file.Close() if e != nil { panic(e.Error()) } // 創建寫出器 writer := bufio.NewWriter(file) _, e = writer.Write([]byte("hello")) if e != nil { fmt.Printf(e.Error()) } writer.Flush() //倒干緩沖區 } ~~~ ### copy 同步 ~~~ func copy() { old, e := os.OpenFile("./test.mp4", os.O_RDONLY, 00666) new, e := os.OpenFile("./new.mp4", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 00666) if e != nil { panic(e.Error()) } reader := bufio.NewReader(old) writer := bufio.NewWriter(new) bytes := make([]byte, 1024) for { _, e := reader.Read(bytes) if e == io.EOF { break }else{ writer.Write(bytes) } } defer func() { writer.Flush() old.Close() new.Close() }() } ~~~ 共同爭搶資源有可能是亂序不推薦(這個根本不能用,亂序的!!!) ~~~ /** * Created by GoLand * User: dollarkiller * Date: 19-6-10 * Time: 下午9:20 * */ package main import ( "bufio" "context" "io" "os" "sync" ) func main() { ch := make(chan []byte,1024) ctx, cancel := context.WithCancel(context.TODO()) var wg sync.WaitGroup wg.Add(2) go read(ch,cancel,&wg) go write(ctx,ch,&wg) wg.Wait() //time.Sleep(time.Second * 3) } func read(ch chan []byte,cancelFunc context.CancelFunc,wg *sync.WaitGroup) { file, e := os.OpenFile("./test.file", os.O_RDONLY, 00666) defer func() { file.Close() }() if e != nil { panic(e.Error()) } reader := bufio.NewReader(file) bytes := make([]byte, 1024) for { _, e := reader.Read(bytes) ch <- bytes if e == io.EOF { cancelFunc() break } } wg.Done() } func write(ctx context.Context,ch chan []byte,wg *sync.WaitGroup) { file, e := os.OpenFile("./new.file", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 00666) defer func() { file.Close() }() if e != nil { panic(e.Error()) } writer := bufio.NewWriter(file) forloop: for { select { case <-ch: writer.Write(<-ch) case <-ctx.Done(): break forloop } } writer.Flush() wg.Done() } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看