Created
January 18, 2021 14:47
-
-
Save ohbarye/4cd31c826e37addf5164c57edf402e41 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"flag" | |
"fmt" | |
"log" | |
"os" | |
) | |
var showLineNumber = flag.Bool("n", false, "Line number") | |
func main() { | |
flag.Parse() | |
allLines := make([]string, 0, 0) | |
for _, filename := range flag.Args() { | |
lines, err := read(filename) | |
if err != nil { | |
log.Fatal("owata") | |
} | |
allLines = append(allLines, lines...) | |
} | |
show(&allLines) | |
} | |
func read(src string) ([]string, error) { | |
sf, err := os.Open(src) | |
if err != nil { | |
return nil, err | |
} | |
defer sf.Close() | |
scanner := bufio.NewScanner(sf) | |
lines := make([]string, 0, 0) | |
for scanner.Scan() { | |
lines = append(lines, scanner.Text()) | |
} | |
if err := scanner.Err(); err != nil { | |
fmt.Fprintln(os.Stderr, "読み込みに失敗しました:", err) | |
} | |
return lines, nil | |
} | |
func show(allLines *[]string) { | |
for i, st := range *allLines { | |
if *showLineNumber { | |
fmt.Fprint(os.Stdout, i+1) | |
fmt.Fprint(os.Stdout, ": ") | |
} | |
fmt.Fprintln(os.Stdout, st) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment