Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
50 lines (37 sloc) 1.45 KB
Title: 58.Line_Filters.go
Author: OpenSource
Date: 2017-05-23
Description: For Study
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.
package main
import (
func main(){
// Here’s an example line filter in Go that writes a capitalized version of all input text.
// You can use this pattern to write your own Go line filters.
// 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(os.Stdin)
// Text returns the current token, here the next line, from the input.
for scanner.Scan(){
// Write out the uppercased line.
ucl := strings.ToUpper(scanner.Text())
// 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.Fprintln(os.Stderr, "error: ",err)
// To try out our line filter, first make a file with a few lowercase lines.
// Then use the line filter to get uppercase lines.
You can’t perform that action at this time.