go-examples/go-cat/main.go

47 lines
794 B
Go
Raw Normal View History

2017-07-07 12:29:36 +00:00
package main
import "fmt"
import "io"
import "io/ioutil"
import "log"
import "os"
import "strings"
var err error
var reader io.Reader
func openFile(path *string) *os.File {
f, err := os.Open(os.Args[1])
if err != nil {
log.Fatalln(err)
}
return f
}
func readMe(r *io.Reader) {
if content, err := ioutil.ReadAll(*r); err != nil {
log.Fatalln(err)
} else {
fmt.Printf("%s", content)
}
}
func main() {
if len(os.Args) <= 1 {
var content string
if _, err := fmt.Scanln(&content); err != nil {
log.Fatalln(err)
}
reader = strings.NewReader(content)
} else {
readers := []io.Reader{}
for i := 1; i < len(os.Args); i++ {
f := openFile(&os.Args[i])
defer f.Close()
readers = append(readers, f)
}
reader = io.MultiReader(readers...)
}
readMe(&reader)
}