diff --git a/001-hello-world/hello-world.go b/001-hello-world/hello-world.go index 68a7062..422aa97 100644 --- a/001-hello-world/hello-world.go +++ b/001-hello-world/hello-world.go @@ -1,11 +1,11 @@ // ## Hello World // Here's an example Go program. -package main +package main import "fmt" // It prints `Hello world`. func main() { - fmt.Println("Hello world") + fmt.Println("Hello world") } diff --git a/002-values/values.go b/002-values/values.go index 0010a3a..57339cd 100644 --- a/002-values/values.go +++ b/002-values/values.go @@ -2,22 +2,22 @@ // Go has various value types, including strings, // different types of numbers, booleans, etc. -package main +package main import "fmt" func main() { // Here are some strings, which can be added together. - fmt.Println("Hello world") + fmt.Println("Hello world") fmt.Println("Hello " + "other") // Some examples of integers and floats. - fmt.Println("1+1 =", 1+1) + fmt.Println("1+1 =", 1+1) fmt.Println("7.0/3.0 =", 7.0/3.0) // And booleans, which work as you'd expect. - fmt.Println(true && false) + fmt.Println(true && false) fmt.Println(true || false) fmt.Println(!true) } diff --git a/003-variables/variables.go b/003-variables/variables.go index 0a74ce5..f684dea 100644 --- a/003-variables/variables.go +++ b/003-variables/variables.go @@ -7,10 +7,10 @@ import "fmt" func main() { // `var` declares 1 or more variables. The type comes // at the end. - var x string = "Hello world" - fmt.Println(x) + var x string = "Hello world" + fmt.Println(x) // An example of declaring multiple `int` variables. - var a, b int = 1, 2 + var a, b int = 1, 2 fmt.Println(a, b) } diff --git a/004-inline-assignment/inline-assignment.go b/004-inline-assignment/inline-assignment.go index 2fa6ac1..533d682 100644 --- a/004-inline-assignment/inline-assignment.go +++ b/004-inline-assignment/inline-assignment.go @@ -6,6 +6,6 @@ import "fmt" func main() { // `x := val` is shorthand for `var x type = val`. - x := "Hello assignment" - fmt.Println(x) + x := "Hello assignment" + fmt.Println(x) } diff --git a/009-arrays/arrays.go b/009-arrays/arrays.go index e4834e7..947db5f 100644 --- a/009-arrays/arrays.go +++ b/009-arrays/arrays.go @@ -5,8 +5,8 @@ package main import "fmt" func main() { - var x [5]int - x[4] = 100 - fmt.Println(x) - fmt.Println(x[4]) + var x [5]int + x[4] = 100 + fmt.Println(x) + fmt.Println(x[4]) } diff --git a/010-range/range.go b/010-range/range.go index e0cba41..1063513 100644 --- a/010-range/range.go +++ b/010-range/range.go @@ -11,7 +11,7 @@ func main() { x[2] = 77 x[3] = 82 x[4] = 83 - + var total float64 = 0 for _, value := range x { total += value diff --git a/011-slices/slices.go b/011-slices/slices.go index 6f4c3db..6d9265c 100644 --- a/011-slices/slices.go +++ b/011-slices/slices.go @@ -5,12 +5,12 @@ package main import "fmt" func main() { - slice1 := []int{1,2,3} + slice1 := []int{1, 2, 3} slice2 := append(slice1, 4, 5) fmt.Println(slice1) - fmt.Println(slice2) - slice3 := make([]int, 2) - copy(slice3, slice1) - fmt.Println(slice1) - fmt.Println(slice3) + fmt.Println(slice2) + slice3 := make([]int, 2) + copy(slice3, slice1) + fmt.Println(slice1) + fmt.Println(slice3) } diff --git a/012-maps/maps.go b/012-maps/maps.go index a2ac1f7..cd898a2 100644 --- a/012-maps/maps.go +++ b/012-maps/maps.go @@ -5,16 +5,16 @@ package main import "fmt" func main() { - x := make(map[string]int) - x["this"] = 7 - x["that"] = 13 - fmt.Println(x) - fmt.Println(x["this"]) - fmt.Println(x["missing"]) - fmt.Println(len(x)) - delete(x, "that") - fmt.Println(x["that"]) - fmt.Println(len(x)) - _, present := x["that"] - fmt.Println(present) + x := make(map[string]int) + x["this"] = 7 + x["that"] = 13 + fmt.Println(x) + fmt.Println(x["this"]) + fmt.Println(x["missing"]) + fmt.Println(len(x)) + delete(x, "that") + fmt.Println(x["that"]) + fmt.Println(len(x)) + _, present := x["that"] + fmt.Println(present) } diff --git a/013-functions/functions.go b/013-functions/functions.go index 76dab86..4c793b1 100644 --- a/013-functions/functions.go +++ b/013-functions/functions.go @@ -5,16 +5,16 @@ package main import "fmt" func avg(vals []float64) float64 { - total := 0.0 - for _, val := range vals { - total += val - } - return total / float64(len(vals)) + total := 0.0 + for _, val := range vals { + total += val + } + return total / float64(len(vals)) } func main() { - input := []float64{98,93,77,82,83} - fmt.Println(input) - output := avg(input) - fmt.Println(output) + input := []float64{98, 93, 77, 82, 83} + fmt.Println(input) + output := avg(input) + fmt.Println(output) } diff --git a/014-multiple-return-values/multiple-returns-values.go b/014-multiple-return-values/multiple-returns-values.go index 0096bb5..b01f2d6 100644 --- a/014-multiple-return-values/multiple-returns-values.go +++ b/014-multiple-return-values/multiple-returns-values.go @@ -10,15 +10,15 @@ import "fmt" // The `(int, int)` in this signature shows that the // function returns 2 ints. -func vals() (int, int) { - return 3, 7 - +func vals() (int, int) { + return 3, 7 + } func main() { // Use the 2 different return values from the call, // i.e. multiple assignement. - x, y := vals() + x, y := vals() fmt.Println(x) fmt.Println(y) } diff --git a/015-varadic-functions/varadic-functions.go b/015-varadic-functions/varadic-functions.go index 69fefb7..e472abb 100644 --- a/015-varadic-functions/varadic-functions.go +++ b/015-varadic-functions/varadic-functions.go @@ -5,29 +5,29 @@ // number of arguments that will be needed for a function // ahead of time. -package main - +package main + import "fmt" // Varadic args are declared with `...type` and // passed in as a slice. -func add(nums ...int) int { - fmt.Print(nums, " ") +func add(nums ...int) int { + fmt.Print(nums, " ") total := 0 - for _, num := range nums { - total += num + for _, num := range nums { + total += num } - return total -} - + return total +} + func main() { // Varadic functions can be called in the usual way. - fmt.Println(add(1, 2)) - fmt.Println(add(1, 2, 3)) + fmt.Println(add(1, 2)) + fmt.Println(add(1, 2, 3)) // If you already have multiple args in a slice, // apply them to a varadic function using ` // func(slice...)`. - nums := []int{1, 2, 3, 4} - fmt.Println(add(nums...)) -} + nums := []int{1, 2, 3, 4} + fmt.Println(add(nums...)) +} diff --git a/017-recursion/recursion.go b/017-recursion/recursion.go index f55c4d2..b0a0993 100644 --- a/017-recursion/recursion.go +++ b/017-recursion/recursion.go @@ -12,5 +12,5 @@ func factorial(x uint) uint { } func main() { - fmt.Println(factorial(7)) + fmt.Println(factorial(7)) } diff --git a/019-panic/panic.go b/019-panic/panic.go index 6bf9e72..5b66167 100644 --- a/019-panic/panic.go +++ b/019-panic/panic.go @@ -9,5 +9,5 @@ func main() { // We'll use panic throught this book to check for // unexpected errors. This is the only program in the // book designed to panic. - panic("O noes") -} + panic("O noes") +} diff --git a/021-new/new.go b/021-new/new.go index 4c98b62..59385bd 100644 --- a/021-new/new.go +++ b/021-new/new.go @@ -9,8 +9,8 @@ func one(xPtr *int) { } func main() { xPtr := new(int) - fmt.Println(xPtr) - fmt.Println(*xPtr) + fmt.Println(xPtr) + fmt.Println(*xPtr) one(xPtr) fmt.Println(xPtr) fmt.Println(*xPtr) diff --git a/022-structs/structs.go b/022-structs/structs.go index 449860a..bd28bfb 100644 --- a/022-structs/structs.go +++ b/022-structs/structs.go @@ -9,16 +9,16 @@ type Circle struct { } func main() { - cEmptyPtr := new(Circle) - fmt.Println(cEmptyPtr) - fmt.Println(*cEmptyPtr) - - cValue := Circle{x: 1, y: 2, r: 5} - fmt.Println(&cValue) - fmt.Println(cValue) + cEmptyPtr := new(Circle) + fmt.Println(cEmptyPtr) + fmt.Println(*cEmptyPtr) - cOrdered := Circle{1, 2, 5} - fmt.Println(cOrdered) + cValue := Circle{x: 1, y: 2, r: 5} + fmt.Println(&cValue) + fmt.Println(cValue) + + cOrdered := Circle{1, 2, 5} + fmt.Println(cOrdered) } // todo: add field access diff --git a/023-methods/methods.go b/023-methods/methods.go index 196b8a4..8921124 100644 --- a/023-methods/methods.go +++ b/023-methods/methods.go @@ -10,7 +10,7 @@ type Circle struct { } func (c *Circle) area() float64 { - return math.Pi * c.r*c.r + return math.Pi * c.r * c.r } type Rectangle struct { @@ -30,10 +30,10 @@ func (r *Rectangle) area() float64 { } func main() { - circle := Circle{x: 0, y: 3, r: 5} - fmt.Println(circle.area()) - rectangle := Rectangle {x1: 3, x2: 10, y1: 5, y2: 7} - fmt.Println(rectangle.area()) + circle := Circle{x: 0, y: 3, r: 5} + fmt.Println(circle.area()) + rectangle := Rectangle{x1: 3, x2: 10, y1: 5, y2: 7} + fmt.Println(rectangle.area()) } // todo: pointer vs value receivers diff --git a/024-embedding/embedding.go b/024-embedding/embedding.go index c5a1f96..6992bf7 100644 --- a/024-embedding/embedding.go +++ b/024-embedding/embedding.go @@ -14,7 +14,7 @@ type Circle struct { } func (c *Circle) area() float64 { - return math.Pi * c.r*c.r + return math.Pi * c.r * c.r } type Rectangle struct { @@ -42,14 +42,14 @@ func totalArea(shapes ...Shape) float64 { } func main() { - circle := Circle{x: 0, y: 3, r: 5} - rectangle := Rectangle {x1: 3, x2: 10, y1: 5, y2: 7} + circle := Circle{x: 0, y: 3, r: 5} + rectangle := Rectangle{x1: 3, x2: 10, y1: 5, y2: 7} - area := 0.0 - for _, s := range []Shape{&circle, &rectangle} { - area += s.area() - } - fmt.Println(area) + area := 0.0 + for _, s := range []Shape{&circle, &rectangle} { + area += s.area() + } + fmt.Println(area) } // todo: is this named wrong? diff --git a/026-errors/errors.go b/026-errors/errors.go index b78c3cd..7316902 100644 --- a/026-errors/errors.go +++ b/026-errors/errors.go @@ -2,22 +2,25 @@ package main -import ("fmt"; "errors") +import ( + "errors" + "fmt" +) func myFun(arg int) (int, error) { - if arg == 42 { - return -1, errors.New("Can't work with 42") - - } - return arg + 3, nil + if arg == 42 { + return -1, errors.New("Can't work with 42") + + } + return arg + 3, nil } func main() { - r, _ := myFun(7) - fmt.Println(r) + r, _ := myFun(7) + fmt.Println(r) - _, e := myFun(42) - fmt.Println(e) + _, e := myFun(42) + fmt.Println(e) } // todo: custom errors diff --git a/027-ok-guards/ok-guards.go b/027-ok-guards/ok-guards.go index 6e5b577..fc5e540 100644 --- a/027-ok-guards/ok-guards.go +++ b/027-ok-guards/ok-guards.go @@ -5,18 +5,18 @@ package main import "fmt" func main() { - x := make(map[string]string) - x["here"] = "yes" + x := make(map[string]string) + x["here"] = "yes" - if name, ok := x["?"]; ok { - fmt.Println(name) - } else { - fmt.Println("miss") - } + if name, ok := x["?"]; ok { + fmt.Println(name) + } else { + fmt.Println("miss") + } - if name, ok := x["here"]; ok { - fmt.Println(name) - } + if name, ok := x["here"]; ok { + fmt.Println(name) + } } // todo: note about use with errors diff --git a/029-concurrent-goroutines/concurrent-goroutines.go b/029-concurrent-goroutines/concurrent-goroutines.go index 64235fa..28d05a5 100644 --- a/029-concurrent-goroutines/concurrent-goroutines.go +++ b/029-concurrent-goroutines/concurrent-goroutines.go @@ -9,18 +9,18 @@ import "fmt" func f(n int) { for i := 0; i < 10; i++ { fmt.Println(n, ":", i) - time.Sleep(time.Millisecond * time.Duration(rand.Intn(150))) + time.Sleep(time.Millisecond * time.Duration(rand.Intn(150))) } } func main() { for i := 0; i < 5; i++ { - f(i) - } - fmt.Println() - for i := 0; i < 5; i++ { - go f(i) - } + f(i) + } + fmt.Println() + for i := 0; i < 5; i++ { + go f(i) + } var input string fmt.Scanln(&input) } diff --git a/030-channels/channels.go b/030-channels/channels.go index 4c2f135..3af0cd0 100644 --- a/030-channels/channels.go +++ b/030-channels/channels.go @@ -6,7 +6,7 @@ import "fmt" func main() { var messages chan string = make(chan string) - go func() { messages <- "ping"}() - msg := <- messages - fmt.Println(msg) + go func() { messages <- "ping" }() + msg := <-messages + fmt.Println(msg) } diff --git a/031-channel-buffering/channel-buffering.go b/031-channel-buffering/channel-buffering.go index 9126111..98b774b 100644 --- a/031-channel-buffering/channel-buffering.go +++ b/031-channel-buffering/channel-buffering.go @@ -7,6 +7,6 @@ import "fmt" func main() { var messages chan string = make(chan string, 1) messages <- "ping" - msg := <- messages - fmt.Println(msg) + msg := <-messages + fmt.Println(msg) } diff --git a/032-channel-directions/channel-directions.go b/032-channel-directions/channel-directions.go index 91e3096..6d336fa 100644 --- a/032-channel-directions/channel-directions.go +++ b/032-channel-directions/channel-directions.go @@ -12,14 +12,14 @@ func pinger(pings chan<- string) { func ponger(pings <-chan string, pongs chan<- string) { for { - <- pings - pongs <- "pong" + <-pings + pongs <- "pong" } } func printer(pongs <-chan string) { for { - msg := <- pongs + msg := <-pongs fmt.Println(msg) } } @@ -32,6 +32,6 @@ func main() { go ponger(pings, pongs) go printer(pongs) - var input string + var input string fmt.Scanln(&input) } diff --git a/033-synchronization/synchronization.go b/033-synchronization/synchronization.go index de17241..c8abb1e 100644 --- a/033-synchronization/synchronization.go +++ b/033-synchronization/synchronization.go @@ -4,29 +4,29 @@ // accross goroutines. Here's an example of waiting // for another goroutine to finish. -package main - -import "fmt" +package main + +import "fmt" import "time" // The `done` channel will be used for // synchronization. -func worker(done chan<- bool) { - fmt.Print("Working...") +func worker(done chan<- bool) { + fmt.Print("Working...") time.Sleep(time.Second) fmt.Println(" done") // Send a value to notify that the work is done. - done <- true + done <- true } func main() { // Start a worker goroutine, give it the channel to // notify on. - done := make(chan bool, 1) - go worker(done) - + done := make(chan bool, 1) + go worker(done) + // Block until we can receive a value from the worker // over the channel. - <- done -} + <-done +} diff --git a/034-select/select.go b/034-select/select.go index 5c6398e..0007a65 100644 --- a/034-select/select.go +++ b/034-select/select.go @@ -8,27 +8,27 @@ import "fmt" func main() { c1 := make(chan string) c2 := make(chan string) - d := make(chan bool, 1) + d := make(chan bool, 1) go func() { time.Sleep(time.Second * 1) - c1 <- "from 1" + c1 <- "from 1" }() go func() { time.Sleep(time.Second * 2) - c2 <- "from 2" + c2 <- "from 2" }() go func() { - for i :=0; i < 2; i ++ { + for i := 0; i < 2; i++ { select { - case msg1 := <- c1: + case msg1 := <-c1: fmt.Println(msg1) - case msg2 := <- c2: + case msg2 := <-c2: fmt.Println(msg2) } } - d <- true + d <- true }() - <- d + <-d } diff --git a/035-timeouts/timeouts.go b/035-timeouts/timeouts.go index 8c5f43e..5733d32 100644 --- a/035-timeouts/timeouts.go +++ b/035-timeouts/timeouts.go @@ -7,21 +7,21 @@ import "fmt" func main() { c := make(chan string) - d := make(chan bool, 1) + d := make(chan bool, 1) go func() { time.Sleep(time.Millisecond * 1500) - c <- "ready" + c <- "ready" }() go func() { - select { - case msg := <- c: - fmt.Println(msg) - case <- time.After(time.Millisecond * 1000): - fmt.Println("timeout") - } - d <- true - }() - <- d + select { + case msg := <-c: + fmt.Println(msg) + case <-time.After(time.Millisecond * 1000): + fmt.Println("timeout") + } + d <- true + }() + <-d } diff --git a/036-scatter-gather/081-scatter-gather.go b/036-scatter-gather/081-scatter-gather.go index fc0c3a9..6b814d8 100644 --- a/036-scatter-gather/081-scatter-gather.go +++ b/036-scatter-gather/081-scatter-gather.go @@ -8,19 +8,19 @@ import "math/rand" import "fmt" func main() { - times := new([20]int) - wait := new(sync.WaitGroup) - for i := 0; i < 20; i++ { - n := i - wait.Add(1) - go func() { - opTime := rand.Intn(2000) - time.Sleep(time.Duration(opTime) * time.Millisecond) - fmt.Println(n) - times[n] = opTime - wait.Done() - }() - } - wait.Wait() - fmt.Println(*times) + times := new([20]int) + wait := new(sync.WaitGroup) + for i := 0; i < 20; i++ { + n := i + wait.Add(1) + go func() { + opTime := rand.Intn(2000) + time.Sleep(time.Duration(opTime) * time.Millisecond) + fmt.Println(n) + times[n] = opTime + wait.Done() + }() + } + wait.Wait() + fmt.Println(*times) } diff --git a/037-rate-limiting/rate-limiting.go b/037-rate-limiting/rate-limiting.go index c8d1879..5ffd5ab 100644 --- a/037-rate-limiting/rate-limiting.go +++ b/037-rate-limiting/rate-limiting.go @@ -5,10 +5,10 @@ import "time" import "fmt" func main() { - throttle := time.Tick(time.Millisecond * 200) - for { - <- throttle - go fmt.Println("rate-limited action") + throttle := time.Tick(time.Millisecond * 200) + for { + <-throttle + go fmt.Println("rate-limited action") } } diff --git a/039-timers/timers.go b/039-timers/timers.go index 536a5a3..b5e3169 100644 --- a/039-timers/timers.go +++ b/039-timers/timers.go @@ -6,17 +6,17 @@ import "time" import "fmt" func main() { - timer1 := time.NewTimer(time.Second) - <- timer1.C - fmt.Println("Timer 1 expired") - stop1 := timer1.Stop() - fmt.Println("Timer 2 stopped:", stop1) + timer1 := time.NewTimer(time.Second) + <-timer1.C + fmt.Println("Timer 1 expired") + stop1 := timer1.Stop() + fmt.Println("Timer 2 stopped:", stop1) - timer2 := time.NewTimer(time.Second) - go func() { - <- timer2.C - fmt.Println("Timer 2 expired") - }() - stop2 := timer2.Stop() - fmt.Println("Timer 2 stopped:", stop2) + timer2 := time.NewTimer(time.Second) + go func() { + <-timer2.C + fmt.Println("Timer 2 expired") + }() + stop2 := timer2.Stop() + fmt.Println("Timer 2 stopped:", stop2) } diff --git a/041-sorting/sorting.go b/041-sorting/sorting.go index 98d3085..b3b766c 100644 --- a/041-sorting/sorting.go +++ b/041-sorting/sorting.go @@ -3,25 +3,25 @@ // 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" +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) + 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) + sort.Ints(ints) fmt.Println(ints) // Check if a slice is in sorted order. - s := sort.IntsAreSorted(ints) + s := sort.IntsAreSorted(ints) fmt.Println(s) } diff --git a/042-sorting-by-functions/sorting-by-functions.go b/042-sorting-by-functions/sorting-by-functions.go index 4c8c317..734327d 100644 --- a/042-sorting-by-functions/sorting-by-functions.go +++ b/042-sorting-by-functions/sorting-by-functions.go @@ -2,14 +2,18 @@ package main -import ("fmt" ; "sort") +import ( + "fmt" + "sort" +) -type Person struct { +type Person struct { Name string - Age int + Age int } type ByName []Person + func (this ByName) Len() int { return len(this) } @@ -21,8 +25,9 @@ func (this ByName) Swap(i, j int) { } type ByAge []Person + func (this ByAge) Len() int { - return len(this) + return len(this) } func (this ByAge) Less(i, j int) bool { return this[i].Age < this[j].Age @@ -33,15 +38,15 @@ func (this ByAge) Swap(i, j int) { func main() { kids := []Person{ - {"Jack", 10}, + {"Jack", 10}, {"Jill", 9}, - {"Bob", 12}, + {"Bob", 12}, } fmt.Println("Original:", kids) - - sort.Sort(ByName(kids)) - fmt.Println("ByName: ", kids) - sort.Sort(ByAge(kids)) - fmt.Println("ByAge: ", kids) + sort.Sort(ByName(kids)) + fmt.Println("ByName: ", kids) + + sort.Sort(ByAge(kids)) + fmt.Println("ByAge: ", kids) } diff --git a/043-collection-functions/collection-functions.go b/043-collection-functions/collection-functions.go index 20cb9e6..662ff19 100644 --- a/043-collection-functions/collection-functions.go +++ b/043-collection-functions/collection-functions.go @@ -1,95 +1,95 @@ // ## Collection Functions -package main +package main import "strings" import "fmt" func Index(elems []string, val string) int { - for i, v := range elems { - if v == val { - return i - } - } - return -1 + for i, v := range elems { + if v == val { + return i + } + } + return -1 } func Include(elems []string, val string) bool { - return Index(elems, val) >= 0 + return Index(elems, val) >= 0 } -func Any(elems []string, f func(string)bool) bool { - for _, v := range elems { - if f(v) { - return true - } - } - return false +func Any(elems []string, f func(string) bool) bool { + for _, v := range elems { + if f(v) { + return true + } + } + return false } -func All(elems []string, f func(string)bool) bool { - for _, v := range elems { - if !f(v) { - return false - } - } - return true +func All(elems []string, f func(string) bool) bool { + for _, v := range elems { + if !f(v) { + return false + } + } + return true } -func Filter(elems []string, f func(string)bool) []string { - filtered := []string{} - for _, v := range elems { - if f(v) { - filtered = append(filtered, v) - } - } - return filtered +func Filter(elems []string, f func(string) bool) []string { + filtered := []string{} + for _, v := range elems { + if f(v) { + filtered = append(filtered, v) + } + } + return filtered } -func Map(elems []string, f func(string)string) []string { - mapped := make([]string, len(elems)) - for i, v := range elems { - mapped[i] = f(v) - } - return mapped +func Map(elems []string, f func(string) string) []string { + mapped := make([]string, len(elems)) + for i, v := range elems { + mapped[i] = f(v) + } + return mapped } func main() { - var elems = []string{"peach", "apple", "pear", "banana"} + var elems = []string{"peach", "apple", "pear", "banana"} - fmt.Println(Index(elems, "pear")) - fmt.Println(Index(elems, "grape")) - fmt.Println() + fmt.Println(Index(elems, "pear")) + fmt.Println(Index(elems, "grape")) + fmt.Println() - fmt.Println(Include(elems, "pear")) - fmt.Println(Include(elems, "grape")) - fmt.Println() + fmt.Println(Include(elems, "pear")) + fmt.Println(Include(elems, "grape")) + fmt.Println() - fmt.Println(Any(elems, func(v string) bool { - return strings.HasPrefix(v, "p") - })) - fmt.Println(Any(elems, func(v string) bool { - return strings.HasPrefix(v, "g") - })) - fmt.Println() + fmt.Println(Any(elems, func(v string) bool { + return strings.HasPrefix(v, "p") + })) + fmt.Println(Any(elems, func(v string) bool { + return strings.HasPrefix(v, "g") + })) + fmt.Println() - fmt.Println(All(elems, func(v string) bool { - return strings.Contains(v, "a") - })) - fmt.Println(All(elems, func(v string) bool { - return strings.Contains(v, "p") - })) - fmt.Println() + fmt.Println(All(elems, func(v string) bool { + return strings.Contains(v, "a") + })) + fmt.Println(All(elems, func(v string) bool { + return strings.Contains(v, "p") + })) + fmt.Println() - fmt.Println(Filter(elems, func(v string) bool { - return strings.Contains(v, "p") - })) - fmt.Println() - - fmt.Println(Map(elems, func(s string) string { - return strings.ToUpper(s) - })) - fmt.Println() + fmt.Println(Filter(elems, func(v string) bool { + return strings.Contains(v, "p") + })) + fmt.Println() + + fmt.Println(Map(elems, func(s string) string { + return strings.ToUpper(s) + })) + fmt.Println() } // todo: generics diff --git a/044-string-functions/string-functions.go b/044-string-functions/string-functions.go index 15e67de..c9dd0b0 100644 --- a/044-string-functions/string-functions.go +++ b/044-string-functions/string-functions.go @@ -6,7 +6,7 @@ import "strings" import "fm" func p(o interface{}) { - fmt.Println(o) + fmt.Println(o) } func main() { @@ -15,8 +15,8 @@ func main() { p(strings.Count("test", "t")) p(strings.HasPrefix("test", "te")) p(strings.HasSuffix("test", "st")) - p(strings.Index("test", "e")) - p(strings.Join([]string{"a","b"}, "-")) + p(strings.Index("test", "e")) + p(strings.Join([]string{"a", "b"}, "-")) p(strings.Repeat("a", 5)) p(strings.Replace("foo", "o", "0", -1)) p(strings.Replace("foo", "o", "0", 1)) diff --git a/046-regexs/regexs.go b/046-regexs/regexs.go index 0e2bfd7..29c014b 100644 --- a/046-regexs/regexs.go +++ b/046-regexs/regexs.go @@ -6,14 +6,14 @@ import "regexp" import "fmt" func main() { - m1, _ := regexp.MatchString("p[a-z]+ch", "apple") - m2, _ := regexp.MatchString("p[a-z]+ch", "peach") - fmt.Println(m1) - fmt.Println(m2) + m1, _ := regexp.MatchString("p[a-z]+ch", "apple") + m2, _ := regexp.MatchString("p[a-z]+ch", "peach") + fmt.Println(m1) + fmt.Println(m2) - r1, _ := regexp.Compile("p[a-z]+ch") - fmt.Println(r1.MatchString("apple")) - fmt.Println(r1.MatchString("peach")) + r1, _ := regexp.Compile("p[a-z]+ch") + fmt.Println(r1.MatchString("apple")) + fmt.Println(r1.MatchString("peach")) } // todo: more diff --git a/047-bytes/bytes.go b/047-bytes/bytes.go index 3eab9a2..5e337e9 100644 --- a/047-bytes/bytes.go +++ b/047-bytes/bytes.go @@ -5,8 +5,8 @@ package main import "fmt" func main() { - arr := []byte("some bytes") - str := string([]byte{'a', ' ', 's', 't', 'r', 'i', 'n', 'g'}) - fmt.Println(arr) - fmt.Println(str) + arr := []byte("some bytes") + str := string([]byte{'a', ' ', 's', 't', 'r', 'i', 'n', 'g'}) + fmt.Println(arr) + fmt.Println(str) } diff --git a/048-json/json.go b/048-json/json.go index 927aaf9..1cdf6d7 100644 --- a/048-json/json.go +++ b/048-json/json.go @@ -6,32 +6,34 @@ import "encoding/json" import "fmt" func main() { - // data to bytes/string - bol, _ := json.Marshal(true) - fmt.Println(string(bol)) + // data to bytes/string + bol, _ := json.Marshal(true) + fmt.Println(string(bol)) - num, _ := json.Marshal(1) - fmt.Println(string(num)) + num, _ := json.Marshal(1) + fmt.Println(string(num)) - str, _ := json.Marshal("gopher") - fmt.Println(string(str)) - - arr, _ := json.Marshal([]string{"apple", "peach", "pear"}) - fmt.Println(string(arr)) + str, _ := json.Marshal("gopher") + fmt.Println(string(str)) - hsh, _ := json.Marshal(map[string]int{"apple": 5, "lettuce": 7}) - fmt.Println(string(hsh)) + arr, _ := json.Marshal([]string{"apple", "peach", "pear"}) + fmt.Println(string(arr)) - // string to data - byt := []byte(`{"Name":"Wednesday","Age":6,"Parents":["Gomez","Morticia"]}`) - var dat map[string]interface{} - err := json.Unmarshal(byt, &dat) - if err != nil { panic(err) } - fmt.Println(dat) + hsh, _ := json.Marshal(map[string]int{"apple": 5, "lettuce": 7}) + fmt.Println(string(hsh)) - name := dat["Name"].(string) - fmt.Println(name) + // string to data + byt := []byte(`{"Name":"Wednesday","Age":6,"Parents":["Gomez","Morticia"]}`) + var dat map[string]interface{} + err := json.Unmarshal(byt, &dat) + if err != nil { + panic(err) + } + fmt.Println(dat) - parents := dat["Parents"].([]interface{}) - fmt.Println(parents) + name := dat["Name"].(string) + fmt.Println(name) + + parents := dat["Parents"].([]interface{}) + fmt.Println(parents) } diff --git a/050-epochs/epochs.go b/050-epochs/epochs.go index c5cad5c..d489859 100644 --- a/050-epochs/epochs.go +++ b/050-epochs/epochs.go @@ -1,19 +1,19 @@ // ## Epochs -package main - +package main + import "time" func main() { // Use `time.Now` with `Unix` or `UnixNane` to get // elapsed time since the Unix epoch. - now := time.Now() - secs := now.Unix() - nanos := now.UnixNano() + now := time.Now() + secs := now.Unix() + nanos := now.UnixNano() // There is no `UnixMillis`. - millis := nanos / 1000000 - println("Secs: ", secs) - println("Millis:", millis) - println("Nanos: ", nanos) + millis := nanos / 1000000 + println("Secs: ", secs) + println("Millis:", millis) + println("Nanos: ", nanos) } diff --git a/051-elapsed-time/elapsed-time.go b/051-elapsed-time/elapsed-time.go index 66345a1..ae8026d 100644 --- a/051-elapsed-time/elapsed-time.go +++ b/051-elapsed-time/elapsed-time.go @@ -6,7 +6,7 @@ import "time" import "fmt" func main() { - start := time.Now() - time.Sleep(3 * time.Second) - fmt.Println(time.Since(start)) + start := time.Now() + time.Sleep(3 * time.Second) + fmt.Println(time.Since(start)) } diff --git a/052-random-numbers/random-numbers.go b/052-random-numbers/random-numbers.go index e857c9b..38f6aac 100644 --- a/052-random-numbers/random-numbers.go +++ b/052-random-numbers/random-numbers.go @@ -1,7 +1,7 @@ // Random Numbers -package main - +package main + // The `math/rand` package provides psuedo-random // numbers. import "math/rand" @@ -10,30 +10,30 @@ import "fmt" func main() { // For example, `rand.Intn` returns a random `int` n, // `0 <= n < 100`. - fmt.Print(rand.Intn(100), ",") - fmt.Print(rand.Intn(100)) + fmt.Print(rand.Intn(100), ",") + fmt.Print(rand.Intn(100)) fmt.Println() // `rand.Float64` returns a `float64` `f`, // `0.0 <= f < 1.0`. - fmt.Println(rand.Float64()) - + fmt.Println(rand.Float64()) + // To make the psuedo-random generator deterministic, // give it a well-known seed. - s1 := rand.NewSource(42) - r1 := rand.New(s1) - - // Call the resulting `rand.Source` just like the - // functions on the `rand` package. - fmt.Print(r1.Intn(100), ",") - fmt.Print(r1.Intn(100)) + s1 := rand.NewSource(42) + r1 := rand.New(s1) + + // Call the resulting `rand.Source` just like the + // functions on the `rand` package. + fmt.Print(r1.Intn(100), ",") + fmt.Print(r1.Intn(100)) fmt.Println() // If you seed a source with the same number, it // produces the same sequence of random numbers. - s2 := rand.NewSource(42) - r2 := rand.New(s2) - fmt.Print(r2.Intn(100), ",") - fmt.Print(r2.Intn(100)) - fmt.Println() + s2 := rand.NewSource(42) + r2 := rand.New(s2) + fmt.Print(r2.Intn(100), ",") + fmt.Print(r2.Intn(100)) + fmt.Println() } diff --git a/053-number-parsing/number-parsing.go b/053-number-parsing/number-parsing.go index de07b4a..bdda3a0 100644 --- a/053-number-parsing/number-parsing.go +++ b/053-number-parsing/number-parsing.go @@ -8,23 +8,23 @@ import "fmt" func main() { // `64` tells how many bits of precision to parse. - f, _ := strconv.ParseFloat("1.234", 64) + f, _ := strconv.ParseFloat("1.234", 64) fmt.Println(f) - + // `0` means infer the base from the string. // `64` requires that the result fit in 64 bits. - i, _ := strconv.ParseInt("123", 0, 64) - println(i) + i, _ := strconv.ParseInt("123", 0, 64) + println(i) // `ParseInt` will recognize hex-formatted numbers. - d, _ := strconv.ParseInt("0x1b3e", 0, 64) + d, _ := strconv.ParseInt("0x1b3e", 0, 64) println(d) // `Atoi` is a convenienice function for `int` parsing. - k, _ := strconv.Atoi("456") + k, _ := strconv.Atoi("456") println(k) // Parse functions return an error on bad input. - _, e := strconv.Atoi("wat") + _, e := strconv.Atoi("wat") fmt.Println(e) } diff --git a/056-reading-files/reading-files.go b/056-reading-files/reading-files.go index 720a705..c713019 100644 --- a/056-reading-files/reading-files.go +++ b/056-reading-files/reading-files.go @@ -13,5 +13,4 @@ func main() { fmt.Print(string(contents)) } - // todo: streaming reads diff --git a/057-writing-files/writing-files.go b/057-writing-files/writing-files.go index e1c4d08..58ebd98 100644 --- a/057-writing-files/writing-files.go +++ b/057-writing-files/writing-files.go @@ -5,7 +5,7 @@ package main import "os" func main() { - file, err := os.Create("xx-file-write.txt") + file, err := os.Create("xx-file-write.txt") if err != nil { panic(err) } diff --git a/059-command-line-arguments/command-line-arguments.go b/059-command-line-arguments/command-line-arguments.go index c5bfdda..0b0acf8 100644 --- a/059-command-line-arguments/command-line-arguments.go +++ b/059-command-line-arguments/command-line-arguments.go @@ -2,25 +2,24 @@ // Use `os.Args` to access command-line arguments and // the name of the program. -package main - +package main + import "os" import "fmt" func main() { // `os.Args` includes the program name as the first // value. - argsWithProg := os.Args - argsWithoutProg := os.Args[1:] + argsWithProg := os.Args + argsWithoutProg := os.Args[1:] // `Args` are a slice, you can get individual args // with normal indexing. - arg := os.Args[3] - + arg := os.Args[3] - fmt.Println(argsWithProg) - fmt.Println(argsWithoutProg) - fmt.Println(arg) + fmt.Println(argsWithProg) + fmt.Println(argsWithoutProg) + fmt.Println(arg) } // todo: discuss building before here diff --git a/060-command-line-flags/command-line-flags.go b/060-command-line-flags/command-line-flags.go index 64a8092..89b8b4e 100644 --- a/060-command-line-flags/command-line-flags.go +++ b/060-command-line-flags/command-line-flags.go @@ -8,11 +8,11 @@ import "fmt" func main() { maxp := flag.Int("repeat", 3, "time to repeat args") flag.Parse() - for i := 0; i < *maxp; i++ { - for _, arg := range flag.Args() { - fmt.Println(arg) - } - } + for i := 0; i < *maxp; i++ { + for _, arg := range flag.Args() { + fmt.Println(arg) + } + } } // todo: multiple flags diff --git a/061-environment-variables/environment-variables.go b/061-environment-variables/environment-variables.go index 31ecc64..b8cfff0 100644 --- a/061-environment-variables/environment-variables.go +++ b/061-environment-variables/environment-variables.go @@ -1,6 +1,6 @@ // ## Environment Variables -package main +package main // Use the `os` package to list, set, and get // environment variables. @@ -12,15 +12,15 @@ func main() { // `os.Environ` returns a slice of strings in the form // `KEY=value`. You can `strings.Split` them to get // the key and value. - for _, e := range os.Environ() { - pair := strings.Split(e, "=") - fmt.Println(pair[0]) + for _, e := range os.Environ() { + pair := strings.Split(e, "=") + fmt.Println(pair[0]) } fmt.Println() // `Setenv` sets a given key, value pair. // `Getenv` returns the value at key, or an empty // string if the key isn't present. - os.Setenv("FOO", "bar") - fmt.Println(os.Getenv("FOO")) -} + os.Setenv("FOO", "bar") + fmt.Println(os.Getenv("FOO")) +} diff --git a/062-spawning-processes/spawning-processes.go b/062-spawning-processes/spawning-processes.go index e9ea292..44ac4b7 100644 --- a/062-spawning-processes/spawning-processes.go +++ b/062-spawning-processes/spawning-processes.go @@ -6,13 +6,13 @@ import "os/exec" import "fmt" func main() { - cmd := exec.Command("ls", "-a", "-l") - out, err := cmd.Output() - if err != nil { - panic(err) - } - fmt.Println("Files:") - fmt.Print(string(out)) + cmd := exec.Command("ls", "-a", "-l") + out, err := cmd.Output() + if err != nil { + panic(err) + } + fmt.Println("Files:") + fmt.Print(string(out)) } // todo: full command lines with bash diff --git a/063-execing-processes/execing-processes.go b/063-execing-processes/execing-processes.go index dd6c9d8..1603b31 100644 --- a/063-execing-processes/execing-processes.go +++ b/063-execing-processes/execing-processes.go @@ -7,14 +7,14 @@ import "os" import "os/exec" func main() { - binary, lookErr := exec.LookPath("ls") - if lookErr != nil { - panic(lookErr) - } - execErr := syscall.Exec(binary, []string{"-a", "-l", "-h"}, os.Environ()) - if execErr != nil { - panic(execErr) - } + binary, lookErr := exec.LookPath("ls") + if lookErr != nil { + panic(lookErr) + } + execErr := syscall.Exec(binary, []string{"-a", "-l", "-h"}, os.Environ()) + if execErr != nil { + panic(execErr) + } } // todo: note lack of fork diff --git a/064-signals/signals.go b/064-signals/signals.go index 168d8b4..7dfa0ac 100644 --- a/064-signals/signals.go +++ b/064-signals/signals.go @@ -2,21 +2,26 @@ package main -import ("fmt"; "os"; "os/signal"; "syscall") +import ( + "fmt" + "os" + "os/signal" + "syscall" +) func main() { - c := make(chan os.Signal, 1) - d := make(chan bool, 1) + c := make(chan os.Signal, 1) + d := make(chan bool, 1) - signal.Notify(c, syscall.SIGINT) - go func(){ - sig := <- c - fmt.Println() - fmt.Println(sig) - d <- true - }() - fmt.Println("Awaiting signal") - <- d + signal.Notify(c, syscall.SIGINT) + go func() { + sig := <-c + fmt.Println() + fmt.Println(sig) + d <- true + }() + fmt.Println("Awaiting signal") + <-d } // todo: sending signals? diff --git a/065-exit/exit.go b/065-exit/exit.go index d8dec26..ad4c531 100644 --- a/065-exit/exit.go +++ b/065-exit/exit.go @@ -1,16 +1,16 @@ // Exit -package main +package main // Use `os.Exit` to immediatly exit with a given // status. -import "os" +import "os" func main() { // This `println` will never be reached because the // exit is immediate. - defer println("!") - os.Exit(3) + defer println("!") + os.Exit(3) } // todo: discuss building before getting here diff --git a/066-http-client/http-client.go b/066-http-client/http-client.go index 79a043b..bca3223 100644 --- a/066-http-client/http-client.go +++ b/066-http-client/http-client.go @@ -7,11 +7,11 @@ import "io/ioutil" import "fmt" func main() { - resp, err := http.Get("http://127.0.0.1:5000/") - if err != nil { - panic(err) - } - defer resp.Body.Close() - body, err := ioutil.ReadAll(resp.Body) - fmt.Print(string(body)) + resp, err := http.Get("http://127.0.0.1:5000/") + if err != nil { + panic(err) + } + defer resp.Body.Close() + body, err := ioutil.ReadAll(resp.Body) + fmt.Print(string(body)) } diff --git a/067-https-client/https-client.go b/067-https-client/https-client.go index f5c8d08..7b58a24 100644 --- a/067-https-client/https-client.go +++ b/067-https-client/https-client.go @@ -8,13 +8,15 @@ import "io/ioutil" import "fmt" func main() { - tr := &http.Transport{ - TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, - } - client := &http.Client{Transport: tr} - resp, err := client.Get("https://127.0.0.1:5000/") - if err != nil { panic(err) } - defer resp.Body.Close() - body, _ := ioutil.ReadAll(resp.Body) - fmt.Print(string(body)) + tr := &http.Transport{ + TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, + } + client := &http.Client{Transport: tr} + resp, err := client.Get("https://127.0.0.1:5000/") + if err != nil { + panic(err) + } + defer resp.Body.Close() + body, _ := ioutil.ReadAll(resp.Body) + fmt.Print(string(body)) } diff --git a/068-redis/redis.go b/068-redis/redis.go index b58c83f..da15664 100644 --- a/068-redis/redis.go +++ b/068-redis/redis.go @@ -7,60 +7,70 @@ import "time" import "fmt" func main() { - // initialize - conf := redis.DefaultConfig() - client := redis.NewClient(conf) - fmt.Println(conf) - fmt.Println(client) + // initialize + conf := redis.DefaultConfig() + client := redis.NewClient(conf) + fmt.Println(conf) + fmt.Println(client) - // set & get - setRep := client.Set("foo", "bar") - if setRep.Err != nil { panic(setRep.Err) } - fmt.Println(setRep) - getRep := client.Get("foo") - if getRep.Err != nil { panic(getRep.Err) } - getStr, _ := getRep.Str() - fmt.Println(getRep) - fmt.Println(getStr) + // set & get + setRep := client.Set("foo", "bar") + if setRep.Err != nil { + panic(setRep.Err) + } + fmt.Println(setRep) + getRep := client.Get("foo") + if getRep.Err != nil { + panic(getRep.Err) + } + getStr, _ := getRep.Str() + fmt.Println(getRep) + fmt.Println(getStr) - // varadic calls - client.Set("foo1", "bar1") - client.Set("foo2", "bar2") - client.Set("foo3", "bar3") - mgetRep := client.Mget("foo1", "foo2", "foo3") - fmt.Println(mgetRep) + // varadic calls + client.Set("foo1", "bar1") + client.Set("foo2", "bar2") + client.Set("foo3", "bar3") + mgetRep := client.Mget("foo1", "foo2", "foo3") + fmt.Println(mgetRep) // multi calls - mcallRep := client.MultiCall(func(mc *redis.MultiCall) { - mc.Set("k1", "v1") - mc.Get("k1") - }) - if mcallRep.Err != nil { panic(mcallRep.Err) } - mcallVal, _ := mcallRep.Elems[1].Str() - fmt.Println(mcallVal) + mcallRep := client.MultiCall(func(mc *redis.MultiCall) { + mc.Set("k1", "v1") + mc.Get("k1") + }) + if mcallRep.Err != nil { + panic(mcallRep.Err) + } + mcallVal, _ := mcallRep.Elems[1].Str() + fmt.Println(mcallVal) - // transactional calls - tranRep := client.Transaction(func(mc *redis.MultiCall) { - mc.Set("k2", "v2") - mc.Get("k2") - }) - if tranRep.Err != nil { panic(tranRep.Err) } - tranStr, _ := tranRep.Elems[1].Str() - fmt.Println(tranStr) + // transactional calls + tranRep := client.Transaction(func(mc *redis.MultiCall) { + mc.Set("k2", "v2") + mc.Get("k2") + }) + if tranRep.Err != nil { + panic(tranRep.Err) + } + tranStr, _ := tranRep.Elems[1].Str() + fmt.Println(tranStr) - // pubsub - msgHdlr := func(msg *redis.Message) { - fmt.Println(msg) - } - sub, subErr := client.Subscription(msgHdlr) - if subErr != nil { panic(subErr) } - defer sub.Close() - sub.Subscribe("chan1", "chan2") - sub.Psubscribe("chan*") - client.Publish("chan1", "foo") - sub.Unsubscribe("chan1") - client.Publish("chan2", "bar") - time.Sleep(time.Second) + // pubsub + msgHdlr := func(msg *redis.Message) { + fmt.Println(msg) + } + sub, subErr := client.Subscription(msgHdlr) + if subErr != nil { + panic(subErr) + } + defer sub.Close() + sub.Subscribe("chan1", "chan2") + sub.Psubscribe("chan*") + client.Publish("chan1", "foo") + sub.Unsubscribe("chan1") + client.Publish("chan2", "bar") + time.Sleep(time.Second) } // todo: connection pooling & concurrency? diff --git a/069-postgres/postgres.go b/069-postgres/postgres.go index 62dbcb4..54a09f3 100644 --- a/069-postgres/postgres.go +++ b/069-postgres/postgres.go @@ -8,46 +8,60 @@ import "time" import "fmt" func main() { - db, openErr := sql.Open("postgres", "dbname=gobyexample sslmode=disable") - if openErr != nil { panic(openErr) } - defer db.Close() - fmt.Println(db) - - createRep, createErr := db.Exec("CREATE TABLE items (a int, b float, c boolean, d text, e timestamp with time zone)") - if createErr != nil { panic(createErr) } - fmt.Println(createRep) - - insertRep, insertErr := db.Exec("INSERT INTO items VALUES (1, 2.0, false, 'string', '2000-01-01T01:02:03Z')") - if insertErr != nil { panic(insertErr) } - fmt.Println(insertRep) - - t1, _ := time.Parse(time.RFC3339, "2000-04-08T03:02:01Z") - t2, _ := time.Parse(time.RFC3339, "2007-03-02T10:15:45Z") - minsertRep, minsertErr := db.Exec("Insert INTO items VALUES ($1, $2, $3, $4, $5), ($6, $7, $8, $9, $10)", - 3, 7.0, true, "more", t1, - 5, 1.0, false, "less", t2) - if minsertErr != nil { panic(minsertErr) } - num, _ := minsertRep.RowsAffected() - fmt.Println(num) - - rows, selectErr := db.Query("SELECT * FROM items") - if selectErr != nil { panic(selectErr) } - defer rows.Close() - for rows.Next() { - var r1 int - var r2 float64 - var r3 bool - var r4 string - var r5 time.Time - rows.Scan(&r1, &r2, &r3, &r4, &r5) - fmt.Println(r1, r2, r3, r4, r5) + db, openErr := sql.Open("postgres", "dbname=gobyexample sslmode=disable") + if openErr != nil { + panic(openErr) } - rowsErr := rows.Err() - if rowsErr != nil { panic(rowsErr) } - - dropRep, dropErr := db.Exec("DROP TABLE items") - if dropErr != nil { panic(dropErr) } - fmt.Println(dropRep) + defer db.Close() + fmt.Println(db) + + createRep, createErr := db.Exec("CREATE TABLE items (a int, b float, c boolean, d text, e timestamp with time zone)") + if createErr != nil { + panic(createErr) + } + fmt.Println(createRep) + + insertRep, insertErr := db.Exec("INSERT INTO items VALUES (1, 2.0, false, 'string', '2000-01-01T01:02:03Z')") + if insertErr != nil { + panic(insertErr) + } + fmt.Println(insertRep) + + t1, _ := time.Parse(time.RFC3339, "2000-04-08T03:02:01Z") + t2, _ := time.Parse(time.RFC3339, "2007-03-02T10:15:45Z") + minsertRep, minsertErr := db.Exec("Insert INTO items VALUES ($1, $2, $3, $4, $5), ($6, $7, $8, $9, $10)", + 3, 7.0, true, "more", t1, + 5, 1.0, false, "less", t2) + if minsertErr != nil { + panic(minsertErr) + } + num, _ := minsertRep.RowsAffected() + fmt.Println(num) + + rows, selectErr := db.Query("SELECT * FROM items") + if selectErr != nil { + panic(selectErr) + } + defer rows.Close() + for rows.Next() { + var r1 int + var r2 float64 + var r3 bool + var r4 string + var r5 time.Time + rows.Scan(&r1, &r2, &r3, &r4, &r5) + fmt.Println(r1, r2, r3, r4, r5) + } + rowsErr := rows.Err() + if rowsErr != nil { + panic(rowsErr) + } + + dropRep, dropErr := db.Exec("DROP TABLE items") + if dropErr != nil { + panic(dropErr) + } + fmt.Println(dropRep) } // todo: connection pooling & concurrency diff --git a/070-sending-email/sending-email.go b/070-sending-email/sending-email.go index 94abd29..490a86a 100644 --- a/070-sending-email/sending-email.go +++ b/070-sending-email/sending-email.go @@ -5,21 +5,23 @@ package main import "net/smtp" func main() { - auth := smtp.PlainAuth( - "", - "mark@heroku.com", - "xxx", - "smtp.gmail.com", - ) + auth := smtp.PlainAuth( + "", + "mark@heroku.com", + "xxx", + "smtp.gmail.com", + ) - err := smtp.SendMail( - "smtp.gmail.com:25", - auth, - "mark+sent@heroku.com", - []string{"mark@heroku.com"}, - []byte("nThe body."), - ) - if err != nil { panic(err) } + err := smtp.SendMail( + "smtp.gmail.com:25", + auth, + "mark+sent@heroku.com", + []string{"mark@heroku.com"}, + []byte("nThe body."), + ) + if err != nil { + panic(err) + } } // todo: missing subject, cc, bcc diff --git a/071-hello-web/hello-web.go b/071-hello-web/hello-web.go index d5a65d7..a6b6819 100644 --- a/071-hello-web/hello-web.go +++ b/071-hello-web/hello-web.go @@ -6,7 +6,7 @@ import "net/http" func hello(res http.ResponseWriter, req *http.Request) { res.Header().Set("Content-Type", "text/plain") - res.Write([]byte("Hello web\n")) + res.Write([]byte("Hello web\n")) } func main() { diff --git a/072-responses/responses.go b/072-responses/responses.go index bb9671e..458aeb1 100644 --- a/072-responses/responses.go +++ b/072-responses/responses.go @@ -6,13 +6,13 @@ import "net/http" import "fmt" func hello(res http.ResponseWriter, req *http.Request) { - res.Header().Set("Content-Type", "text/plain") - if req.URL.Path == "/protected" { - res.WriteHeader(401) - fmt.Fprintln(res, "Not allowed") - } else { - fmt.Fprintln(res, "Hello") - } + res.Header().Set("Content-Type", "text/plain") + if req.URL.Path == "/protected" { + res.WriteHeader(401) + fmt.Fprintln(res, "Not allowed") + } else { + fmt.Fprintln(res, "Hello") + } } func main() { diff --git a/073-request-routing/request-routing.go b/073-request-routing/request-routing.go index a1a585f..365bfe3 100644 --- a/073-request-routing/request-routing.go +++ b/073-request-routing/request-routing.go @@ -7,13 +7,13 @@ import "net/http" import "fmt" func hello(w http.ResponseWriter, req *http.Request) { - fmt.Fprintln(w, "hello " + req.URL.Query().Get(":name")) + fmt.Fprintln(w, "hello "+req.URL.Query().Get(":name")) } func main() { - p := pat.New() - p.Get("/hello/:name", http.HandlerFunc(hello)) - http.ListenAndServe(":5000", p) + p := pat.New() + p.Get("/hello/:name", http.HandlerFunc(hello)) + http.ListenAndServe(":5000", p) } // todo: consider gorilla-web diff --git a/074-request-logging/request-logging.go b/074-request-logging/request-logging.go index 7af01dc..eacb10e 100644 --- a/074-request-logging/request-logging.go +++ b/074-request-logging/request-logging.go @@ -7,32 +7,32 @@ import "time" import "fmt" func runLogging(logs chan string) { - for log := range logs { - fmt.Println(log) - } + for log := range logs { + fmt.Println(log) + } } func wrapLogging(f http.HandlerFunc) http.HandlerFunc { - logs := make(chan string, 10000) - go runLogging(logs) - return func(res http.ResponseWriter, req *http.Request) { - start := time.Now() - f(res, req) - method := req.Method - path := req.URL.Path - elapsed := float64(time.Since(start)) / 1000000.0 - logs <- fmt.Sprintf("method=%s path=%s elapsed=%f", method, path, elapsed) - } + logs := make(chan string, 10000) + go runLogging(logs) + return func(res http.ResponseWriter, req *http.Request) { + start := time.Now() + f(res, req) + method := req.Method + path := req.URL.Path + elapsed := float64(time.Since(start)) / 1000000.0 + logs <- fmt.Sprintf("method=%s path=%s elapsed=%f", method, path, elapsed) + } } func hello(res http.ResponseWriter, req *http.Request) { res.Header().Set("Content-Type", "text/plain") - time.Sleep(time.Millisecond * 50) - fmt.Fprintln(res, "Hello logged world") + time.Sleep(time.Millisecond * 50) + fmt.Fprintln(res, "Hello logged world") } func main() { - handler := wrapLogging(hello) + handler := wrapLogging(hello) http.HandleFunc("/", handler) http.ListenAndServe(":5000", nil) } diff --git a/075-static-content/static-content.go b/075-static-content/static-content.go index 52c3672..c15056e 100644 --- a/075-static-content/static-content.go +++ b/075-static-content/static-content.go @@ -5,8 +5,8 @@ package main import "net/http" func main() { - handler := http.FileServer(http.Dir("./")) - http.ListenAndServe(":5000", handler) + handler := http.FileServer(http.Dir("./")) + http.ListenAndServe(":5000", handler) } // todo: index pages diff --git a/076-basic-authentication/basic-authentication.go b/076-basic-authentication/basic-authentication.go index cecb132..fb31752 100644 --- a/076-basic-authentication/basic-authentication.go +++ b/076-basic-authentication/basic-authentication.go @@ -3,44 +3,44 @@ package main import ( - "net/http" - "encoding/base64" - "strings" - "fmt" + "encoding/base64" + "fmt" + "net/http" + "strings" ) type Authenticator func(string, string) bool func testAuth(r *http.Request, auth Authenticator) bool { - s := strings.SplitN(r.Header.Get("Authorization"), " ", 2) - if len(s) != 2 || s[0] != "Basic" { - return false - } - b, err := base64.StdEncoding.DecodeString(s[1]) - if err != nil { - return false - } - pair := strings.SplitN(string(b), ":", 2) - if len(pair) != 2 { - return false - } - return auth(pair[0], pair[1]) + s := strings.SplitN(r.Header.Get("Authorization"), " ", 2) + if len(s) != 2 || s[0] != "Basic" { + return false + } + b, err := base64.StdEncoding.DecodeString(s[1]) + if err != nil { + return false + } + pair := strings.SplitN(string(b), ":", 2) + if len(pair) != 2 { + return false + } + return auth(pair[0], pair[1]) } func requireAuth(w http.ResponseWriter, r *http.Request) { - w.Header().Set("WWW-Authenticate", `Basic realm="private"`) - w.WriteHeader(401) - w.Write([]byte("401 Unauthorized\n")) + w.Header().Set("WWW-Authenticate", `Basic realm="private"`) + w.WriteHeader(401) + w.Write([]byte("401 Unauthorized\n")) } func wrapAuth(h http.HandlerFunc, a Authenticator) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - if testAuth(r, a) { - h(w, r) - } else { - requireAuth(w, r) - } - } + if testAuth(r, a) { + h(w, r) + } else { + requireAuth(w, r) + } + } } func hello(w http.ResponseWriter, r *http.Request) { @@ -48,10 +48,10 @@ func hello(w http.ResponseWriter, r *http.Request) { } func main() { - checkPassword := func(_, password string) bool { - return password == "supersecret" - } - handler1 := http.HanderFunc(hello) - handler2 := wrapAuth(handler1, checkPassword) + checkPassword := func(_, password string) bool { + return password == "supersecret" + } + handler1 := http.HanderFunc(hello) + handler2 := wrapAuth(handler1, checkPassword) http.ListenAndServe(":5000", handler2) } diff --git a/077-canonical-hosts/canonical-hosts.go b/077-canonical-hosts/canonical-hosts.go index b53ab01..56e7897 100644 --- a/077-canonical-hosts/canonical-hosts.go +++ b/077-canonical-hosts/canonical-hosts.go @@ -8,26 +8,26 @@ import "fmt" func hello(res http.ResponseWriter, req *http.Request) { res.Header().Set("Content-Type", "text/plain") - fmt.Fprintln(res, "Hello canonical world") + fmt.Fprintln(res, "Hello canonical world") } func wrapCanonicalHost(f http.HandlerFunc, canonicalHost string) http.HandlerFunc { - return func(res http.ResponseWriter, req *http.Request) { - hostPort := strings.Split(req.Host, ":") - host := hostPort[0] - if host != canonicalHost { - fmt.Println("redirecting from", host, "to", canonicalHost) - hostPort[0] = canonicalHost - url := "http://" + strings.Join(hostPort, ":") + req.URL.String() - http.Redirect(res, req, url, 301) - return - } - f(res, req) - } + return func(res http.ResponseWriter, req *http.Request) { + hostPort := strings.Split(req.Host, ":") + host := hostPort[0] + if host != canonicalHost { + fmt.Println("redirecting from", host, "to", canonicalHost) + hostPort[0] = canonicalHost + url := "http://" + strings.Join(hostPort, ":") + req.URL.String() + http.Redirect(res, req, url, 301) + return + } + f(res, req) + } } func main() { - handler := wrapCanonicalHost(hello, "localhost") + handler := wrapCanonicalHost(hello, "localhost") http.HandleFunc("/", handler) http.ListenAndServe(":5000", nil) } diff --git a/078-middleware/middleware.go b/078-middleware/middleware.go index 6f85166..d7486f0 100644 --- a/078-middleware/middleware.go +++ b/078-middleware/middleware.go @@ -7,19 +7,19 @@ import "fmt" func hello(res http.ResponseWriter, req *http.Request) { res.Header().Set("Content-Type", "text/plain") - fmt.Fprintln(res, "Hello wrapped world") + fmt.Fprintln(res, "Hello wrapped world") } func wrapMiddleware(f http.HandlerFunc) http.HandlerFunc { - return func(res http.ResponseWriter, req *http.Request) { - fmt.Println("before") - f(res, req) - fmt.Println("after") - } + return func(res http.ResponseWriter, req *http.Request) { + fmt.Println("before") + f(res, req) + fmt.Println("after") + } } func main() { - handler := wrapMiddleware(hello) + handler := wrapMiddleware(hello) http.HandleFunc("/", handler) http.ListenAndServe(":5000", nil) } diff --git a/079-graceful-shutdown/graceful-shutdown.go b/079-graceful-shutdown/graceful-shutdown.go index 27ec0e5..5f45849 100644 --- a/079-graceful-shutdown/graceful-shutdown.go +++ b/079-graceful-shutdown/graceful-shutdown.go @@ -3,88 +3,92 @@ package main import ( + "fmt" "net" "net/http" - "time" "os" "os/signal" - "syscall" - "fmt" "sync/atomic" + "syscall" + "time" ) func slow(res http.ResponseWriter, req *http.Request) { - fmt.Println("respond at=start") - time.Sleep(time.Second * 5) - res.Header().Set("Content-Type", "text/plain") - fmt.Fprintln(res, "Finally.") - fmt.Println("respond at=finish") + fmt.Println("respond at=start") + time.Sleep(time.Second * 5) + res.Header().Set("Content-Type", "text/plain") + fmt.Fprintln(res, "Finally.") + fmt.Println("respond at=finish") } var connCount int64 = 0 type watchedConn struct { - net.Conn + net.Conn } func (w *watchedConn) Close() error { - atomic.AddInt64(&connCount, -1) - return w.Conn.Close() + atomic.AddInt64(&connCount, -1) + return w.Conn.Close() } type watchedListener struct { - net.Listener + net.Listener } func (l *watchedListener) Accept() (net.Conn, error) { - conn, err := l.Listener.Accept() - if err != nil { - return nil, err - } - atomic.AddInt64(&connCount, 1) - return &watchedConn{Conn: conn}, nil + conn, err := l.Listener.Accept() + if err != nil { + return nil, err + } + atomic.AddInt64(&connCount, 1) + return &watchedConn{Conn: conn}, nil } func main() { - stop := make(chan bool, 1) - sig := make(chan os.Signal, 1) - - server := &http.Server{Handler: http.HandlerFunc(slow)} - fmt.Println("listen at=start") - listener, listenErr := net.Listen("tcp", ":5000") - if listenErr != nil { panic(listenErr) } - wListener := &watchedListener{Listener: listener} - fmt.Println("listen at=finish") + stop := make(chan bool, 1) + sig := make(chan os.Signal, 1) - go func() { - <- stop - fmt.Println("close at=start") - closeErr := wListener.Close() - if closeErr != nil { panic(closeErr) } - fmt.Println("close at=finish") - }() + server := &http.Server{Handler: http.HandlerFunc(slow)} + fmt.Println("listen at=start") + listener, listenErr := net.Listen("tcp", ":5000") + if listenErr != nil { + panic(listenErr) + } + wListener := &watchedListener{Listener: listener} + fmt.Println("listen at=finish") - go func() { - signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM) - fmt.Println("trap at=start") - <- sig - stop <- true - fmt.Println("trap at=finish") - }() + go func() { + <-stop + fmt.Println("close at=start") + closeErr := wListener.Close() + if closeErr != nil { + panic(closeErr) + } + fmt.Println("close at=finish") + }() - fmt.Println("serve at=start") - server.Serve(wListener) - fmt.Println("serve at=finish") - for { - connCountCurrent := atomic.LoadInt64(&connCount) - if connCountCurrent > 0 { - fmt.Println("wait at=pending remaining=", connCountCurrent) - time.Sleep(time.Second) - } else { - fmt.Println("wait at=finish remaining=", connCountCurrent) - return - } - } + go func() { + signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM) + fmt.Println("trap at=start") + <-sig + stop <- true + fmt.Println("trap at=finish") + }() + + fmt.Println("serve at=start") + server.Serve(wListener) + fmt.Println("serve at=finish") + for { + connCountCurrent := atomic.LoadInt64(&connCount) + if connCountCurrent > 0 { + fmt.Println("wait at=pending remaining=", connCountCurrent) + time.Sleep(time.Second) + } else { + fmt.Println("wait at=finish remaining=", connCountCurrent) + return + } + } } // todo: clean up logging diff --git a/082-sha1-hashes/sha1-hashes.go b/082-sha1-hashes/sha1-hashes.go index f637bd9..d451c41 100644 --- a/082-sha1-hashes/sha1-hashes.go +++ b/082-sha1-hashes/sha1-hashes.go @@ -1,29 +1,29 @@ // ## SHA1 Hashes - -package main - + +package main + // Package `crypto/sha1` computes SHA1 hashes. -import "crypto/sha1" -import "encoding/hex" +import "crypto/sha1" +import "encoding/hex" import "fmt" - -func main() { + +func main() { // The pattern is `sha1.New()`, `sha1.Write(bytes)`, // then `sha1.Sum([]byte{}) - h := sha1.New() - + h := sha1.New() + // `Write` expects bytes. If you have a string `s` // use `[]byte(s)` to coerce it. - h.Write([]byte("sha1 this string")) - + 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) - + bs := h.Sum(nil) + // SHA1 values are often printed in hex, for example // with git. - fmt.Println(hex.EncodeToString(bs)) -} + fmt.Println(hex.EncodeToString(bs)) +} // You can compute other hashes using a similar // pattern. For exmpale, to compute MD5 hashes diff --git a/084-https-server/https-servers.go b/084-https-server/https-servers.go index 11a69e3..4bd253b 100644 --- a/084-https-server/https-servers.go +++ b/084-https-server/https-servers.go @@ -5,12 +5,14 @@ package main import "net/http" func handler(res http.ResponseWriter, req *http.Request) { - res.Header().Set("Content-Type", "text/plain") - res.Write([]byte("Hello from HTTPS\n")) + res.Header().Set("Content-Type", "text/plain") + res.Write([]byte("Hello from HTTPS\n")) } func main() { - http.HandleFunc("/", handler) - err := http.ListenAndServeTLS(":5000", "/tmp/server.crt", "/tmp/server.key", nil) - if err != nil { panic(err) } + http.HandleFunc("/", handler) + err := http.ListenAndServeTLS(":5000", "/tmp/server.crt", "/tmp/server.key", nil) + if err != nil { + panic(err) + } } diff --git a/tool/number.go b/tool/number.go index e6c66a4..08f2a6a 100644 --- a/tool/number.go +++ b/tool/number.go @@ -6,14 +6,14 @@ package main import ( "fmt" "io/ioutil" - "strings" - "regexp" "os" + "regexp" "sort" + "strings" ) func minInt(a, b int) int { - if (a < b) { + if a < b { return a } return b @@ -24,7 +24,9 @@ func main() { sourceNames := make([]string, 0) sourceMap := make(map[string]string) fileInfos, dirErr := ioutil.ReadDir("./") - if dirErr != nil { panic(dirErr) } + if dirErr != nil { + panic(dirErr) + } baseTrimmer, _ := regexp.Compile("[0-9x]+-") for _, fi := range fileInfos { baseName := baseTrimmer.ReplaceAllString(fi.Name(), "") @@ -36,15 +38,17 @@ func main() { // read names from index indexBytes, idxErr := ioutil.ReadFile("tool/index.txt") - if idxErr != nil { panic (idxErr) } + if idxErr != nil { + panic(idxErr) + } indexNamesAll := strings.Split(string(indexBytes), "\n") indexNames := make([]string, 0) for _, indexName := range indexNamesAll { - if indexName != "" && !strings.Contains(indexName, "#") && !strings.Contains(indexName, "~") { + if indexName != "" && !strings.Contains(indexName, "#") && !strings.Contains(indexName, "~") { indexNames = append(indexNames, indexName) } } - + // sanity check two lists if len(sourceNames) != len(indexNames) { sort.Strings(sourceNames) @@ -54,7 +58,7 @@ func main() { } os.Exit(1) } - + // rename some stuff for index, indexName := range indexNames { oldName := sourceMap[indexName]