首頁 > 後端開發 > Golang > 主體

為什麼延遲 gzip.NewWriter 關閉會導致 Go 中的資料遺失?

DDD
發布: 2024-10-25 22:47:29
原創
703 人瀏覽過

Why Does Deferring gzip.NewWriter Closure Cause Data Loss in Go?

延遲關閉會導致gzip Writer中的資料遺失

在Go中使用gzip.NewWriter來壓縮位元組片時,通常會觀察到:延遲寫入器的關閉會導致資料遺失。在讀取壓縮資料時,這種現象變得很明顯,因為它會因意外的檔案結束 (EOF) 錯誤而提前終止。

要理解這一點,讓我們檢查提供的程式碼片段:

<code class="go">func zipData(originData []byte) ([]byte, error) {
    // Create a buffer to store the compressed data
    var bf bytes.Buffer
    
    // Initialize the gzip writer with the buffer
    gw := gzip.NewWriter(&bf)
    
    // Defer closure of the writer
    defer gw.Close()
    
    // Write the original data to the writer
    _, err := gw.Write(originData)
    if err != nil {
        return nil, err
    }
    
    // Flush the writer to write any buffered data
    if err = gw.Flush(); err != nil {
        return nil, err
    }
    
    // Return the compressed data
    return bf.Bytes(), nil
}</code>
登入後複製

這個問題源自於使用 defer 關閉 gzip writer (gw)。根據Close() 的文件:

「Close 透過將任何未寫入的資料刷新到底層io.Writer 並寫入GZIP 頁腳來關閉Writer。」

在這種情況下,延遲的閉包在函數傳回儲存在緩衝區(bf)中的壓縮資料後執行。但是,在呼叫 Close() 方法之前不會寫入頁腳,導致返回時壓縮資料不完整。

可以透過在返回壓縮資料之前手動關閉writer 來解決此問題:

<code class="go">func zipData(originData []byte) ([]byte, error) {
    // ... (same code as before) ...
    
    // Close the writer to write the footer
    if err := gw.Close(); err != nil {
        return nil, err
    }
    
    // Return the compressed data
    return bf.Bytes(), nil
}</code>
登入後複製

透過在返回之前關閉writer,GZIP頁腳寫入成功,並且可以讀取壓縮資料而不會遇到EOF錯誤。

以上是為什麼延遲 gzip.NewWriter 關閉會導致 Go 中的資料遺失?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!