12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- package main
- import (
- "bufio"
- "fmt"
- "os"
- )
- func Test() {
- fmt.Printf("test message!")
- }
- func DUP2() {
- counts := make(map[string]int)
- files := os.Args[1:]
- for _,value := range files {
- f,err :=os.Open(value)
- fmt.Printf("内容:%s\n",value)
- fmt.Print(f,err)
- }
- if len(files) == 0 {
- countLines(os.Stdin, counts)
- } else {
- for _, arg := range files {
- f, err := os.Open(arg)
- if err != nil {
- fmt.Fprintf(os.Stderr, "dup2: %v\n", err)
- continue
- }
- countLines(f, counts)
- f.Close()
- }
- }
- for line, n := range counts {
- fmt.Printf("%d\t%s\n", n, line)
- }
- }
- func countLines(f *os.File, counts map[string]int) {
- input := bufio.NewScanner(f)
- for input.Scan() && input.Text() != "end!" {
- counts[input.Text()]++
- }
-
- }
|