package main import ( "bufio" "fmt" "io" "os" "github.com/alexflint/go-arg" common "source.heropunch.io/tomo/go-coreutils" ) func head(num uint, reader io.Reader) { scanner := bufio.NewScanner(reader) for scanner.Scan() && num > 0 { fmt.Println(scanner.Text()) num = num - 1 } common.ExitIfError(scanner.Err()) } func main() { var args struct { Lines uint `arg:"-n"` Files []string `arg:"positional"` } args.Lines = 10 arg.MustParse(&args) if len(args.Files) == 0 { head(args.Lines, os.Stdin) os.Exit(0) } for _, filename := range args.Files { file, err := os.Open(filename) common.ExitIfError(err) head(args.Lines, file) err = file.Close() common.ExitIfError(err) } }