line by line Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/line-by-line/ Tue, 12 Nov 2019 19:38:17 +0000 en-US hourly 1 https://wordpress.org/?v=6.8.1 https://i0.wp.com/golangbyexamples.com/wp-content/uploads/2021/05/cropped-go_border-1.png?fit=32%2C32&ssl=1 line by line Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/line-by-line/ 32 32 159787465 Read a large file Line by Line in Go (Golang) https://vikasboss.github.io/read-large-file-line-by-line-go/ https://vikasboss.github.io/read-large-file-line-by-line-go/#comments Sat, 19 Oct 2019 03:56:42 +0000 https://vikasboss.github.io/?p=288 When it comes to reading large files, obviously we don’t want to load the entire file in memory. bufio package in golang comes to the rescue when reading large files. Let’s say...

The post Read a large file Line by Line in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
When it comes to reading large files, obviously we don’t want to load the entire file in memory. bufio package in golang comes to the rescue when reading large files. Let’s say we have a sample.txt file with below contents

This is an example
to show how
to read file 
line by line.

Here is the program:

package main
import (
    "bufio"
    "fmt"
    "log"
    "os"
)
func main(){
    LinebyLineScan()
}
func LinebyLineScan() {
    file, err := os.Open("./sample.txt")
    if err != nil {
        log.Fatal(err)
    }
    defer file.Close()
    scanner := bufio.NewScanner(file)
    for scanner.Scan() {
        fmt.Println(scanner.Text())
    }
    if err := scanner.Err(); err != nil {
        log.Fatal(err)
    }
}

Output:

This is an example
to show how 
to read file 
line by line.

Note however bufio.Scanner has max buffer size 64*1024 bytes which means in case you file has any line greater than the size of 64*1024, then it will give the error

bufio.Scanner: token too long

The post Read a large file Line by Line in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/read-large-file-line-by-line-go/feed/ 1 288