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

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

代码示例1
package main

import (
    "bufio"
    // "fmt"
    "log"
    "os"

    c "github.com/gookit/color"

    "github.com/tidwall/gjson"
)

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

    scanner := bufio.NewScanner(file)

    //! Inrease the buffer capacity if necessary
    const maxCapacity = 100 * 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() {

        line := scanner.Text()
        g := gjson.Parse(line)
        for _, r := range g.Array() {
            if r.Get("ev").String() == "AM" {
                c.Greenln(" ")

            }
        }
    }

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