2019年1月5日土曜日

開発環境

プログラミング言語Go (ADDISON-WESLEY PROFESSIONAL COMPUTING SERIES) (Alan A.A. Donovan(著)、Brian W. Kernighan(著)、柴田 芳樹(翻訳)、丸善出版)の第1章(チュートリアル)、1.3(重複した行を見つける)、練習問題1.4の解答を求めてみる。

コード

package main

import (
 "bufio"
 "fmt"
 "os"
)

func main() {
 counts := make(map[string]int)
 filenames := make(map[string]string)
 files := os.Args[1:]
 if len(files) == 0 {
  countLines(os.Stdin, counts, "os.Stdin", filenames)
 } 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, arg, filenames)
   f.Close()
  }
 }
 for line, n := range counts {
  if n > 1 {
   fmt.Printf("%d\t%s\n", n, line)
   fmt.Printf("files: %s\n", filenames[line])
  }
 }
}

func countLines(f *os.File, counts map[string]int, filename string, filenames map[string]string) {
 input := bufio.NewScanner(f)
 for input.Scan() {
  text := input.Text()
  counts[text]++
  filenames[text] += filename + " "
 }
}

入出力結果(Terminal, cmd(コマンドプロンプト))

$ go run sample4.go < sample4.go
2 }
files: os.Stdin os.Stdin 
3  }
files: os.Stdin os.Stdin os.Stdin 
2   }
files: os.Stdin os.Stdin 
3 
files: os.Stdin os.Stdin os.Stdin 
$ go build -o dup2 sample4.go
$ ./dup2  sample4.go
2   }
files: sample4.go sample4.go 
3 
files: sample4.go sample4.go sample4.go 
3  }
files: sample4.go sample4.go sample4.go 
2 }
files: sample4.go sample4.go 
$ ./dup2  sample1.go sample2.go sample3.go sample4.go
2  "strings"
files: sample1.go sample3.go 
5 }
files: sample1.go sample2.go sample3.go sample4.go sample4.go 
2   }
files: sample4.go sample4.go 
4 )
files: sample1.go sample2.go sample3.go sample4.go 
4  "os"
files: sample1.go sample2.go sample3.go sample4.go 
4  "fmt"
files: sample1.go sample2.go sample3.go sample4.go 
4 func main() {
files: sample1.go sample2.go sample3.go sample4.go 
4 package main
files: sample1.go sample2.go sample3.go sample4.go 
5  }
files: sample2.go sample3.go sample4.go sample4.go sample4.go 
4 import (
files: sample1.go sample2.go sample3.go sample4.go 
11 
files: sample1.go sample1.go sample2.go sample2.go sample3.go sample3.go sample3.go sample3.go sample4.go sample4.go sample4.go 
$ ./dup2 sample4.go sample4.go
2  for line, n := range counts {
files: sample4.go sample4.go 
2   if n > 1 {
files: sample4.go sample4.go 
2    fmt.Printf("%d\t%s\n", n, line)
files: sample4.go sample4.go 
2  for input.Scan() {
files: sample4.go sample4.go 
2 import (
files: sample4.go sample4.go 
2  "fmt"
files: sample4.go sample4.go 
2    if err != nil {
files: sample4.go sample4.go 
2  "bufio"
files: sample4.go sample4.go 
2    countLines(f, counts, arg, filenames)
files: sample4.go sample4.go 
6  }
files: sample4.go sample4.go sample4.go sample4.go sample4.go sample4.go 
2    fmt.Printf("files: %s\n", filenames[line])
files: sample4.go sample4.go 
2 func countLines(f *os.File, counts map[string]int, filename string, filenames map[string]string) {
files: sample4.go sample4.go 
2  } else {
files: sample4.go sample4.go 
2     continue
files: sample4.go sample4.go 
2    }
files: sample4.go sample4.go 
2    f.Close()
files: sample4.go sample4.go 
4   }
files: sample4.go sample4.go sample4.go sample4.go 
4 }
files: sample4.go sample4.go sample4.go sample4.go 
2 func main() {
files: sample4.go sample4.go 
2  counts := make(map[string]int)
files: sample4.go sample4.go 
2  files := os.Args[1:]
files: sample4.go sample4.go 
2   for _, arg := range files {
files: sample4.go sample4.go 
2 package main
files: sample4.go sample4.go 
2  "os"
files: sample4.go sample4.go 
2  if len(files) == 0 {
files: sample4.go sample4.go 
2  input := bufio.NewScanner(f)
files: sample4.go sample4.go 
2   text := input.Text()
files: sample4.go sample4.go 
2   filenames[text] += filename + " "
files: sample4.go sample4.go 
2     fmt.Fprintf(os.Stderr, "dup2: %v\n", err)
files: sample4.go sample4.go 
6 
files: sample4.go sample4.go sample4.go sample4.go sample4.go sample4.go 
2 )
files: sample4.go sample4.go 
2  filenames := make(map[string]string)
files: sample4.go sample4.go 
2   countLines(os.Stdin, counts, "os.Stdin", filenames)
files: sample4.go sample4.go 
2    f, err := os.Open(arg)
files: sample4.go sample4.go 
2   counts[text]++
files: sample4.go sample4.go 
$

0 コメント:

コメントを投稿