tgpl

The Go Programming Language - Solutions
Log | Files | Refs

exercise-1-4.go (500B)


      1 package main
      2 
      3 import (
      4 	"fmt"
      5 	"io/ioutil"
      6 	"os"
      7 	"strings"
      8 )
      9 
     10 func main() {
     11 	names := ""
     12 	counts := make(map[string]int)
     13 	for _, filename := range os.Args[1:] {
     14 		data, err := ioutil.ReadFile(filename)
     15 		if err != nil {
     16 			fmt.Fprintf(os.Stderr, "dup3: %v\n", err)
     17 			continue
     18 		}
     19 		for _, line := range strings.Split(string(data), "\n") {
     20 			counts[line]++
     21 			
     22 		}
     23 		for _, n := range counts {
     24 				if n > 1 {
     25 					names += filename
     26 					names += " "
     27 					break;
     28 				}
     29 		}
     30 	}
     31 
     32 	fmt.Println(names)
     33 }