DEV Community

Clavin June
Clavin June

Posted on • Originally published at clavinjune.dev on

3 1

Golang Stream All Lines From Stdin

Sunday Snippet #13 golang stream all lines from stdin

write.sh

#! /bin/sh

set -euo pipefail

for i in {1..100}; do
    echo "$i"
    sleep 1
done
Enter fullscreen mode Exit fullscreen mode

main.go

package main

import (
    "bufio"
    "encoding/base64"
    "log"
    "os"
)

func main() {
    l := log.New(os.Stdout, "[STREAM] ", log.Lshortfile|log.LstdFlags|log.Lmsgprefix)
    s := bufio.NewScanner(os.Stdin)

    for s.Scan() {
        t := s.Text()
        b := s.Bytes()

        // for example process the input to b64
        b64 := base64.StdEncoding.EncodeToString(b)
        l.Println(t, "=>", b64)
    }
}
Enter fullscreen mode Exit fullscreen mode

Usage

sh write.sh | go run main.go
2022/04/10 20:55:25 main.go:20: [STREAM] 1 => MQ==
2022/04/10 20:55:26 main.go:20: [STREAM] 2 => Mg==
2022/04/10 20:55:27 main.go:20: [STREAM] 3 => Mw==
2022/04/10 20:55:28 main.go:20: [STREAM] 4 => NA==
2022/04/10 20:55:29 main.go:20: [STREAM] 5 => NQ==
2022/04/10 20:55:30 main.go:20: [STREAM] 6 => Ng==
^Csignal: interrupt
Enter fullscreen mode Exit fullscreen mode

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay