]> Zhao Yanbai Git Server - acecode.git/commitdiff
add <<The Go Programming Language Exercise
authorAceVest <zhaoyanbai@126.com>
Mon, 5 Jun 2017 15:46:12 +0000 (23:46 +0800)
committerAceVest <zhaoyanbai@126.com>
Mon, 5 Jun 2017 15:46:12 +0000 (23:46 +0800)
learn/go/exercise/1.1.go [new file with mode: 0644]
learn/go/exercise/1.2.go [new file with mode: 0644]
learn/go/exercise/1.3.go [new file with mode: 0644]
learn/go/exercise/1.4.go [new file with mode: 0644]

diff --git a/learn/go/exercise/1.1.go b/learn/go/exercise/1.1.go
new file mode 100644 (file)
index 0000000..e86f6a8
--- /dev/null
@@ -0,0 +1,28 @@
+/*
+ * ------------------------------------------------------------------------
+ *   File Name: 1.1.go
+ *      Author: Zhao Yanbai
+ *              2017-06-05 23:10:22 Monday CST
+ * Description: none
+ * ------------------------------------------------------------------------
+ */
+package main
+import (
+       "os"
+       "fmt"
+)
+
+func main() {
+       defer fmt.Println("Program Exited...")
+
+       s, sep := "", " "
+       for i, arg := range os.Args[:] {
+               if i > 0 {
+                       s += sep
+               }
+
+               s += arg
+       }
+
+       fmt.Println(s)
+}
diff --git a/learn/go/exercise/1.2.go b/learn/go/exercise/1.2.go
new file mode 100644 (file)
index 0000000..f63b4ee
--- /dev/null
@@ -0,0 +1,20 @@
+/*
+ * ------------------------------------------------------------------------
+ *   File Name: 1.2.go
+ *      Author: Zhao Yanbai
+ *              2017-06-05 23:13:55 Monday CST
+ * Description: none
+ * ------------------------------------------------------------------------
+ */
+package main
+import (
+       "os"
+       "fmt"
+)
+
+func main() {
+       defer fmt.Println("Program Exited...")
+       for i, v := range(os.Args) {
+               fmt.Printf("%10d %s\n", i, v)
+       }
+}
diff --git a/learn/go/exercise/1.3.go b/learn/go/exercise/1.3.go
new file mode 100644 (file)
index 0000000..8a1c28a
--- /dev/null
@@ -0,0 +1,18 @@
+/*
+ * ------------------------------------------------------------------------
+ *   File Name: 1.3.go
+ *      Author: Zhao Yanbai
+ *              2017-06-05 23:16:25 Monday CST
+ * Description: none
+ * ------------------------------------------------------------------------
+ */
+package main
+import (
+       "fmt"
+)
+
+func main() {
+       defer fmt.Println("Program Exited...")
+
+       
+}
diff --git a/learn/go/exercise/1.4.go b/learn/go/exercise/1.4.go
new file mode 100644 (file)
index 0000000..7a84be9
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * ------------------------------------------------------------------------
+ *   File Name: 1.4.go
+ *      Author: Zhao Yanbai
+ *              2017-06-05 23:24:57 Monday CST
+ * Description: none
+ * ------------------------------------------------------------------------
+ */
+package main
+import (
+       "os"
+       "fmt"
+       "log"
+       "bufio"
+)
+
+
+
+
+func main() {
+       defer fmt.Println("Program Exited...")
+
+       files := os.Args[1:]
+
+       if len(files) == 0 {
+               countLines(os.Stdin)
+       } else {
+               for _, file := range(files) {
+                       fmt.Println("Counting %s\n", file)
+                       f, err := os.Open(file)
+                       defer f.Close()
+                       if err != nil {
+                               log.Fatal(err)
+                       }
+                       countLines(f)
+               }
+       }
+}
+
+
+func countLines(f *os.File) {
+       var counts = make(map[string]int)
+       scanner := bufio.NewScanner(f)
+       for scanner.Scan() {
+               counts[scanner.Text()]++
+       }
+
+
+       for k, v := range(counts) {
+               fmt.Printf("%s:%d\n", k, v)
+       }
+}