go-cat2 refactoring
This commit is contained in:
parent
d4ce506f26
commit
22c69aaf27
@ -1,54 +1,25 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
import "io"
|
||||
import "log"
|
||||
import "os"
|
||||
import "strings"
|
||||
|
||||
var err error
|
||||
var reader io.Reader
|
||||
|
||||
const BUFSIZE = 32 * 1024
|
||||
|
||||
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) {
|
||||
var n int
|
||||
buf := make([]byte, BUFSIZE)
|
||||
for {
|
||||
n, err = r.Read(buf)
|
||||
if n == 0 || err == io.EOF {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
log.Fatalln("Read error", err)
|
||||
}
|
||||
fmt.Printf("%s", buf[:n])
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
if len(os.Args) <= 1 {
|
||||
var content string
|
||||
if _, err := fmt.Scanln(&content); err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
reader = strings.NewReader(content)
|
||||
reader = os.Stdin
|
||||
} else {
|
||||
readers := []io.Reader{}
|
||||
for _, fname := range os.Args[1:] {
|
||||
f := openFile(&fname)
|
||||
defer f.Close()
|
||||
readers = append(readers, f)
|
||||
if f, err := os.Open(fname); err != nil {
|
||||
log.Fatalln(err)
|
||||
} else {
|
||||
defer f.Close()
|
||||
readers = append(readers, f)
|
||||
}
|
||||
}
|
||||
reader = io.MultiReader(readers...)
|
||||
}
|
||||
readMe(reader)
|
||||
io.Copy(os.Stdout, reader)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user