我有以下代碼:
imageFile, _ := os.Open("image.png") // returns *os.File, error
decodedImage, _, _ := image.Decode(imageFile) // returns image.Image, string, error
// imageFile has been modified!
當我imageFile在呼叫image.Decode它之后嘗試使用它時不再表現相同,這使我相信以某種方式進行了image.Decode修改imageFile。
為什么在image.Decode修改原始值的同時回傳一個新值decodedImage- 這不是誤導嗎?
如何保留原始值?有沒有辦法制作檔案的“真實”副本,指向已分配記憶體的新部分?
我剛開始使用 Go,如果我在這里遺漏了一些明顯的東西,我深表歉意。
uj5u.com熱心網友回復:
問題中的代碼不會修改磁盤上的檔案。
imageFile影像解碼后的當前位置在檔案開頭的某個位置。再次讀取呸,尋求回檔案的開頭:
imageFile, err := os.Open("image.png")
if err != nil { log.Fatal(err) }
decodedImage, _, err := image.Decode(imageFile)
if err != nil { log.Fatal(err) }
// Rewind back to the start of the file.
_, err := imageFile.Seek(0, io.SeekStart)
if err != nil { log.Fatal(err) }
// Do something with imageFile here.
將log.Fatal錯誤處理替換為適合您的應用程式的任何內容。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/349705.html
上一篇:While回圈功能
下一篇:附加在c(指標的指標)上
