Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Clavin June
Clavin June

Posted on • Originally published atclavinjune.dev on

     

Golang Stream All Lines From Stdin

Sunday Snippet #13 golang stream all lines from stdin

write.sh

#! /bin/shset-euo pipefailforiin{1..100};doecho"$i"sleep1done
Enter fullscreen modeExit fullscreen mode

main.go

packagemainimport("bufio""encoding/base64""log""os")funcmain(){l:=log.New(os.Stdout,"[STREAM] ",log.Lshortfile|log.LstdFlags|log.Lmsgprefix)s:=bufio.NewScanner(os.Stdin)fors.Scan(){t:=s.Text()b:=s.Bytes()// for example process the input to b64b64:=base64.StdEncoding.EncodeToString(b)l.Println(t,"=>",b64)}}
Enter fullscreen modeExit fullscreen mode

Usage

sh write.sh | go run main.go2022/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 modeExit fullscreen mode

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

a bunch of perceptrons
  • Location
    Jakarta, Indonesia
  • Joined

More fromClavin June

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp