Skip to content

Instantly share code, notes, and snippets.

@kenzo0107
Last active March 27, 2020 18:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kenzo0107/d5d98776b7e6432b3856ce9abe340c31 to your computer and use it in GitHub Desktop.
Save kenzo0107/d5d98776b7e6432b3856ce9abe340c31 to your computer and use it in GitHub Desktop.
//【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