diff --git a/README b/README index d98dea0..98eac50 100644 --- a/README +++ b/README @@ -15,6 +15,7 @@ get web presence in good enough shape to point to from blog post subscription management validate book style in editor + 01-hello 22-varags xx-rand xx-sha1 diff --git a/src/01-hello.go b/src/01-hello.go index 6ed73fb..045cbec 100644 --- a/src/01-hello.go +++ b/src/01-hello.go @@ -1,11 +1,14 @@ + // Here's an example Go program. + package main import "fmt" func main() { - fmt.Println("Hello, world") + fmt.Println("Hello world") // It prints `Hello world`. } -// == running -// $ go run 01-hello.go -// Hello, world +/* +$ go run 01-hello.go // Use `go run` to run the program. +Hello world +*/ diff --git a/src/02-numbers.go b/src/02-numbers.go deleted file mode 100644 index e017c5f..0000000 --- a/src/02-numbers.go +++ /dev/null @@ -1,7 +0,0 @@ -package main - -import "fmt" - -func main() { - fmt.Println("1 + 1 =", 1 + 1) -} diff --git a/src/02-values.go b/src/02-values.go new file mode 100644 index 0000000..9fe59ce --- /dev/null +++ b/src/02-values.go @@ -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. diff --git a/src/03-strings.go b/src/03-strings.go deleted file mode 100644 index 6f2218c..0000000 --- a/src/03-strings.go +++ /dev/null @@ -1,9 +0,0 @@ -package main - -import "fmt" - -func main() { - fmt.Println(len("Hello World")) - fmt.Println("Hello World"[1]) - fmt.Println("Hello " + "World") -} diff --git a/src/04-booleans.go b/src/04-booleans.go deleted file mode 100644 index b339bea..0000000 --- a/src/04-booleans.go +++ /dev/null @@ -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) -} diff --git a/src/10-for.go b/src/10-for.go index 0c269c1..6cc4de9 100644 --- a/src/10-for.go +++ b/src/10-for.go @@ -1,14 +1,20 @@ + // `for` is Go's only looping construct. Below are two common + // forms. + package main import "fmt" func main() { - i := 1 - for i <= 10 { + i := 1 // We initialize `i` with `1` and loop until it's 10. + for i <= 10 { fmt.Println(i) 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) } } + + // There are other forms of `for`; we'll see them later. diff --git a/src/21-returns.go b/src/21-returns.go index 3c711e5..a01a712 100644 --- a/src/21-returns.go +++ b/src/21-returns.go @@ -1,13 +1,22 @@ + // A function can return multiple values. + package main import "fmt" -func vals() (int, int) { - return 3, 7 +func vals() (int, int) { // The `(int, int)` in this signature shows that the + return 3, 7 // function returns 2 ints. + } func main() { - x, y := vals() - fmt.Println(x) - fmt.Println(y) + x, y := vals() // Use the 2 different return values from the call. + fmt.Println(x) + fmt.Println(y) } + +/* +$ go run 21-returns.go +3 +7 +*/ diff --git a/src/xx-sha1.go b/src/xx-sha1.go index 412d99d..98d31d4 100644 --- a/src/xx-sha1.go +++ b/src/xx-sha1.go @@ -1,25 +1,27 @@ - // `crypto/sha1` computes SHA1 hashes. + // `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. +} -package main - -import ( - "fmt" - "crypto/sha1" - "encoding/hex" -) - -func main() { - h := sha1.New() // The basic 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, but usually you won't need it. - - 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()`. + // You can compute other hashes like MD5 using the + // same pattern. Import `crypto/md5` and use + // `md5.New()`.