github.com/KEINOS/go-countline@v1.1.0/README.md (about) 1 <!-- markdownlint-disable MD001 MD041 MD050 --> 2 [](https://github.com/KEINOS/go-countline/blob/main/.github/workflows/version-tests.yaml "Supported versions") 3 [](https://pkg.go.dev/github.com/KEINOS/go-countline#section-documentation "Read generated documentation of the app") 4 5 # go-countline 6 7 Go package "[go-countline](https://github.com/KEINOS/go-countline/cl)" does nothing more than **count the number of lines in a file**, but it tries to count as fast as possible. 8 9 > __Note__: Unlike the "`wc -l`" command, this package counts the last line that does not end in line breaks/line feeds (see the example below). 10 11 ## Usage 12 13 ```go 14 go get "github.com/KEINOS/go-countline" 15 ``` 16 17 ```go 18 import "github.com/KEINOS/go-countline/cl" 19 20 func ExampleCountLines() { 21 for _, sample := range []struct { 22 Input string 23 }{ 24 {""}, // --> 0 25 {"Hello"}, // --> 1 26 {"Hello\n"}, // --> 1 27 {"\n"}, // --> 1 28 {"\n\n"}, // --> 2 29 {"\nHello"}, // --> 2 30 {"\nHello\n"}, // --> 2 31 {"\n\nHello"}, // --> 3 32 {"\n\nHello\n"}, // --> 3 33 } { 34 readerFile := strings.NewReader(sample.Input) 35 36 count, err := cl.CountLines(readerFile) 37 if err != nil { 38 log.Fatal(err) 39 } 40 41 fmt.Printf("%#v --> %v\n", sample.Input, count) 42 } 43 // Output: 44 // "" --> 0 45 // "Hello" --> 1 46 // "Hello\n" --> 1 47 // "\n" --> 1 48 // "\n\n" --> 2 49 // "\nHello" --> 2 50 // "\nHello\n" --> 2 51 // "\n\nHello" --> 3 52 // "\n\nHello\n" --> 3 53 } 54 ``` 55 56 ## Benchmark Status 57 58 Benchmark of counting 1 GiB of file size (72,323,529 lines) on MacBook Pro (Retina, 13-inch, Early 2015, 2.7 GHz Intel Core i5, 4 core). 59 60 ```shellsession 61 $ go test -benchmem -count 10 -run=^$ -bench BenchmarkCountLines ./... > bench.txt && benchstat bench.txt 62 name time/op 63 CountLines-4 0.39ns ±19% 64 65 name alloc/op 66 CountLines-4 1.00B ± 0% 67 68 name allocs/op 69 CountLines-4 0.00 70 ``` 71 72 ```shellsession 73 $ cat bench.txt 74 goos: darwin 75 goarch: amd64 76 pkg: github.com/KEINOS/go-countline/cl 77 cpu: Intel(R) Core(TM) i5-5257U CPU @ 2.70GHz 78 BenchmarkCountLines-4 1000000000 0.4294 ns/op 1 B/op 0 allocs/op 79 BenchmarkCountLines-4 1000000000 0.4659 ns/op 1 B/op 0 allocs/op 80 BenchmarkCountLines-4 1000000000 0.3811 ns/op 1 B/op 0 allocs/op 81 BenchmarkCountLines-4 1000000000 0.3696 ns/op 1 B/op 0 allocs/op 82 BenchmarkCountLines-4 1000000000 0.3672 ns/op 1 B/op 0 allocs/op 83 BenchmarkCountLines-4 1000000000 0.3888 ns/op 1 B/op 0 allocs/op 84 BenchmarkCountLines-4 1000000000 0.4071 ns/op 1 B/op 0 allocs/op 85 BenchmarkCountLines-4 1000000000 0.3875 ns/op 1 B/op 0 allocs/op 86 BenchmarkCountLines-4 1000000000 0.3604 ns/op 1 B/op 0 allocs/op 87 BenchmarkCountLines-4 1000000000 0.3613 ns/op 1 B/op 0 allocs/op 88 PASS 89 ok github.com/KEINOS/go-countline/cl 85.368s 90 PASS 91 ok github.com/KEINOS/go-countline/cl/spec 0.275s 92 ``` 93 94 - [See other alternative implementations](./cl/_alt) 95 96 ## Contributing 97 98 ### Statuses 99 100 [](https://github.com/KEINOS/go-countline/actions/workflows/version-tests.yaml) 101 [](https://github.com/KEINOS/go-countline/actions/workflows/platform-test.yaml) 102 [](https://github.com/KEINOS/go-countline/actions/workflows/golangci-lint.yaml) 103 104 [](https://codecov.io/gh/KEINOS/go-countline) 105 [](https://goreportcard.com/report/github.com/KEINOS/go-countline) 106 [](https://github.com/KEINOS/go-countline/actions/workflows/codeQL-analysis.yaml) 107 108 ### Contribute 109 110 **If you have found a faster way** to count the number of lines in a file, feel free to contribute! 111 112 As long as the new function passes the test, it is merged. It then will be replaced to the main fucntion in the next release after the review by the contributors. 113 114 - [Issues](https://github.com/KEINOS/go-countline/issues): [](https://github.com/KEINOS/go-countline/issues) 115 - Please provide a reproducible code snippet. 116 - Pull requests: [](https://github.com/KEINOS/go-countline/pulls) 117 - Branch: `main` 118 - **Any pull requests for the better is welcome!**