added a test of using either stdin or a file from argv
This commit is contained in:
parent
75b8de26b0
commit
84d5063722
29
stdin-or-argv/go.go
Normal file
29
stdin-or-argv/go.go
Normal file
@ -0,0 +1,29 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
// "io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
// "bufio"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var usage = "usage: splitter [file]"
|
||||
|
||||
func main() {
|
||||
var of *os.File
|
||||
var err error
|
||||
if len(os.Args) == 2 {
|
||||
filename := os.Args[1]
|
||||
of, err = os.Open(filename)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
} else {
|
||||
of = os.Stdin
|
||||
}
|
||||
b, err := ioutil.ReadAll(of)
|
||||
fmt.Printf("%v\n", strings.Fields(string(b)))
|
||||
}
|
Loading…
Reference in New Issue
Block a user