some experiments

This commit is contained in:
Mark McGranaghan 2012-09-20 19:13:03 -07:00
parent dc6840fed3
commit 6dacfab5db
9 changed files with 79 additions and 63 deletions

1
README
View File

@ -15,6 +15,7 @@ get web presence in good enough shape to point to from blog post
subscription management subscription management
validate book style in editor validate book style in editor
01-hello
22-varags 22-varags
xx-rand xx-rand
xx-sha1 xx-sha1

View File

@ -1,11 +1,14 @@
// Here's an example Go program.
package main package main
import "fmt" import "fmt"
func main() { func main() {
fmt.Println("Hello, world") fmt.Println("Hello world") // It prints `Hello world`.
} }
// == running /*
// $ go run 01-hello.go $ go run 01-hello.go // Use `go run` to run the program.
// Hello, world Hello world
*/

View File

@ -1,7 +0,0 @@
package main
import "fmt"
func main() {
fmt.Println("1 + 1 =", 1 + 1)
}

22
src/02-values.go Normal file
View File

@ -0,0 +1,22 @@
// Go has various value types, including strings,
// different types of numbers, booleans, etc.
package main
import "fmt"
func main() {
fmt.Println("Hello world") // Strings - you already saw these.
fmt.Println("Hello World"[1])
fmt.Println("Hello " + "World")
fmt.Println("1+1 =", 1+1) // Integers and floats.
fmt.Println("7.0/3.0 =", 7.0/3.0)
fmt.Println(true && false) // Booleans as you'd expect.
fmt.Println(true || false)
fmt.Println(!true)
}
// This is just a sampling of Go's value types. We'll
// learn more about them as we go.

View File

@ -1,9 +0,0 @@
package main
import "fmt"
func main() {
fmt.Println(len("Hello World"))
fmt.Println("Hello World"[1])
fmt.Println("Hello " + "World")
}

View File

@ -1,11 +0,0 @@
package main
import "fmt"
func main() {
fmt.Println(true && true)
fmt.Println(true && false)
fmt.Println(true || true)
fmt.Println(true || false)
fmt.Println(!true)
}

View File

@ -1,14 +1,20 @@
// `for` is Go's only looping construct. Below are two common
// forms.
package main package main
import "fmt" import "fmt"
func main() { func main() {
i := 1 i := 1 // We initialize `i` with `1` and loop until it's 10.
for i <= 10 { for i <= 10 {
fmt.Println(i) fmt.Println(i)
i = i + 1 i = i + 1
} }
for j := 1; j <= 10; j++ {
for j := 1; j <= 10; j++ { // This is a common idiom. We can do it on one line.
fmt.Println(j) fmt.Println(j)
} }
} }
// There are other forms of `for`; we'll see them later.

View File

@ -1,13 +1,22 @@
// A function can return multiple values.
package main package main
import "fmt" import "fmt"
func vals() (int, int) { func vals() (int, int) { // The `(int, int)` in this signature shows that the
return 3, 7 return 3, 7 // function returns 2 ints.
} }
func main() { func main() {
x, y := vals() x, y := vals() // Use the 2 different return values from the call.
fmt.Println(x) fmt.Println(x)
fmt.Println(y) fmt.Println(y)
} }
/*
$ go run 21-returns.go
3
7
*/

View File

@ -1,25 +1,27 @@
// `crypto/sha1` computes SHA1 hashes. // `crypto/sha1` computes SHA1 hashes.
package main package main
import ( import (
"fmt" "fmt"
"crypto/sha1" "crypto/sha1"
"encoding/hex" "encoding/hex"
) )
func main() { func main() {
h := sha1.New() // The basic pattern is `sha1.New()`, `sha1.Write(bytes)`, then h := sha1.New() // The pattern is `sha1.New()`, `sha1.Write(bytes)`,
// `sha1.Sum([]byte{}) // then `sha1.Sum([]byte{})
h.Write([]byte("sha1 this string")) // `Write` expects bytes. If you have a string `s` use h.Write([]byte("sha1 this string")) // `Write` expects bytes. If you have a string `s`
// `[]byte(s)` to coerce it. // use `[]byte(s)` to coerce it.
bs := h.Sum(nil) // Get the result. The argument to `Sum` can be used to append bs := h.Sum(nil) // Get the result. The argument to `Sum` can be used
// to an existing buffer, but usually you won't need it. // to append to an existing buffer: usually uneeded.
fmt.Println(hex.EncodeToString(bs)) // SHA1 values are often printed in hex, for example with git. 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 // You can compute other hashes like MD5 using the
// pattern. Import `crypto/md5` and use `md5.New()`. // same pattern. Import `crypto/md5` and use
// `md5.New()`.