github.com/KEINOS/go-countline@v1.1.0/cl/_alt/alt4.go (about)

     1  //nolint:revive,stylecheck
     2  package _alt
     3  
     4  import (
     5  	"bufio"
     6  	"io"
     7  
     8  	"github.com/pkg/errors"
     9  )
    10  
    11  // CountLinesAlt4 uses atomic and goroutines to count the number of lines.
    12  func CountLinesAlt4(inputReader io.Reader) (int, error) {
    13  	if inputReader == nil {
    14  		return 0, errors.New("given reader is nil")
    15  	}
    16  
    17  	bufReader := bufio.NewReader(inputReader)
    18  	count := 0
    19  
    20  	for {
    21  		_, isPrefix, err := bufReader.ReadLine()
    22  		if err != nil {
    23  			if err == io.EOF {
    24  				break
    25  			}
    26  
    27  			return 0, errors.Wrap(err, "failed to read from reader")
    28  		}
    29  
    30  		if isPrefix {
    31  			continue
    32  		}
    33  
    34  		count++
    35  	}
    36  
    37  	return count, nil
    38  }