Line Filters in Groovy
Our line filter program reads input from stdin, processes it, and then prints a derived result to stdout. In this case, it writes a capitalized version of all input text. You can use this pattern to write your own Groovy line filters.
To try out our line filter, first make a file with a few lowercase lines.
Then use the line filter to get uppercase lines.
In this Groovy version:
We use a
Scanner
to read input fromSystem.in
, which is similar to thebufio.Scanner
in the Go version.The
while
loop withhasNextLine()
is equivalent to thefor scanner.Scan()
loop in Go.We use the
toUpperCase()
method to convert each line to uppercase, similar tostrings.ToUpper()
in Go.Error handling is done by checking
scanner.ioException()
after the loop, which is similar to checkingscanner.Err()
in the Go version.We use
System.err.println()
to print error messages to stderr, equivalent tofmt.Fprintln(os.Stderr, ...)
in Go.
This Groovy script can be run directly without compilation, making it convenient for quick scripting tasks.