updated format to look like gopl.io format
This commit is contained in:
parent
4fd6aaaeff
commit
2cf179e6a4
51
ch1/dup/1_4.go
Normal file
51
ch1/dup/1_4.go
Normal file
@ -0,0 +1,51 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
counts := make(map[string][]string)
|
||||
file := os.Args[1:]
|
||||
if len(file) == 0 {
|
||||
countLines(os.Stdin, counts)
|
||||
} else {
|
||||
for _, i := range file {
|
||||
f, err := os.Open(i)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
}
|
||||
countLines(f, counts)
|
||||
f.Close()
|
||||
}
|
||||
}
|
||||
for lines, n := range counts {
|
||||
if len(n) > 1 {
|
||||
fmt.Printf("%v\t\"%s\"\n", n, strings.Trim(lines, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func contains(slice []string, file string) bool {
|
||||
for _, i := range slice {
|
||||
if i == file {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func countLines(f *os.File, counts map[string][]string) {
|
||||
input := bufio.NewScanner(f)
|
||||
for input.Scan() {
|
||||
cur := counts[input.Text()]
|
||||
if !contains(cur, f.Name()) {
|
||||
cur = append(cur, f.Name())
|
||||
counts[input.Text()] = cur
|
||||
}
|
||||
}
|
||||
}
|
37
ch1/dup/dup2.go
Normal file
37
ch1/dup/dup2.go
Normal file
@ -0,0 +1,37 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
counts := make(map[string]int)
|
||||
file := os.Args[1:]
|
||||
if len(file) == 0 {
|
||||
countLines(os.Stdin, counts)
|
||||
} else {
|
||||
for _, i := range file {
|
||||
f, err := os.Open(i)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
}
|
||||
countLines(f, counts)
|
||||
f.Close()
|
||||
}
|
||||
}
|
||||
for lines, n := range counts {
|
||||
if n > 1 {
|
||||
fmt.Printf("%d\t%s\n", n, lines)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func countLines(f *os.File, counts map[string]int) {
|
||||
input := bufio.NewScanner(f)
|
||||
for input.Scan() {
|
||||
counts[input.Text()]++
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user