From 5cafc322921551655038db3a2447d1e13e1f5823 Mon Sep 17 00:00:00 2001 From: derek mcquay Date: Thu, 3 Dec 2015 19:32:57 -0800 Subject: [PATCH] same --- ch1/1_1.go | 12 ------------ ch1/1_2.go | 19 ------------------- ch1/1_3.go | 33 --------------------------------- ch1/1_4.go | 51 --------------------------------------------------- ch1/dup2.go | 37 ------------------------------------- ch1/echo.go | 11 ----------- 6 files changed, 163 deletions(-) delete mode 100644 ch1/1_1.go delete mode 100644 ch1/1_2.go delete mode 100644 ch1/1_3.go delete mode 100644 ch1/1_4.go delete mode 100644 ch1/dup2.go delete mode 100644 ch1/echo.go diff --git a/ch1/1_1.go b/ch1/1_1.go deleted file mode 100644 index 23ee595..0000000 --- a/ch1/1_1.go +++ /dev/null @@ -1,12 +0,0 @@ -package main - -import ( - "fmt" - "os" - "strings" -) - -func main() { - fmt.Printf("command: %s\n", os.Args[0]) - fmt.Printf("args: %s\n", strings.Join(os.Args[1:], " ")) -} diff --git a/ch1/1_2.go b/ch1/1_2.go deleted file mode 100644 index 02086cc..0000000 --- a/ch1/1_2.go +++ /dev/null @@ -1,19 +0,0 @@ -package main - -import ( - "fmt" - "os" - "strings" -) - -func main() { - fmt.Printf("command: %s\n", os.Args[0]) - fmt.Printf("args: %s\n", strings.Join(os.Args[1:], " ")) - str := "" - deli := " " - for i, j := range os.Args[1:] { - str += fmt.Sprintf("index: %d value: %s", i, j) - str += deli - } - fmt.Println(str) -} diff --git a/ch1/1_3.go b/ch1/1_3.go deleted file mode 100644 index 5c9b00c..0000000 --- a/ch1/1_3.go +++ /dev/null @@ -1,33 +0,0 @@ -package main - -import ( - "fmt" - "os" - "strings" - "time" -) - -func usingJoin() { - fmt.Println(strings.Join(os.Args[1:], " ")) -} - -func forLoop() { - s, sep := "", " " - for _, i := range os.Args[1:] { - s += i - s += sep - } - fmt.Println(s) -} - -func main() { - start := time.Now() - forLoop() - t1 := time.Since(start) - fmt.Println("for loop: ", t1) - start = time.Now() - usingJoin() - t2 := time.Since(start) - fmt.Println("using join: ", t2) - fmt.Println("Δ", t1-t2) -} diff --git a/ch1/1_4.go b/ch1/1_4.go deleted file mode 100644 index c4acd56..0000000 --- a/ch1/1_4.go +++ /dev/null @@ -1,51 +0,0 @@ -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 - } - } -} diff --git a/ch1/dup2.go b/ch1/dup2.go deleted file mode 100644 index 8f2aab0..0000000 --- a/ch1/dup2.go +++ /dev/null @@ -1,37 +0,0 @@ -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()]++ - } -} diff --git a/ch1/echo.go b/ch1/echo.go deleted file mode 100644 index a5df3ec..0000000 --- a/ch1/echo.go +++ /dev/null @@ -1,11 +0,0 @@ -package main - -import ( - "fmt" - "os" - "strings" -) - -func main() { - fmt.Println(strings.Join(os.Args[1:], " ")) -}