comparison src/tac.go @ 0:de451fa0c9cd

golang repository.
author pyon@macmini
date Sat, 01 Oct 2016 11:16:31 +0900
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:de451fa0c9cd
1 package main
2
3 import (
4 "bufio"
5 "flag"
6 "fmt"
7 "os"
8 )
9
10 func main() {
11 flag.Parse()
12
13 files := flag.Args()
14
15 var lines []string
16
17 // Input
18 if len( files ) == 0 {
19 lines = ReverseLines( os.Stdin, nil )
20 } else {
21 for _, arg := range files {
22 f, err := os.Open( arg )
23 if err != nil {
24 fmt.Fprintf( os.Stderr, "tac : %v\n", err )
25 continue
26 }
27 l := ReverseLines( f, nil )
28 f.Close()
29 lines = append( lines, l... )
30 }
31 }
32
33 // Output
34 for _, line := range lines {
35 fmt.Println( line )
36 }
37 }
38
39 func ReverseLines( f *os.File, l []string ) []string {
40 input := bufio.NewScanner( f )
41 for input.Scan() {
42 l = append( l, input.Text() )
43 }
44 for i, j := 0, len(l) - 1; i < j; i, j = i + 1, j - 1 {
45 l[i], l[j] = l[j], l[i]
46 }
47 return l
48 }
49