An open API service indexing awesome lists of open source software.

https://github.com/maxim2266/stout

Package stout (STream OUTput): writing byte streams in a type-safe and extensible way.
https://github.com/maxim2266/stout

byte-stream composition go golang golang-package type-safety

Last synced: about 1 year ago
JSON representation

Package stout (STream OUTput): writing byte streams in a type-safe and extensible way.

Awesome Lists containing this project

README

          

# stout

[![GoDoc](https://godoc.org/github.com/maxim2266/stout?status.svg)](https://godoc.org/github.com/maxim2266/stout)
[![Go Report Card](https://goreportcard.com/badge/github.com/maxim2266/stout)](https://goreportcard.com/report/github.com/maxim2266/stout)
[![License: BSD 3 Clause](https://img.shields.io/badge/License-BSD_3--Clause-yellow.svg)](https://opensource.org/licenses/BSD-3-Clause)

Package `stout` (STream OUTput): writing byte streams in a type-safe and extensible way.

### Motivation
In Go, writing files or other byte streams (pipes, sockets, etc.) is where all that error-checking
boilerplate code pops up, obscuring program logic and making the source code harder to read.
This project is an attempt to improve the situation. The main goals of the project are:
* Reduce the amount of error-checking boilerplate code;
* Make writing multi-part byte streams look more like a declarative composition, as much as possible with Go syntax;
* Develop an API that can be easily extended for any custom type or operation;
* Provide some generally useful functions for real-life applications.

The core ideas behind the implementation of the package are described in [this](https://maxim2266.github.io/stout/) blog post.

### Examples

##### "Hello, user" application:
```Go
func main() {
_, err := stout.WriterBufferedStream(os.Stdout).Write(
stout.String("Hello, "),
stout.String(os.Getenv("USER")),
stout.String("!\n"),
)

if err != nil {
println("ERROR:", err.Error())
os.Exit(1)
}
}
```

##### Simplified implementation of `cat` command:
```Go
func main() {
files := make([]stout.Chunk, 0, len(os.Args)-1)

for _, arg := range os.Args[1:] {
files = append(files, stout.File(arg))
}

_, err := stout.WriterBufferedStream(os.Stdout).Write(files...)

if err != nil {
println("ERROR:", err.Error())
os.Exit(1)
}
}
```

##### Report generator
A complete application that generates a simple HTML page from `ps` command output:
```go
func main() {
// run "ps" command
lines, err := ps()

if err != nil {
die(err.Error())
}

// compose HTML
// note: in a real-life application we would be writing to a socket instead of stdout
_, err = stout.WriterBufferedStream(os.Stdout).Write(
stout.String(hdr),
stout.Repeat(rowsFrom(lines)),
stout.String("
pidcpumemcmd`
```

### Status
Tested on Linux Mint 20.2 with Go version 1.16.6.