📌  相关文章
📜  golang bufio - Go 编程语言 - Go 编程语言代码示例

📅  最后修改于: 2022-03-11 14:44:59.817000             🧑  作者: Mango

代码示例1
package main

import (
    "bufio"
    "fmt"
    "log"
    "os"
)

func main() {
    file, err := os.Open("/path/to/file.txt")
    if err != nil {
        log.Fatal(err)
    }
    defer file.Close()

    scanner := bufio.NewScanner(file)
  
      //! Inrease the buffer capacity if necessary
    const maxCapacity = 10_000 * 1024 // 20GB == 20_000*1024
    buf := make([]byte, maxCapacity)
    scanner.Buffer(buf, maxCapacity)
  
    // optionally, resize scanner's capacity for lines over 64K, see next example
    for scanner.Scan() {
        fmt.Println(scanner.Text())
    }

    if err := scanner.Err(); err != nil {
        log.Fatal(err)
    }
}