Golang Stream All Lines From Stdin

sunday-snippet · Apr 17, 2022 · ~1 min
Sunday Snippet #13 golang stream all lines from stdin
Sunday Snippet #13 golang stream all lines from stdin

write.sh

1
2
3
4
5
6
7
8
#! /bin/sh

set -euo pipefail

for i in {1..100}; do
    echo "$i"
    sleep 1
done

main.go

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
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)
	}
}

Usage

1
2
3
4
5
6
7
8
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
· · ·

Love This Content?

Any kind of supports is greatly appreciated! Kindly support me via Bitcoin, Ko-fi, Trakteer, or just continue to read another content. You can write a response via Webmention and let me know the URL via Telegraph.

Drop Your Comment Below