This commit is contained in:
Mark McGranaghan 2012-09-23 11:47:31 -07:00
parent e02b47ea22
commit accda24c6d
13 changed files with 89 additions and 63 deletions

View File

@ -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()`.

View File

@ -0,0 +1,2 @@
$ go run sha1-hashes.go
cf23df2207d99a74fbe169e3eba035e633b65d94

View File

@ -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()`.

View File

@ -1,3 +1,5 @@
// ## Signals
package main
import ("fmt"; "os"; "os/signal"; "syscall")

4
083-signals/signals.sh Normal file
View File

@ -0,0 +1,4 @@
$ go run signals.go
Awaiting signal
^C
interrupt

View File

@ -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

28
084-sort/sort.go Normal file
View File

@ -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

5
084-sort/sort.sh Normal file
View File

@ -0,0 +1,5 @@
$ go run sort.go
[a b c]
[2 4 7]
true
1

View File

@ -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()
}

View File

@ -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}]

View File

@ -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

6
086-spawn/spawn.sh Normal file
View File

@ -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

View File

@ -79,7 +79,7 @@ rate-limiting
redis
regexs
scatter-gather
sha1
sha1-hashes
signals
sort
sort-by-function