From accda24c6d7658de919abe4c0e4f583a7ca0c868 Mon Sep 17 00:00:00 2001 From: Mark McGranaghan Date: Sun, 23 Sep 2012 11:47:31 -0700 Subject: [PATCH] updates --- 082-sha1-hashes/sha1-hashes.go | 30 +++++++++++++++++++ 082-sha1-hashes/sha1-hashes.sh | 2 ++ 082-sha1/082-sha1.go | 27 ----------------- 083-signals/{083-signals.go => signals.go} | 2 ++ 083-signals/signals.sh | 4 +++ 084-sort/084-sort.go | 28 ----------------- 084-sort/sort.go | 28 +++++++++++++++++ 084-sort/sort.sh | 5 ++++ ...ort-by-function.go => sort-by-function.go} | 5 ++-- 085-sort-by-function/sort-by-function.sh | 4 +++ 086-spawn/{086-spawn.go => spawn.go} | 9 +++--- 086-spawn/spawn.sh | 6 ++++ tool/index.txt | 2 +- 13 files changed, 89 insertions(+), 63 deletions(-) create mode 100644 082-sha1-hashes/sha1-hashes.go create mode 100644 082-sha1-hashes/sha1-hashes.sh delete mode 100644 082-sha1/082-sha1.go rename 083-signals/{083-signals.go => signals.go} (94%) create mode 100644 083-signals/signals.sh delete mode 100644 084-sort/084-sort.go create mode 100644 084-sort/sort.go create mode 100644 084-sort/sort.sh rename 085-sort-by-function/{085-sort-by-function.go => sort-by-function.go} (94%) create mode 100644 085-sort-by-function/sort-by-function.sh rename 086-spawn/{086-spawn.go => spawn.go} (54%) create mode 100644 086-spawn/spawn.sh diff --git a/082-sha1-hashes/sha1-hashes.go b/082-sha1-hashes/sha1-hashes.go new file mode 100644 index 0000000..f637bd9 --- /dev/null +++ b/082-sha1-hashes/sha1-hashes.go @@ -0,0 +1,30 @@ +// ## SHA1 Hashes + +package main + +// Package `crypto/sha1` computes SHA1 hashes. +import "crypto/sha1" +import "encoding/hex" +import "fmt" + +func main() { + // The pattern is `sha1.New()`, `sha1.Write(bytes)`, + // then `sha1.Sum([]byte{}) + h := sha1.New() + + // `Write` expects bytes. If you have a string `s` + // use `[]byte(s)` to coerce it. + h.Write([]byte("sha1 this string")) + + // Get the result. The argument to `Sum` can be used + // to append to an existing buffer: usually uneeded. + bs := h.Sum(nil) + + // SHA1 values are often printed in hex, for example + // with git. + fmt.Println(hex.EncodeToString(bs)) +} + +// You can compute other hashes using a similar +// pattern. For exmpale, to compute MD5 hashes +// import `crypto/md5` and use `md5.New()`. diff --git a/082-sha1-hashes/sha1-hashes.sh b/082-sha1-hashes/sha1-hashes.sh new file mode 100644 index 0000000..cd14ec9 --- /dev/null +++ b/082-sha1-hashes/sha1-hashes.sh @@ -0,0 +1,2 @@ +$ go run sha1-hashes.go +cf23df2207d99a74fbe169e3eba035e633b65d94 diff --git a/082-sha1/082-sha1.go b/082-sha1/082-sha1.go deleted file mode 100644 index 98d31d4..0000000 --- a/082-sha1/082-sha1.go +++ /dev/null @@ -1,27 +0,0 @@ - // `crypto/sha1` computes SHA1 hashes. - -package main - -import ( - "fmt" - "crypto/sha1" - "encoding/hex" -) - -func main() { - h := sha1.New() // The pattern is `sha1.New()`, `sha1.Write(bytes)`, - // then `sha1.Sum([]byte{}) - - h.Write([]byte("sha1 this string")) // `Write` expects bytes. If you have a string `s` - // use `[]byte(s)` to coerce it. - - bs := h.Sum(nil) // Get the result. The argument to `Sum` can be used - // to append to an existing buffer: usually uneeded. - - fmt.Println(hex.EncodeToString(bs)) // SHA1 values are often printed in hex, for example - // with git. -} - - // You can compute other hashes like MD5 using the - // same pattern. Import `crypto/md5` and use - // `md5.New()`. diff --git a/083-signals/083-signals.go b/083-signals/signals.go similarity index 94% rename from 083-signals/083-signals.go rename to 083-signals/signals.go index dab0787..4351601 100644 --- a/083-signals/083-signals.go +++ b/083-signals/signals.go @@ -1,3 +1,5 @@ +// ## Signals + package main import ("fmt"; "os"; "os/signal"; "syscall") diff --git a/083-signals/signals.sh b/083-signals/signals.sh new file mode 100644 index 0000000..3426418 --- /dev/null +++ b/083-signals/signals.sh @@ -0,0 +1,4 @@ +$ go run signals.go +Awaiting signal +^C +interrupt diff --git a/084-sort/084-sort.go b/084-sort/084-sort.go deleted file mode 100644 index ea61796..0000000 --- a/084-sort/084-sort.go +++ /dev/null @@ -1,28 +0,0 @@ -package main // The `sort` package implements sorting for builtins - // and user-defined types. We'll look at some of the -import "fmt" // sorts for builtins here. -import "sort" - -func main() { - strs := []string{"c", "a", "b"} - sort.Strings(strs) // Sort methods are specific to the builtin type. - fmt.Println(strs) // Sorting is in-place (doesn't return a new slice). - - ints := []int{7, 2, 4} - sort.Ints(ints) // Sorting methods are named after the type. - fmt.Println(ints) - - s := sort.IntsAreSorted(ints) // Check if a slice is in sorted order. - fmt.Println(s) -} - -/* -$ go run sort.go -[a b c] -[2 4 7] -true -1 -*/ - -// == todo -// general and convenience searching diff --git a/084-sort/sort.go b/084-sort/sort.go new file mode 100644 index 0000000..22325b8 --- /dev/null +++ b/084-sort/sort.go @@ -0,0 +1,28 @@ +// ## Sort + +// The `sort` package implements sorting for builtins +// and user-defined types. We'll look at some of the +// sorts for builtins here. +package main + +import "fmt" +import "sort" + +func main() { + // Sort methods are specific to the builtin type. + // Sorting is in-place (doesn't return a new slice). + strs := []string{"c", "a", "b"} + sort.Strings(strs) + fmt.Println(strs) + + // Sorting methods are named after the type. + ints := []int{7, 2, 4} + sort.Ints(ints) + fmt.Println(ints) + + // Check if a slice is in sorted order. + s := sort.IntsAreSorted(ints) + fmt.Println(s) +} + +// todo: general and convenience searching diff --git a/084-sort/sort.sh b/084-sort/sort.sh new file mode 100644 index 0000000..007e887 --- /dev/null +++ b/084-sort/sort.sh @@ -0,0 +1,5 @@ +$ go run sort.go +[a b c] +[2 4 7] +true +1 \ No newline at end of file diff --git a/085-sort-by-function/085-sort-by-function.go b/085-sort-by-function/sort-by-function.go similarity index 94% rename from 085-sort-by-function/085-sort-by-function.go rename to 085-sort-by-function/sort-by-function.go index 712a71a..c2eb729 100644 --- a/085-sort-by-function/085-sort-by-function.go +++ b/085-sort-by-function/sort-by-function.go @@ -1,3 +1,5 @@ +// ## Sort by Function + package main import ("fmt" ; "sort") @@ -36,13 +38,10 @@ func main() { {"Bob", 12}, } fmt.Println("Original:", kids) - fmt.Println() sort.Sort(ByName(kids)) fmt.Println("ByName: ", kids) - fmt.Println() sort.Sort(ByAge(kids)) fmt.Println("ByAge: ", kids) - fmt.Println() } diff --git a/085-sort-by-function/sort-by-function.sh b/085-sort-by-function/sort-by-function.sh new file mode 100644 index 0000000..46a6476 --- /dev/null +++ b/085-sort-by-function/sort-by-function.sh @@ -0,0 +1,4 @@ +$ go run sort-by-function.go +Original: [{Jack 10} {Jill 9} {Bob 12}] +ByName: [{Bob 12} {Jack 10} {Jill 9}] +ByAge: [{Jill 9} {Jack 10} {Bob 12}] diff --git a/086-spawn/086-spawn.go b/086-spawn/spawn.go similarity index 54% rename from 086-spawn/086-spawn.go rename to 086-spawn/spawn.go index 8fb330b..c1d0380 100644 --- a/086-spawn/086-spawn.go +++ b/086-spawn/spawn.go @@ -1,16 +1,17 @@ +// ## Spawn + package main import ("os/exec"; "fmt") func main() { - cmd := exec.Command("ls", "-a" "-l", "-h") + cmd := exec.Command("ls", "-a", "-l") out, err := cmd.Output() if err != nil { panic(err) } - fmt.Println("====== Output") + fmt.Println("Files:") fmt.Print(string(out)) } -// == todo -// full command lines with bash? +// todo: full command lines with bash diff --git a/086-spawn/spawn.sh b/086-spawn/spawn.sh new file mode 100644 index 0000000..60cb89a --- /dev/null +++ b/086-spawn/spawn.sh @@ -0,0 +1,6 @@ +$ go run spawn.go +Files: +total 8 +drwxr-xr-x 3 mmcgrana staff 102 Sep 23 11:35 . +drwxr-xr-x 101 mmcgrana staff 3434 Sep 23 11:25 .. +-rw-r--r--@ 1 mmcgrana staff 241 Sep 23 11:37 spawn.go diff --git a/tool/index.txt b/tool/index.txt index 98fa13d..aa79407 100644 --- a/tool/index.txt +++ b/tool/index.txt @@ -79,7 +79,7 @@ rate-limiting redis regexs scatter-gather -sha1 +sha1-hashes signals sort sort-by-function