lines

package module
v0.0.0-...-c310428 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 8, 2025 License: BSD-2-Clause Imports: 3 Imported by: 6

README

go-lines

import "github.com/Maki-Daisuke/go-lines"

Package lines makes it a bit easier to read lines from text files in Go.

Description

There are so many ways to read lines in Go! But, I wanted to write less lines of code as possible. I think life is too short to write lines of code to read lines.)

For example, you need to write code like the following to read line from STDIN:

import (
  "bufio"
  "io"
  "os"
)

func main(){
  r := bufio.NewReader(os.Stdin)
  line := ""
  for {
    l, isPrefix, err := r.ReadLine()
    if err == io.EOF {
      break
    } else if err != nil {
      panic(err)
    }
    line += l
    if !isPrefix {
      do_something_with(line)  // this is what really I want to do.
      line = ""
    }
  }
}

With go-lines package and Go 1.23's range over func feature, you can write like this:

import (
  "os"
  "github.com/Maki-Daisuke/go-lines"
)

func main(){
  for line := range lines.Reader(os.Stdin) {
    do_something_with(line)
  }
}

Yay! It's much less lines of code and much cleaner!

If an error occurs during reading, the Reader function will panic. If you want to handle errors, you can use a recover:

import (
  "fmt"
  "os"
  "github.com/Maki-Daisuke/go-lines"
)

func main(){
  defer func() {
    if err := recover(); err != nil {
      // Handle the error
      fmt.Println("Error:", err)
    }
  }()
  
  for line := range lines.Reader(os.Stdin) {
    do_something_with(line)
  }
}

It's still less lines of code, isn't it?

Usage

func Reader
func Reader(r io.Reader) func(yield func(string) bool)

Reader converts a io.Reader to a func that can be used with range to iterate over lines. If an error occurs during reading, the function will panic. With Go 1.23's range over func feature, you can use it like:

for line := range Reader(reader) {
  do_something_with(line)
}
func String
func String(s string) func(yield func(string) bool)

String takes a string and returns a func that can be used with range to iterate over lines in the string. It internally uses a strings.Reader. With Go 1.23's range over func feature, you can use it like:

s := "line1\nline2\nline3"
for line := range String(s) {
  do_something_with(line)
}

License

The Simplified BSD License (2-clause). See LICENSE file also.

Author

Daisuke (yet another) Maki

Documentation

Overview

Package lines makes it a bit easier to read lines from text files in Go.

Description

There are so many ways to read lines in Go! But, I wanted to write less lines of code as possible. I think life is too short to write lines of code to read lines.)

For example, you need to write code like the following to read line from STDIN:

import (
  "bufio"
  "io"
  "os"
)

func main(){
  r := bufio.NewReader(os.Stdin)
  line := ""
  for {
    l, isPrefix, err := r.ReadLine()
    if err == io.EOF {
      break
    } else if err != nil {
      panic(err)
    }
    line += l
    if !isPrefix {
      do_something_with(line)  // this is what really I want to do.
      line = ""
    }
  }
}

With this package and Go 1.23's range over func feature, you can write like this:

import (
  "os"
  . "github.com/Maki-Daisuke/go-lines"
)

func main(){
  for line := range lines.Reader(os.Stdin) {
    do_something_with(line)
  }
}

Yay! It's much less lines of code and much cleaner!

If an error occurs during reading, the Reader function will panic. If you want to handle errors, you can use a recover:

import (
  "fmt"
  "os"
  . "github.com/Maki-Daisuke/go-lines"
)

func main(){
  defer func() {
    if err := recover(); err != nil {
        // Handle the error
    }
  }()

  for line := range lines.Reader(os.Stdin) {
    do_something_with(line)
  }
}

It's still less lines of code, isn't it?

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Reader

func Reader(r io.Reader) func(yield func(string) bool)

`Reader` converts a `io.Reader` to a func that can be used with range to iterate over lines. If an error occurs during reading, the function will panic. With Go 1.23's range over func feature, you can use it like:

for line := range lines.Reader(reader) {
  do_something_with(line)
}

func String

func String(s string) func(yield func(string) bool)

`String` takes a string and returns a func that can be used with range to iterate over lines in the string. It internally uses a strings.Reader. With Go 1.23's range over func feature, you can use it like:

for line := range lines.String(s) {
  do_something_with(line)
}

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL