Last active
March 27, 2020 18:21
-
-
Save kenzo0107/d5d98776b7e6432b3856ce9abe340c31 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
//【TRY】catコマンドを作ろう | |
package main | |
import ( | |
"bufio" | |
"flag" | |
"fmt" | |
"log" | |
"os" | |
) | |
var ( | |
isShowLineNo *bool = flag.Bool("n", false, "show line number") | |
lineNo int | |
) | |
func main() { | |
flag.Parse() | |
args := flag.Args() | |
for _, b := range args { | |
if err := readFile(b, &lineNo); err != nil { | |
log.Fatal(err) | |
} | |
} | |
} | |
func readFile(b string, lineNo *int) error { | |
f, err := os.Open(b) | |
if err != nil { | |
return err | |
} | |
defer f.Close() | |
scanner := bufio.NewScanner(f) | |
for scanner.Scan() { | |
if *isShowLineNo { | |
*lineNo++ | |
print(*lineNo, ": ") | |
} | |
fmt.Println(scanner.Text()) | |
} | |
if err := scanner.Err(); err != nil { | |
fmt.Fprintln(os.Stderr, "fail to read", err) | |
} | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment