Reading Files {.en}

阅读文件 {.zh}

::: {.en} Reading and writing files are basic tasks needed for many Go programs. First we’ll look at some examples of reading files. :::

::: {.zh}

读取和写入文件是许多Go程序所需的基本任务。首先,我们将看一些阅读文件的例子。

:::

  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "io"
  6. "io/ioutil"
  7. "os"
  8. )

::: {.en} Reading files requires checking most calls for errors. This helper will streamline our error checks below. :::

::: {.zh}

读取文件需要检查大多数错误调用。这个帮助程序将简化我们的错误检查。

:::

  1. func check(e error) {
  2. if e != nil {
  3. panic(e)
  4. }
  5. }
  6. func main() {

::: {.en} Perhaps the most basic file reading task is slurping a file’s entire contents into memory. :::

::: {.zh}

也许最基本的文件读取任务是将文件的全部内容发送到内存中。

:::

  1. dat, err := ioutil.ReadFile("/tmp/dat")
  2. check(err)
  3. fmt.Print(string(dat))

::: {.en} You’ll often want more control over how and what parts of a file are read. For these tasks, start by Opening a file to obtain an os.File value. :::

::: {.zh}

您经常需要更多地控制文件的读取方式和部分内容。对于这些任务,从打开'文件开始获取os.File`值。

:::

  1. f, err := os.Open("/tmp/dat")
  2. check(err)

::: {.en} Read some bytes from the beginning of the file. Allow up to 5 to be read but also note how many actually were read. :::

::: {.zh}

从文件的开头读取一些字节。最多可读取5个字节,但也要注意读取的实际数量。

:::

  1. b1 := make([]byte, 5)
  2. n1, err := f.Read(b1)
  3. check(err)
  4. fmt.Printf("%d bytes: %sn", n1, string(b1[:n1]))

::: {.en} You can also Seek to a known location in the file and Read from there. :::

::: {.zh}

您还可以“查找”文件中的已知位置和“读取”。

:::

  1. o2, err := f.Seek(6, 0)
  2. check(err)
  3. b2 := make([]byte, 2)
  4. n2, err := f.Read(b2)
  5. check(err)
  6. fmt.Printf("%d bytes @ %d: ", n2, o2)
  7. fmt.Printf("%vn", string(b2[:n2]))

::: {.en} The io package provides some functions that may be helpful for file reading. For example, reads like the ones above can be more robustly implemented with ReadAtLeast. :::

::: {.zh}

io包提供了一些可能有助于文件读取的功能。例如,使用ReadAtLeast可以更加稳健地实现上述读取。

:::

  1. o3, err := f.Seek(6, 0)
  2. check(err)
  3. b3 := make([]byte, 2)
  4. n3, err := io.ReadAtLeast(f, b3, 2)
  5. check(err)
  6. fmt.Printf("%d bytes @ %d: %sn", n3, o3, string(b3))

::: {.en} There is no built-in rewind, but Seek(0, 0) accomplishes this. :::

::: {.zh}

没有内置的倒带,但是Seek(0,0)完成了这个。

:::

  1. _, err = f.Seek(0, 0)
  2. check(err)

::: {.en} The bufio package implements a buffered reader that may be useful both for its efficiency with many small reads and because of the additional reading methods it provides. :::

::: {.zh}

bufio包实现了一个bufferedreader,它既可以用于许多小读取的效率,也可以用于它提供的附加读取方法。

:::

  1. r4 := bufio.NewReader(f)
  2. b4, err := r4.Peek(5)
  3. check(err)
  4. fmt.Printf("5 bytes: %sn", string(b4))

::: {.en} Close the file when you’re done (usually this would be scheduled immediately after Opening with defer). :::

::: {.zh}

完成后关闭文件(通常这将在使用defer`打开后立即安排)。

:::

  1. f.Close()
  2. }
  1. $ echo "hello" > /tmp/dat
  2. $ echo "go" >> /tmp/dat
  3. $ go run reading-files.go
  4. hello
  5. go
  6. 5 bytes: hello
  7. 2 bytes @ 6: go
  8. 2 bytes @ 6: go
  9. 5 bytes: hello

::: {.en} Next we’ll look at writing files. :::

::: {.zh}

接下来我们将看看编写文件。

:::