Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count lines via bufio

Tags:

go

I'm utilizing bufio to do a for loop for each line in a text file. I have no idea how to count the amount of lines though.

scanner := bufio.NewScanner(bufio.NewReader(file))

The above is what I use to scan my file.

like image 754
Mike Avatar asked May 28 '26 16:05

Mike


1 Answers

You could do something like this:

counter := 0
for scanner.Scan() {
    line := scanner.Text()
    counter++
    // do something with your line
}
fmt.Printf("Lines read: %d", counter)
like image 90
IamNaN Avatar answered Jun 01 '26 13:06

IamNaN



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!