ioutil.TempFile
创建一个临时文件并打开该文件进行读写并返回结果*os.File
(文件描述符)。因此,当您在文件内写入时,指针将移动到该偏移量,即当前位于文件末尾。
但是当您的要求是从文件中读取时,您需要Seek
回到开头或任何需要的偏移量使用*os.File.Seek
方法。所以,添加tmpFile.Seek(0, 0)
会给你想要的行为。
另外,作为一个好的做法,不要忘记关闭文件。注意我用过defer tmpFile.Close()
它在退出之前关闭文件。
请参考以下示例:
package main
import (
"bufio"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
)
func main() {
tmpFile, err := ioutil.TempFile("", fmt.Sprintf("%s-", filepath.Base(os.Args[0])))
if err != nil {
log.Fatal("Could not create temporary file", err)
}
defer tmpFile.Close()
fmt.Println("Created temp file: ", tmpFile.Name())
fmt.Println("Writing some data to the temp file")
if _, err = tmpFile.WriteString("test data"); err != nil {
log.Fatal("Unable to write to temporary file", err)
} else {
fmt.Println("Data should have been written")
}
fmt.Println("Trying to read the temp file now")
// Seek the pointer to the beginning
tmpFile.Seek(0, 0)
s := bufio.NewScanner(tmpFile)
for s.Scan() {
fmt.Println(s.Text())
}
if err = s.Err(); err != nil {
log.Fatal("error reading temp file", err)
}
}
更新:
来自OP的评论:
鉴于删除实际文件也被推迟,是否需要推迟关闭?如果是这样,我想推迟的顺序很重要。
所以,这是一个很好的问题。因此基本的经验法则是关闭文件然后删除。因此,甚至可以先删除然后再关闭它,但这取决于操作系统。
如果您参考C++ 的文档:
如果文件当前由当前进程或另一个进程打开,则此函数的行为是实现定义的(特别是,POSIX 系统取消链接文件名,尽管文件系统空间不会被回收,即使这是到该文件的最后一个硬链接)文件直到最后一个运行的进程关闭该文件为止,Windows不允许删除该文件)
因此,在 Windows 上,如果您尝试先删除它而不关闭它,那么这肯定是一个问题。
So, as defer
是堆叠的,所以执行顺序是
defer os.Remove(tmpFile.Name()) // Called 2nd
defer tmpFile.Close() // Called 1st