Thursday, February 2, 2017

'c' on steriods ?

'go' test # 2
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
package main

import (
 "bufio"
 "fmt"
 "os"
)

func main() {
 // Read From 'stdin'
 stdinscanner := bufio.NewScanner(os.Stdin)
 // Split Input into words using intrinsic
 stdinscanner.Split(bufio.ScanWords)

 // Output the words
 for stdinscanner.Scan() {
  fmt.Printf("%s\n", stdinscanner.Text())
 }
}

We have a simple program that reads a sentence from stdin and outputs each word - we can invoke it like :-
bash-3.2$ ./splitsentence
the quick brown fox jumped over the lazy Garth
the
quick
brown
fox
jumped

- we need to Ctrl-C here to stop splitsentence executing, else, we can do this, in the Unix 'pipe' style :-
bash-3.2$ echo "the quick brown fox jumped over the lazy Garth" | ./splitsentence
the
quick
brown
fox
jumped
over
the
lazy
Garth

No comments:

Post a Comment