From 9536908ab8ac304a60158259ca4adeb557a59dfe Mon Sep 17 00:00:00 2001 From: derek mcquay Date: Thu, 3 Dec 2015 19:33:19 -0800 Subject: [PATCH] same --- ch1/echo/1_1.go | 12 ++++++++++++ ch1/echo/1_2.go | 19 +++++++++++++++++++ ch1/echo/1_3.go | 33 +++++++++++++++++++++++++++++++++ ch1/echo/echo.go | 11 +++++++++++ 4 files changed, 75 insertions(+) create mode 100644 ch1/echo/1_1.go create mode 100644 ch1/echo/1_2.go create mode 100644 ch1/echo/1_3.go create mode 100644 ch1/echo/echo.go diff --git a/ch1/echo/1_1.go b/ch1/echo/1_1.go new file mode 100644 index 0000000..23ee595 --- /dev/null +++ b/ch1/echo/1_1.go @@ -0,0 +1,12 @@ +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/echo/1_2.go b/ch1/echo/1_2.go new file mode 100644 index 0000000..02086cc --- /dev/null +++ b/ch1/echo/1_2.go @@ -0,0 +1,19 @@ +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/echo/1_3.go b/ch1/echo/1_3.go new file mode 100644 index 0000000..5c9b00c --- /dev/null +++ b/ch1/echo/1_3.go @@ -0,0 +1,33 @@ +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/echo/echo.go b/ch1/echo/echo.go new file mode 100644 index 0000000..a5df3ec --- /dev/null +++ b/ch1/echo/echo.go @@ -0,0 +1,11 @@ +package main + +import ( + "fmt" + "os" + "strings" +) + +func main() { + fmt.Println(strings.Join(os.Args[1:], " ")) +}