{"id":18840224,"url":"https://github.com/maxim2266/stout","last_synced_at":"2025-04-14T07:30:48.412Z","repository":{"id":57551711,"uuid":"222311967","full_name":"maxim2266/stout","owner":"maxim2266","description":"Package stout (STream OUTput): writing byte streams in a type-safe and extensible way.","archived":true,"fork":false,"pushed_at":"2024-12-06T11:25:20.000Z","size":42,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-14T07:06:42.013Z","etag":null,"topics":["byte-stream","composition","go","golang","golang-package","type-safety"],"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-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/maxim2266.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,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-11-17T21:05:25.000Z","updated_at":"2025-03-15T22:21:51.000Z","dependencies_parsed_at":"2024-06-20T04:42:56.753Z","dependency_job_id":null,"html_url":"https://github.com/maxim2266/stout","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maxim2266%2Fstout","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maxim2266%2Fstout/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maxim2266%2Fstout/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maxim2266%2Fstout/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/maxim2266","download_url":"https://codeload.github.com/maxim2266/stout/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248839280,"owners_count":21169788,"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":["byte-stream","composition","go","golang","golang-package","type-safety"],"created_at":"2024-11-08T02:46:54.742Z","updated_at":"2025-04-14T07:30:48.126Z","avatar_url":"https://github.com/maxim2266.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# stout\n\n[![GoDoc](https://godoc.org/github.com/maxim2266/stout?status.svg)](https://godoc.org/github.com/maxim2266/stout)\n[![Go Report Card](https://goreportcard.com/badge/github.com/maxim2266/stout)](https://goreportcard.com/report/github.com/maxim2266/stout)\n[![License: BSD 3 Clause](https://img.shields.io/badge/License-BSD_3--Clause-yellow.svg)](https://opensource.org/licenses/BSD-3-Clause)\n\nPackage `stout` (STream OUTput): writing byte streams in a type-safe and extensible way.\n\n### Motivation\nIn Go, writing files or other byte streams (pipes, sockets, etc.) is where all that error-checking\nboilerplate code pops up, obscuring program logic and making the source code harder to read.\nThis project is an attempt to improve the situation. The main goals of the project are:\n* Reduce the amount of error-checking boilerplate code;\n* Make writing multi-part byte streams look more like a declarative composition, as much as possible with Go syntax;\n* Develop an API that can be easily extended for any custom type or operation;\n* Provide some generally useful functions for real-life applications.\n\nThe core ideas behind the implementation of the package are described in [this](https://maxim2266.github.io/stout/) blog post.\n\n### Examples\n\n##### \"Hello, user\" application:\n```Go\nfunc main() {\n    _, err := stout.WriterBufferedStream(os.Stdout).Write(\n        stout.String(\"Hello, \"),\n        stout.String(os.Getenv(\"USER\")),\n        stout.String(\"!\\n\"),\n    )\n\n    if err != nil {\n        println(\"ERROR:\", err.Error())\n        os.Exit(1)\n    }\n}\n```\n\n##### Simplified implementation of `cat` command:\n```Go\nfunc main() {\n    files := make([]stout.Chunk, 0, len(os.Args)-1)\n\n    for _, arg := range os.Args[1:] {\n        files = append(files, stout.File(arg))\n    }\n\n    _, err := stout.WriterBufferedStream(os.Stdout).Write(files...)\n\n    if err != nil {\n        println(\"ERROR:\", err.Error())\n        os.Exit(1)\n    }\n}\n```\n\n##### Report generator\nA complete application that generates a simple HTML page from `ps` command output:\n```go\nfunc main() {\n    // run \"ps\" command\n    lines, err := ps()\n\n    if err != nil {\n        die(err.Error())\n    }\n\n    // compose HTML\n    // note: in a real-life application we would be writing to a socket instead of stdout\n    _, err = stout.WriterBufferedStream(os.Stdout).Write(\n        stout.String(hdr),\n        stout.Repeat(rowsFrom(lines)),\n        stout.String(\"\u003c/table\u003e\u003c/body\u003e\u003c/html\u003e\"),\n    )\n\n    if err != nil {\n        die(err.Error())\n    }\n}\n\nfunc rowsFrom(lines [][]byte) func(int, *stout.Writer) (int64, error) {\n    return func(i int, w *stout.Writer) (int64, error) {\n        // iteration stop\n        if i \u003e= len(lines) {\n            return 0, io.EOF\n        }\n\n        // get the i-th record\n        fields := bytes.Fields(lines[i])\n\n        if len(fields) \u003c 4 {\n            return 0, io.EOF\n        }\n\n        // compose and write one row\n        return w.WriteChunks([]stout.Chunk{\n            stout.String(\"\u003ctr\u003e\u003ctd\u003e\"),\n            stout.Join(\"\u003c/td\u003e\u003ctd\u003e\",\n                stout.ByteSlice(fields[0]),                         // pid\n                stout.ByteSlice(fields[1]),                         // %cpu\n                stout.ByteSlice(fields[2]),                         // %mem\n                stout.String(html.EscapeString(string(fields[3]))), // cmd\n            ),\n            stout.String(\"\u003c/td\u003e\u003c/tr\u003e\"),\n        })\n    }\n}\n\nfunc ps() (lines [][]byte, err error) {\n    var buff bytes.Buffer\n\n    // get the output of the command into the buffer\n    _, err = stout.ByteBufferStream(\u0026buff).Write(\n        stout.Command(\"ps\", \"--no-headers\", \"-Ao\", \"pid,%cpu,%mem,cmd\"),\n    )\n\n    if err == nil {\n        lines = bytes.Split(buff.Bytes(), []byte{'\\n'})\n    }\n\n    return\n}\n\nfunc die(msg string) {\n    println(\"error:\", msg)\n    os.Exit(1)\n}\n\nconst hdr = `\u003c!DOCTYPE html\u003e\n\u003chtml\u003e\u003chead\u003e\u003cmeta charset=\"UTF-8\"\u003e\u003c/head\u003e\n\u003cbody\u003e\u003ctable\u003e\n\u003ctr\u003e\u003cth\u003epid\u003c/th\u003e\u003cth\u003ecpu\u003c/th\u003e\u003cth\u003emem\u003c/th\u003e\u003cth\u003ecmd\u003c/th\u003e`\n```\n\n### Status\nTested on Linux Mint 20.2 with Go version 1.16.6.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaxim2266%2Fstout","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmaxim2266%2Fstout","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaxim2266%2Fstout/lists"}