###### base64-encode.go ```go package main import ( "bufio" "encoding/base64" "os" ) func main() { // Buffered input that splits input on lines. input := bufio.NewScanner(os.Stdin) // Base64 Encoder/writer. encoder := base64.NewEncoder( base64.StdEncoding, os.Stdout) // Scan until EOF (no more input). for input.Scan() { bytes := input.Bytes() _, _ = encoder.Write(bytes) _, _ = encoder.Write([]byte{'\n'}) } // Close the encoder and ensure it flushes remaining output _ = encoder.Close() } ``` This example reads a line at a time from `stdin`, base64 encodes it, and writes it back out to `stdout`. Since the scanner splits on newline characters (`\n`) without returning them, it's necessary to explicitly write a newline after writing each line. ``` $ cat base64-encode.go | go run base64-encode.go cGFja2FnZSBtYWluCgppbXBvcnQgKAoJImJ1ZmlvIgoJImVuY29kaW5nL2Jhc2U2NCIKCSJvcyIKKQoKZnVuYyBtYWluKCkgewoKCS8vIEJ1ZmZlcmVkIGlucHV0IHRoYXQgc3BsaXRzIGlucHV0IG9uIGxpbmVzLgoJaW5wdXQgOj0gYnVmaW8uTmV3U2Nhbm5lcihvcy5TdGRpbikKCgkvLyBCYXNlNjQgRW5jb2Rlci93cml0ZXIuCgllbmNvZGVyIDo9IGJhc2U2NC5OZXdFbmNvZGVyKAoJCWJhc2U2NC5TdGRFbmNvZGluZywKCQlvcy5TdGRvdXQpCgoJLy8gU2NhbiB1bnRpbCBFT0YgKG5vIG1vcmUgaW5wdXQpLgoJZm9yIGlucHV0LlNjYW4oKSB7CgkJYnl0ZXMgOj0gaW5wdXQuQnl0ZXMoKQoJCV8sIF8gPSBlbmNvZGVyLldyaXRlKGJ5dGVzKQoJCV8sIF8gPSBlbmNvZGVyLldyaXRlKFtdYnl0ZXsnXG4nfSkKCX0KCgkvLyBDbG9zZSB0aGUgZW5jb2RlciBhbmQgZW5zdXJlIGl0IGZsdXNoZXMgcmVtYWluaW5nIG91dHB1dAoJXyA9IGVuY29kZXIuQ2xvc2UoKQp9Cg== ``` You can confirm the text was correctly encoded by piping the encoded result to the system `base64` command to decode it: ``` $ cat base64-encode.go | go run base64-encode.go | base64 -D package main import ( "bufio" "encoding/base64" "os" ) func main() { // Buffered input that splits input on lines. input := bufio.NewScanner(os.Stdin) // Base64 Encoder/writer. encoder := base64.NewEncoder( base64.StdEncoding, os.Stdout) // Scan until EOF (no more input). for input.Scan() { bytes := input.Bytes() _, _ = encoder.Write(bytes) _, _ = encoder.Write([]byte{'\n'}) } // Close the encoder and ensure it flushes remaining output _ = encoder.Close() } ``` --- Next: [[11. Spawn Process]]