-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
41 lines (37 loc) · 1.14 KB
/
main.go
File metadata and controls
41 lines (37 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// A line filter is a common type of program that reads input on stdin,
// processes it, and then prints some derived result to stdout.
// grep and sed are common line filters.
//
// Here's an example line filter in that writes a
// capitalized version of all input text.
package main
import (
"solod.dev/so/bufio"
"solod.dev/so/fmt"
"solod.dev/so/mem"
"solod.dev/so/os"
"solod.dev/so/strings"
)
func main() {
// Wrapping the unbuffered os.Stdin with a buffered
// scanner gives us a convenient Scan method that
// advances the scanner to the next token; which is
// the next line in the default scanner.
scanner := bufio.NewScanner(mem.System, os.Stdin)
defer scanner.Free()
for scanner.Scan() {
// Text returns the current token, here the next line,
// from the input.
str := strings.ToUpper(mem.System, scanner.Text())
// Write out the uppercased line.
fmt.Println(str)
// Free the occupied memory.
mem.FreeString(mem.System, str)
}
// Check for errors during Scan. End of file is
// expected and not reported by Scan as an error.
if err := scanner.Err(); err != nil {
fmt.Fprintf(os.Stderr, "error: %s\n", err)
os.Exit(1)
}
}