Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "bufio"
- "flag"
- "fmt"
- "os"
- )
- func main() {
- lines := flag.Int("n", 10, "Lines")
- reverse := flag.Bool("r", false, "Reverse")
- flag.Parse()
- args := flag.Args()
- // 引数があるか確認
- if len(args) == 0 {
- fmt.Println("Plese input file path.")
- os.Exit(1)
- }
- file, err := os.Open(args[0])
- if err != nil {
- fmt.Println(err)
- os.Exit(1)
- }
- defer file.Close()
- var text []string
- sc := bufio.NewScanner(file)
- // linesNumber := *lines + 1
- for i := 1; sc.Scan(); i++ {
- text = append(text, sc.Text())
- }
- for i := 0; i < *lines; i++ {
- if len(text) > i {
- if *reverse {
- point := len(text) - 1 - i
- fmt.Println(text[point])
- } else {
- fmt.Println(text[i])
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment