some experiments
This commit is contained in:
parent
dc6840fed3
commit
6dacfab5db
1
README
1
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
|
||||
|
@ -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
|
||||
*/
|
||||
|
@ -1,7 +0,0 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
fmt.Println("1 + 1 =", 1 + 1)
|
||||
}
|
22
src/02-values.go
Normal file
22
src/02-values.go
Normal 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.
|
@ -1,9 +0,0 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
fmt.Println(len("Hello World"))
|
||||
fmt.Println("Hello World"[1])
|
||||
fmt.Println("Hello " + "World")
|
||||
}
|
@ -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)
|
||||
}
|
@ -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.
|
||||
|
@ -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
|
||||
*/
|
||||
|
@ -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()`.
|
||||
|
Loading…
x
Reference in New Issue
Block a user