{"id":13646887,"url":"https://github.com/Maki-Daisuke/go-lines","last_synced_at":"2025-04-21T21:31:37.781Z","repository":{"id":136977366,"uuid":"46259744","full_name":"Maki-Daisuke/go-lines","owner":"Maki-Daisuke","description":"Makes it a bit easier to read lines from text files in Go","archived":false,"fork":false,"pushed_at":"2025-03-08T02:34:33.000Z","size":7,"stargazers_count":8,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-14T03:57:10.062Z","etag":null,"topics":["cui","go","golang"],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-2-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Maki-Daisuke.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2015-11-16T07:48:51.000Z","updated_at":"2025-03-08T02:34:36.000Z","dependencies_parsed_at":"2023-03-22T12:17:34.740Z","dependency_job_id":null,"html_url":"https://github.com/Maki-Daisuke/go-lines","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Maki-Daisuke%2Fgo-lines","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Maki-Daisuke%2Fgo-lines/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Maki-Daisuke%2Fgo-lines/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Maki-Daisuke%2Fgo-lines/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Maki-Daisuke","download_url":"https://codeload.github.com/Maki-Daisuke/go-lines/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250136749,"owners_count":21380885,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cui","go","golang"],"created_at":"2024-08-02T01:03:13.353Z","updated_at":"2025-04-21T21:31:37.770Z","avatar_url":"https://github.com/Maki-Daisuke.png","language":"Go","funding_links":[],"categories":["Go"],"sub_categories":[],"readme":"# go-lines\n\n    import \"github.com/Maki-Daisuke/go-lines\"\n\nPackage lines makes it a bit easier to read lines from text files in Go.\n\n\n### Description\n\nThere are so many ways to read lines in Go! But, I wanted to write less lines of\ncode as possible. I think life is too short to write lines of code to read\nlines.)\n\nFor example, you need to write code like the following to read line from STDIN:\n\n```go\nimport (\n  \"bufio\"\n  \"io\"\n  \"os\"\n)\n\nfunc main(){\n  r := bufio.NewReader(os.Stdin)\n  line := \"\"\n  for {\n    l, isPrefix, err := r.ReadLine()\n    if err == io.EOF {\n      break\n    } else if err != nil {\n      panic(err)\n    }\n    line += l\n    if !isPrefix {\n      do_something_with(line)  // this is what really I want to do.\n      line = \"\"\n    }\n  }\n}\n```\n\nWith go-lines package and Go 1.23's range over func feature, you can write like this:\n\n```go\nimport (\n  \"os\"\n  \"github.com/Maki-Daisuke/go-lines\"\n)\n\nfunc main(){\n  for line := range lines.Reader(os.Stdin) {\n    do_something_with(line)\n  }\n}\n```\n\nYay! It's much less lines of code and much cleaner!\n\nIf an error occurs during reading, the Reader function will panic.\nIf you want to handle errors, you can use a recover:\n\n```go\nimport (\n  \"fmt\"\n  \"os\"\n  \"github.com/Maki-Daisuke/go-lines\"\n)\n\nfunc main(){\n  defer func() {\n    if err := recover(); err != nil {\n      // Handle the error\n      fmt.Println(\"Error:\", err)\n    }\n  }()\n  \n  for line := range lines.Reader(os.Stdin) {\n    do_something_with(line)\n  }\n}\n```\n\nIt's still less lines of code, isn't it?\n\n\n## Usage\n\n#### func Reader\n\n```go\nfunc Reader(r io.Reader) func(yield func(string) bool)\n```\n`Reader` converts a `io.Reader` to a func that can be used with range to iterate over lines. \nIf an error occurs during reading, the function will panic. With Go 1.23's range over func \nfeature, you can use it like:\n\n```go\nfor line := range Reader(reader) {\n  do_something_with(line)\n}\n```\n\n#### func String\n\n```go\nfunc String(s string) func(yield func(string) bool)\n```\n`String` takes a string and returns a func that can be used with range to iterate over \nlines in the string. It internally uses a strings.Reader. With Go 1.23's range over func \nfeature, you can use it like:\n\n```go\ns := \"line1\\nline2\\nline3\"\nfor line := range String(s) {\n  do_something_with(line)\n}\n```\n\n\n## License\n\nThe Simplified BSD License (2-clause).\nSee [LICENSE](LICENSE) file also.\n\n\n## Author\n\nDaisuke (yet another) Maki\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FMaki-Daisuke%2Fgo-lines","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FMaki-Daisuke%2Fgo-lines","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FMaki-Daisuke%2Fgo-lines/lists"}