From: AceVest Date: Mon, 5 Jun 2017 15:46:12 +0000 (+0800) Subject: add < 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 index 0000000..f63b4ee --- /dev/null +++ b/learn/go/exercise/1.2.go @@ -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 index 0000000..8a1c28a --- /dev/null +++ b/learn/go/exercise/1.3.go @@ -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 index 0000000..7a84be9 --- /dev/null +++ b/learn/go/exercise/1.4.go @@ -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) + } +}