diff --git a/000_temp/63-fall-2018/010-hands-on/02/main.go b/000_temp/63-fall-2018/010-hands-on/02/main.go index 0e8d297b..06b30003 100644 --- a/000_temp/63-fall-2018/010-hands-on/02/main.go +++ b/000_temp/63-fall-2018/010-hands-on/02/main.go @@ -6,11 +6,10 @@ func main() { foo() } - // func receiver identifier(parameters) return(s) {code} func foo() { x := "James Bond" y := 32 fmt.Println(x, y) -} \ No newline at end of file +} diff --git a/000_temp/63-fall-2018/010-hands-on/03/main.go b/000_temp/63-fall-2018/010-hands-on/03/main.go index e15ff88c..2b50d353 100644 --- a/000_temp/63-fall-2018/010-hands-on/03/main.go +++ b/000_temp/63-fall-2018/010-hands-on/03/main.go @@ -11,7 +11,6 @@ func main() { foo("Cole", 19) } - // func // modularize our code and DRY (don't repeat yourself) // funcs are defined with parameters diff --git a/000_temp/63-fall-2018/010-hands-on/04/main.go b/000_temp/63-fall-2018/010-hands-on/04/main.go index 9d790777..63371834 100644 --- a/000_temp/63-fall-2018/010-hands-on/04/main.go +++ b/000_temp/63-fall-2018/010-hands-on/04/main.go @@ -7,8 +7,7 @@ func main() { fmt.Println(z) } - // func receiver identifier(parameters) returns {code} func foo(x int, y int) int { return x * y -} \ No newline at end of file +} diff --git a/000_temp/63-fall-2018/010-hands-on/05/main.go b/000_temp/63-fall-2018/010-hands-on/05/main.go index 53cb74c7..8392d1fd 100644 --- a/000_temp/63-fall-2018/010-hands-on/05/main.go +++ b/000_temp/63-fall-2018/010-hands-on/05/main.go @@ -7,9 +7,8 @@ func main() { fmt.Println(x) } - // func receiver identifier(parameters) returns {code} func foo(s string) string { //return fmt.Sprint("Hello ", s, "!") return "Hello " + s + "!" -} \ No newline at end of file +} diff --git a/000_temp/63-fall-2018/011-loops/main.go b/000_temp/63-fall-2018/011-loops/main.go index 330652c9..98f4b8dd 100644 --- a/000_temp/63-fall-2018/011-loops/main.go +++ b/000_temp/63-fall-2018/011-loops/main.go @@ -6,10 +6,9 @@ func main() { foo() } - // func receiver identifier(parameters) returns {code} func foo() { for i := 0; i <= 100; i++ { fmt.Println(i) } -} \ No newline at end of file +} diff --git a/000_temp/63-fall-2018/012-conditional/main.go b/000_temp/63-fall-2018/012-conditional/main.go index c7f06600..13dd3037 100644 --- a/000_temp/63-fall-2018/012-conditional/main.go +++ b/000_temp/63-fall-2018/012-conditional/main.go @@ -6,12 +6,11 @@ func main() { foo() } - // func receiver identifier(parameters) returns {code} func foo() { for i := 0; i <= 100; i++ { - if i % 2 == 0 { + if i%2 == 0 { fmt.Println(i) } } -} \ No newline at end of file +} diff --git a/000_temp/63-fall-2018/013-slice/main.go b/000_temp/63-fall-2018/013-slice/main.go index bc594823..a2a54bdc 100644 --- a/000_temp/63-fall-2018/013-slice/main.go +++ b/000_temp/63-fall-2018/013-slice/main.go @@ -3,7 +3,7 @@ package main import "fmt" func main() { - xi := []int{2,3,4,5,6,7,8} + xi := []int{2, 3, 4, 5, 6, 7, 8} fmt.Println(xi) for i, v := range xi { diff --git a/000_temp/63-fall-2018/014-maps/main.go b/000_temp/63-fall-2018/014-maps/main.go index 5d2ab53f..0732c53b 100644 --- a/000_temp/63-fall-2018/014-maps/main.go +++ b/000_temp/63-fall-2018/014-maps/main.go @@ -3,7 +3,7 @@ package main import "fmt" func main() { - m := map[string]int{"James":32, "Jenny":27,} + m := map[string]int{"James": 32, "Jenny": 27} fmt.Println(m) for k, v := range m { diff --git a/000_temp/63-fall-2018/015-struct/main.go b/000_temp/63-fall-2018/015-struct/main.go index 8fcf44d6..1fe72944 100644 --- a/000_temp/63-fall-2018/015-struct/main.go +++ b/000_temp/63-fall-2018/015-struct/main.go @@ -3,26 +3,26 @@ package main import "fmt" type person struct { - first string - last string - age int + first string + last string + age int sayings []string } func main() { p1 := person{ - first: "James", - last: "Bond", - age: 32, - sayings: []string{"Shaken, not stirred", "Bond, James Bond",}, + first: "James", + last: "Bond", + age: 32, + sayings: []string{"Shaken, not stirred", "Bond, James Bond"}, } fmt.Println(p1) p2 := person{ - first: "Jenny", - last: "Moneypenny", - age: 27, - sayings: []string{"Danger knows no gender", "A woman's place is in control",}, + first: "Jenny", + last: "Moneypenny", + age: 27, + sayings: []string{"Danger knows no gender", "A woman's place is in control"}, } fmt.Println(p2) @@ -37,7 +37,7 @@ func main() { } fmt.Println("---------") - m := map[string]person{"James":p1, "Jenny":p2,} + m := map[string]person{"James": p1, "Jenny": p2} for k, p := range m { fmt.Println(k, p) diff --git a/000_temp/63-fall-2018/016-fun-with-text/main.go b/000_temp/63-fall-2018/016-fun-with-text/main.go index bc445452..da0c8289 100644 --- a/000_temp/63-fall-2018/016-fun-with-text/main.go +++ b/000_temp/63-fall-2018/016-fun-with-text/main.go @@ -15,4 +15,4 @@ func main() { fmt.Printf("%d - %b - %#X\n", v, v, v) } -} \ No newline at end of file +} diff --git a/000_temp/63-fall-2018/017-slice/main.go b/000_temp/63-fall-2018/017-slice/main.go index ea59942c..c4775c31 100644 --- a/000_temp/63-fall-2018/017-slice/main.go +++ b/000_temp/63-fall-2018/017-slice/main.go @@ -22,4 +22,4 @@ func main() { fmt.Printf("%T\n", xs) fmt.Println(xs) -} \ No newline at end of file +} diff --git a/000_temp/63-fall-2018/018-map/main.go b/000_temp/63-fall-2018/018-map/main.go index c84807fd..bbcd2fb6 100644 --- a/000_temp/63-fall-2018/018-map/main.go +++ b/000_temp/63-fall-2018/018-map/main.go @@ -3,9 +3,9 @@ package main import "fmt" func main() { - m := map[string]int{"James":7, "Jenny":8,} + m := map[string]int{"James": 7, "Jenny": 8} fmt.Printf("%T\n", m) fmt.Println(m) fmt.Println(m["James"]) -} \ No newline at end of file +} diff --git a/000_temp/63-fall-2018/019-struct/01/main.go b/000_temp/63-fall-2018/019-struct/01/main.go index d32ae55d..da9a6f35 100644 --- a/000_temp/63-fall-2018/019-struct/01/main.go +++ b/000_temp/63-fall-2018/019-struct/01/main.go @@ -3,6 +3,7 @@ package _1 import "fmt" type hotdog int + var x hotdog func main() { @@ -13,4 +14,4 @@ func main() { fmt.Printf("%T\n", y) fmt.Println(y) -} \ No newline at end of file +} diff --git a/000_temp/63-fall-2018/019-struct/02/main.go b/000_temp/63-fall-2018/019-struct/02/main.go index 7cb3b388..005acb9d 100644 --- a/000_temp/63-fall-2018/019-struct/02/main.go +++ b/000_temp/63-fall-2018/019-struct/02/main.go @@ -3,16 +3,16 @@ package main import "fmt" type person struct { - first string + first string license int sayings []string } func main() { p1 := person{ - first: "James", + first: "James", license: 007, - sayings: []string{"Shaken, not stirred", "Bond, James Bond",}, + sayings: []string{"Shaken, not stirred", "Bond, James Bond"}, } fmt.Println(p1) -} \ No newline at end of file +} diff --git a/000_temp/63-fall-2018/019-struct/03/main.go b/000_temp/63-fall-2018/019-struct/03/main.go index 61de635f..0a11ccb8 100644 --- a/000_temp/63-fall-2018/019-struct/03/main.go +++ b/000_temp/63-fall-2018/019-struct/03/main.go @@ -3,29 +3,29 @@ package main import "fmt" type person struct { - first string + first string license int sayings []string } func main() { p1 := person{ - first: "James", + first: "James", license: 7, - sayings: []string{"Shaken, not stirred", "Bond, James Bond",}, + sayings: []string{"Shaken, not stirred", "Bond, James Bond"}, } fmt.Println(p1) p2 := person{ - first: "Jenny", + first: "Jenny", license: 8, - sayings: []string{"When Bond can't handle it, call me", "I will always love Bond",}, + sayings: []string{"When Bond can't handle it, call me", "I will always love Bond"}, } fmt.Println(p2) xp := []person{p1, p2} fmt.Println(xp) - mp := map[string]person{"Mr":p1, "Ms":p2,} + mp := map[string]person{"Mr": p1, "Ms": p2} fmt.Println(mp) -} \ No newline at end of file +} diff --git a/000_temp/63-fall-2018/020-looping/02/main.go b/000_temp/63-fall-2018/020-looping/02/main.go index 1fb0b8ee..35e9ef03 100644 --- a/000_temp/63-fall-2018/020-looping/02/main.go +++ b/000_temp/63-fall-2018/020-looping/02/main.go @@ -14,10 +14,10 @@ func main() { } for _, val := range xi { - fmt.Println("-",val) + fmt.Println("-", val) } - m := map[string]int{"James":32, "Jenny":27,} + m := map[string]int{"James": 32, "Jenny": 27} for k, v := range m { fmt.Println(k, v) @@ -30,4 +30,4 @@ func main() { for _, value := range m { fmt.Println("-", value) } -} \ No newline at end of file +} diff --git a/000_temp/63-fall-2018/021-receiver/01/main.go b/000_temp/63-fall-2018/021-receiver/01/main.go index 608fdde7..be330a10 100644 --- a/000_temp/63-fall-2018/021-receiver/01/main.go +++ b/000_temp/63-fall-2018/021-receiver/01/main.go @@ -3,8 +3,8 @@ package main import "fmt" type person struct { - first string - last string + first string + last string saying string } @@ -16,14 +16,14 @@ func (p person) speak() { func main() { p1 := person{ - first: "James", - last: "Bond", + first: "James", + last: "Bond", saying: "Shaken, not stirred.", } p2 := person{ - first: "Jenny", - last: "Moneypenny", + first: "Jenny", + last: "Moneypenny", saying: "Helllllllo, James.", } diff --git a/000_temp/63-fall-2018/022-interfaces/01/main.go b/000_temp/63-fall-2018/022-interfaces/01/main.go index e194fb3a..301c5996 100644 --- a/000_temp/63-fall-2018/022-interfaces/01/main.go +++ b/000_temp/63-fall-2018/022-interfaces/01/main.go @@ -3,8 +3,8 @@ package main import "fmt" type person struct { - first string - last string + first string + last string saying string } @@ -30,21 +30,21 @@ func foo(h human) { func main() { p1 := person{ - first: "James", - last: "Bond", + first: "James", + last: "Bond", saying: "Shaken, not stirred.", } p2 := person{ - first: "Jenny", - last: "Moneypenny", + first: "Jenny", + last: "Moneypenny", saying: "Helllllllo, James.", } sa1 := secretAgent{ person: person{ - first: "Ian", - last: "Fleming", + first: "Ian", + last: "Fleming", saying: "The books were based on real stories", }, ltk: true, diff --git a/000_temp/63-fall-2018/022-interfaces/02/main.go b/000_temp/63-fall-2018/022-interfaces/02/main.go index e194fb3a..301c5996 100644 --- a/000_temp/63-fall-2018/022-interfaces/02/main.go +++ b/000_temp/63-fall-2018/022-interfaces/02/main.go @@ -3,8 +3,8 @@ package main import "fmt" type person struct { - first string - last string + first string + last string saying string } @@ -30,21 +30,21 @@ func foo(h human) { func main() { p1 := person{ - first: "James", - last: "Bond", + first: "James", + last: "Bond", saying: "Shaken, not stirred.", } p2 := person{ - first: "Jenny", - last: "Moneypenny", + first: "Jenny", + last: "Moneypenny", saying: "Helllllllo, James.", } sa1 := secretAgent{ person: person{ - first: "Ian", - last: "Fleming", + first: "Ian", + last: "Fleming", saying: "The books were based on real stories", }, ltk: true, diff --git a/000_temp/63-fall-2018/023-review/main.go b/000_temp/63-fall-2018/023-review/main.go index 9f33dc01..663f1e30 100644 --- a/000_temp/63-fall-2018/023-review/main.go +++ b/000_temp/63-fall-2018/023-review/main.go @@ -8,8 +8,8 @@ var d = 43 const z = 142 type person struct { - first string - age int + first string + age int saying string } @@ -53,7 +53,7 @@ func main() { // slice AGGREGATE or COMPOSITE data structure - ee := []int{2,3,4,7,9,} + ee := []int{2, 3, 4, 7, 9} fmt.Println(ee) // it was going SEQUENCE @@ -67,9 +67,8 @@ func main() { } } - // map AGGREGATE or COMPOSITE data structure - f := map[string]int{"James":32, "Jenny":27,} + f := map[string]int{"James": 32, "Jenny": 27} fmt.Println(f) for k, v := range f { @@ -78,14 +77,14 @@ func main() { // struct AGGREGATE or COMPOSITE data structure p1 := person{ - first: "James", - age: 32, + first: "James", + age: 32, saying: "shaken, not stirred", } - p2 := person { - first: "Jenny", - age: 27, + p2 := person{ + first: "Jenny", + age: 27, saying: "nobody does it better", } @@ -102,8 +101,8 @@ func main() { sa1 := secretagent{ person: person{ - first: "Jack", - age: 29, + first: "Jack", + age: 29, saying: "Blahb blahblabhalbhablhab", }, ltk: true, diff --git a/000_temp/63-fall-2018/024-unfurling-slice/main.go b/000_temp/63-fall-2018/024-unfurling-slice/main.go index 5cbce4f9..2870a3af 100644 --- a/000_temp/63-fall-2018/024-unfurling-slice/main.go +++ b/000_temp/63-fall-2018/024-unfurling-slice/main.go @@ -3,11 +3,11 @@ package main import "fmt" func main() { - xi := []int{1,2,4,5,7,8,9,} + xi := []int{1, 2, 4, 5, 7, 8, 9} foo(xi...) } func foo(i ...int) { fmt.Println(i) fmt.Printf("%T\n", i) -} \ No newline at end of file +} diff --git a/000_temp/63-fall-2018/025-defer/main.go b/000_temp/63-fall-2018/025-defer/main.go index 26cd71ca..08d33d20 100644 --- a/000_temp/63-fall-2018/025-defer/main.go +++ b/000_temp/63-fall-2018/025-defer/main.go @@ -23,4 +23,4 @@ func one() { func two() { fmt.Println("twoooooooooo") -} \ No newline at end of file +} diff --git a/000_temp/63-fall-2018/026-anonymous-func/main.go b/000_temp/63-fall-2018/026-anonymous-func/main.go index 6f1cd19d..9271c3d6 100644 --- a/000_temp/63-fall-2018/026-anonymous-func/main.go +++ b/000_temp/63-fall-2018/026-anonymous-func/main.go @@ -7,9 +7,9 @@ func main() { // anonymous func - func(x int){ + func(x int) { fmt.Println(x) }(4) } -func foo() {fmt.Println("this is foo")} \ No newline at end of file +func foo() { fmt.Println("this is foo") } diff --git a/000_temp/63-fall-2018/027-func-expression/main.go b/000_temp/63-fall-2018/027-func-expression/main.go index f536aab4..7c9531f9 100644 --- a/000_temp/63-fall-2018/027-func-expression/main.go +++ b/000_temp/63-fall-2018/027-func-expression/main.go @@ -7,7 +7,7 @@ func main() { fmt.Println(a) d := 14 - b := func(z string){ + b := func(z string) { fmt.Println(z) } @@ -18,10 +18,10 @@ func main() { b("jenny") x := foo("cat") y := foo("bird") - fmt.Println(x,y) + fmt.Println(x, y) fmt.Println(d) } func foo(s string) string { return s + "dogggggggg" -} \ No newline at end of file +} diff --git a/000_temp/63-fall-2018/028-returning-a-func/main.go b/000_temp/63-fall-2018/028-returning-a-func/main.go index 9fc52167..a0e34398 100644 --- a/000_temp/63-fall-2018/028-returning-a-func/main.go +++ b/000_temp/63-fall-2018/028-returning-a-func/main.go @@ -5,9 +5,9 @@ import "fmt" func main() { x := foo() y := foo() - fmt.Printf("%T\t %T\n", x,y) + fmt.Printf("%T\t %T\n", x, y) - func(z string){ + func(z string) { fmt.Println(z) }("james") @@ -17,7 +17,7 @@ func main() { } func foo() func(string) { - return func(z string){ + return func(z string) { fmt.Println(z) } -} \ No newline at end of file +} diff --git a/000_temp/63-fall-2018/029-pointers/main.go b/000_temp/63-fall-2018/029-pointers/main.go index e53221a7..2420c2f6 100644 --- a/000_temp/63-fall-2018/029-pointers/main.go +++ b/000_temp/63-fall-2018/029-pointers/main.go @@ -16,4 +16,4 @@ func main() { *y = 43 fmt.Println(x) -} \ No newline at end of file +} diff --git a/000_temp/63-fall-2018/030-os-args/main.go b/000_temp/63-fall-2018/030-os-args/main.go index 2babcccd..5868877c 100644 --- a/000_temp/63-fall-2018/030-os-args/main.go +++ b/000_temp/63-fall-2018/030-os-args/main.go @@ -1,8 +1,8 @@ package main import ( - "os" "fmt" + "os" ) func main() { diff --git a/000_temp/63-fall-2018/031-string-template/main.go b/000_temp/63-fall-2018/031-string-template/main.go index 7d1d6803..fa04dcd4 100644 --- a/000_temp/63-fall-2018/031-string-template/main.go +++ b/000_temp/63-fall-2018/031-string-template/main.go @@ -2,10 +2,10 @@ package main import ( "fmt" - "os" + "io" "log" + "os" "strings" - "io" ) func main() { diff --git a/000_temp/63-fall-2018/032-text-template/main.go b/000_temp/63-fall-2018/032-text-template/main.go index 6285de4a..81e7733b 100644 --- a/000_temp/63-fall-2018/032-text-template/main.go +++ b/000_temp/63-fall-2018/032-text-template/main.go @@ -1,10 +1,10 @@ package main import ( - "text/template" + "fmt" "log" "os" - "fmt" + "text/template" ) func main() { diff --git a/000_temp/63-fall-2018/033-parseglob/main.go b/000_temp/63-fall-2018/033-parseglob/main.go index 0d024b14..660e28ab 100644 --- a/000_temp/63-fall-2018/033-parseglob/main.go +++ b/000_temp/63-fall-2018/033-parseglob/main.go @@ -1,8 +1,8 @@ package main import ( - "text/template" "os" + "text/template" ) var tpl *template.Template diff --git a/000_temp/63-fall-2018/034-pass-in-data-aggregate/main.go b/000_temp/63-fall-2018/034-pass-in-data-aggregate/main.go index 4138fcfa..5fae3d01 100644 --- a/000_temp/63-fall-2018/034-pass-in-data-aggregate/main.go +++ b/000_temp/63-fall-2018/034-pass-in-data-aggregate/main.go @@ -1,8 +1,8 @@ package main import ( - "text/template" "os" + "text/template" ) var tpl *template.Template @@ -13,16 +13,16 @@ func init() { type person struct { First string - Last string - Age int + Last string + Age int } func main() { p1 := person{ First: "James", - Last: "Bond", - Age: 32, + Last: "Bond", + Age: 32, } err := tpl.ExecuteTemplate(os.Stdout, "one.gohtml", p1) @@ -32,8 +32,8 @@ func main() { p2 := person{ First: "Jenny", - Last: "Moneypenny", - Age: 27, + Last: "Moneypenny", + Age: 27, } xp := []person{p1, p2} diff --git a/000_temp/63-fall-2018/035-hash-bucket/main.go b/000_temp/63-fall-2018/035-hash-bucket/main.go index abc30424..fd2d43b7 100644 --- a/000_temp/63-fall-2018/035-hash-bucket/main.go +++ b/000_temp/63-fall-2018/035-hash-bucket/main.go @@ -38,4 +38,4 @@ func hashBucket(word string) int { fmt.Print(word[0], " ---- ") fmt.Printf("%#U\n", word[0]) return int(word[0]) -} \ No newline at end of file +} diff --git a/000_temp/63-fall-2018/036-templates-review/main.go b/000_temp/63-fall-2018/036-templates-review/main.go index ebbb00ac..806ab44b 100644 --- a/000_temp/63-fall-2018/036-templates-review/main.go +++ b/000_temp/63-fall-2018/036-templates-review/main.go @@ -2,8 +2,8 @@ package main import ( "html/template" - "os" "log" + "os" ) var tpl *template.Template @@ -45,5 +45,4 @@ func main() { log.Fatal("there was an error", err) } - -} \ No newline at end of file +} diff --git a/000_temp/63-fall-2018/037-data/01/main.go b/000_temp/63-fall-2018/037-data/01/main.go index fd858f6f..340d7bdc 100644 --- a/000_temp/63-fall-2018/037-data/01/main.go +++ b/000_temp/63-fall-2018/037-data/01/main.go @@ -2,8 +2,8 @@ package main import ( "html/template" - "os" "log" + "os" ) var tpl *template.Template @@ -30,8 +30,7 @@ func main() { panic(err) } - - xi := []int{7,8,9,125,345,} + xi := []int{7, 8, 9, 125, 345} err = tpl.ExecuteTemplate(f2, "about.gohtml", xi) if err != nil { panic(err) diff --git a/000_temp/63-fall-2018/037-data/02/main.go b/000_temp/63-fall-2018/037-data/02/main.go index 957a710c..7ca6dcd4 100644 --- a/000_temp/63-fall-2018/037-data/02/main.go +++ b/000_temp/63-fall-2018/037-data/02/main.go @@ -2,8 +2,8 @@ package main import ( "html/template" - "os" "log" + "os" ) var tpl *template.Template @@ -31,7 +31,6 @@ func main() { panic(err) } - t := ` here is a raw string diff --git a/000_temp/63-fall-2018/037-data/03/main.go b/000_temp/63-fall-2018/037-data/03/main.go index ca00444a..df79fde9 100644 --- a/000_temp/63-fall-2018/037-data/03/main.go +++ b/000_temp/63-fall-2018/037-data/03/main.go @@ -2,8 +2,8 @@ package main import ( "html/template" - "os" "log" + "os" ) var tpl *template.Template @@ -31,7 +31,6 @@ func main() { panic(err) } - f := false err = tpl.ExecuteTemplate(f2, "about.gohtml", f) if err != nil { diff --git a/000_temp/64-HANDLER/main.go b/000_temp/64-HANDLER/main.go index b5b6687c..aec134db 100644 --- a/000_temp/64-HANDLER/main.go +++ b/000_temp/64-HANDLER/main.go @@ -1,11 +1,10 @@ package main import ( - "net/http" "io" + "net/http" ) - type hotdog int // func receiver identifier(params) return(s) {} @@ -25,4 +24,4 @@ type Handler interface { ServeHTTP(ResponseWriter, *Request) } -*/ \ No newline at end of file +*/ diff --git a/000_temp/65-DEFAULT-SERVE-MUX/main.go b/000_temp/65-DEFAULT-SERVE-MUX/main.go index cce7dabe..23a13c24 100644 --- a/000_temp/65-DEFAULT-SERVE-MUX/main.go +++ b/000_temp/65-DEFAULT-SERVE-MUX/main.go @@ -1,14 +1,13 @@ package main import ( - "net/http" - "log" "fmt" "html" "io" + "log" + "net/http" ) - type hotdog int func (h hotdog) ServeHTTP(w http.ResponseWriter, r *http.Request) { @@ -27,7 +26,6 @@ func main() { fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path)) }) - log.Fatal(http.ListenAndServe(":8080", nil)) } @@ -36,7 +34,6 @@ func fooHandler(w http.ResponseWriter, r *http.Request) { io.WriteString(w, "hello from foo") } - /* type Handler @@ -44,4 +41,4 @@ type Handler interface { ServeHTTP(ResponseWriter, *Request) } -*/ \ No newline at end of file +*/ diff --git a/000_temp/66-HANDLEFUNC/main.go b/000_temp/66-HANDLEFUNC/main.go index 6eb300f5..89a3e730 100644 --- a/000_temp/66-HANDLEFUNC/main.go +++ b/000_temp/66-HANDLEFUNC/main.go @@ -1,9 +1,9 @@ package main import ( - "net/http" - "io" "fmt" + "io" + "net/http" ) func main() { @@ -18,10 +18,10 @@ func home(w http.ResponseWriter, r *http.Request) { io.WriteString(w, "hello from home") } -func about (w http.ResponseWriter, r *http.Request) { +func about(w http.ResponseWriter, r *http.Request) { io.WriteString(w, "wassup from about") } -func contact (w http.ResponseWriter, r * http.Request) { +func contact(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "get in touch with us, yo, here at contact") } diff --git a/000_temp/67-KABOOM-BOOOYAH/main.go b/000_temp/67-KABOOM-BOOOYAH/main.go index efbb3464..10cde19e 100644 --- a/000_temp/67-KABOOM-BOOOYAH/main.go +++ b/000_temp/67-KABOOM-BOOOYAH/main.go @@ -1,8 +1,8 @@ package main import ( - "net/http" "html/template" + "net/http" ) var tpl *template.Template @@ -23,10 +23,10 @@ func home(w http.ResponseWriter, r *http.Request) { tpl.ExecuteTemplate(w, "default.gohtml", nil) } -func about (w http.ResponseWriter, r *http.Request) { +func about(w http.ResponseWriter, r *http.Request) { tpl.ExecuteTemplate(w, "about.gohtml", nil) } -func contact (w http.ResponseWriter, r * http.Request) { +func contact(w http.ResponseWriter, r *http.Request) { tpl.ExecuteTemplate(w, "contact.gohtml", nil) } diff --git a/000_temp/69-review-golang/main.go b/000_temp/69-review-golang/main.go index 79853a7c..4eab484b 100644 --- a/000_temp/69-review-golang/main.go +++ b/000_temp/69-review-golang/main.go @@ -25,4 +25,4 @@ func def(w http.ResponseWriter, r *http.Request) { func abo(w http.ResponseWriter, r *http.Request) { tpl.ExecuteTemplate(w, "about.gohtml", nil) -} \ No newline at end of file +} diff --git a/000_temp/71/main.go b/000_temp/71/main.go index dc7e34b2..eb3782b3 100644 --- a/000_temp/71/main.go +++ b/000_temp/71/main.go @@ -24,4 +24,4 @@ func hom(w http.ResponseWriter, r *http.Request) { func abo(w http.ResponseWriter, r *http.Request) { tpl.ExecuteTemplate(w, "about.gohtml", nil) -} \ No newline at end of file +} diff --git a/000_temp/73/01/main.go b/000_temp/73/01/main.go index 8fd45780..95fa031a 100644 --- a/000_temp/73/01/main.go +++ b/000_temp/73/01/main.go @@ -2,7 +2,6 @@ package main import "fmt" - // a VALUE of TYPE var x int @@ -24,4 +23,4 @@ func main() { fmt.Println(f, g, h, j) fmt.Printf("%T\t %T\t %T\t %T\t \n", f, g, h, j) -} \ No newline at end of file +} diff --git a/000_temp/73/02/main.go b/000_temp/73/02/main.go index f6b872e7..0388f4d6 100644 --- a/000_temp/73/02/main.go +++ b/000_temp/73/02/main.go @@ -4,7 +4,7 @@ import "fmt" // AGGREGATE or COMPOSITE func main() { - xi := []int{4,5,6,7,42} + xi := []int{4, 5, 6, 7, 42} fmt.Println(xi) diff --git a/000_temp/73/04/main.go b/000_temp/73/04/main.go index 6b2ef70d..474d752c 100644 --- a/000_temp/73/04/main.go +++ b/000_temp/73/04/main.go @@ -4,7 +4,7 @@ import "fmt" type person struct { first string - age int + age int } type secretAgent struct { @@ -15,17 +15,17 @@ type secretAgent struct { func main() { p := person{ first: "Miss Moneypenny", - age: 27, + age: 27, } - sa := secretAgent { - person: person { + sa := secretAgent{ + person: person{ first: "James", - age: 32, + age: 32, }, ltk: true, } fmt.Println(p) fmt.Println(sa) -} \ No newline at end of file +} diff --git a/000_temp/73/06/main.go b/000_temp/73/06/main.go index fb9b32f5..2db4ce4d 100644 --- a/000_temp/73/06/main.go +++ b/000_temp/73/06/main.go @@ -13,7 +13,6 @@ func main() { fmt.Println("a after bar", a) } - func foo(b int) { b = 43 fmt.Println("b", b) @@ -23,4 +22,4 @@ func bar(c *int) { *c = 44 fmt.Println("c", c) fmt.Println("*c", *c) -} \ No newline at end of file +} diff --git a/000_temp/75/main.go b/000_temp/75/main.go index d2803717..8a7a4074 100644 --- a/000_temp/75/main.go +++ b/000_temp/75/main.go @@ -65,12 +65,10 @@ func con2(w http.ResponseWriter, r *http.Request) { tpl2.ExecuteTemplate(w, "contact.gohtml", nil) } - func con3(w http.ResponseWriter, r *http.Request) { tpl3.ExecuteTemplate(w, "contact.gohtml", nil) } - func con4(w http.ResponseWriter, r *http.Request) { tpl4.ExecuteTemplate(w, "pale", nil) } diff --git a/000_temp/77-web-server/main.go b/000_temp/77-web-server/main.go index be55885b..66e55de7 100644 --- a/000_temp/77-web-server/main.go +++ b/000_temp/77-web-server/main.go @@ -6,13 +6,14 @@ import ( ) var tpl *template.Template + func init() { tpl = template.Must(template.ParseGlob("templates/*.gohtml")) } func main() { http.HandleFunc("/", foo) - http.HandleFunc("/about", bar ) + http.HandleFunc("/about", bar) http.HandleFunc("/contact", con) http.Handle("/resources/", http.StripPrefix("/resources", http.FileServer(http.Dir("./assets")))) http.ListenAndServe(":8080", nil) @@ -23,28 +24,27 @@ func foo(w http.ResponseWriter, r *http.Request) { } func bar(w http.ResponseWriter, r *http.Request) { - tpl.ExecuteTemplate(w, "about.gohtml", []int{1,2,3,4,5,6,7,8,9}) + tpl.ExecuteTemplate(w, "about.gohtml", []int{1, 2, 3, 4, 5, 6, 7, 8, 9}) } func con(w http.ResponseWriter, r *http.Request) { type person struct { First string - Age int + Age int } p1 := person{ First: "James", - Age: 32, + Age: 32, } - p2 := person { + p2 := person{ First: "Jenny", - Age: 27, + Age: 27, } xp := []person{p1, p2} - tpl.ExecuteTemplate(w, "contact.gohtml", xp) } diff --git a/000_temp/78/main.go b/000_temp/78/main.go index 70a2a72d..49ec19ea 100644 --- a/000_temp/78/main.go +++ b/000_temp/78/main.go @@ -1,11 +1,11 @@ package main import ( - "net/http" "fmt" "html/template" - "os" "io" + "net/http" + "os" ) var tpl *template.Template @@ -14,7 +14,6 @@ func init() { tpl = template.Must(template.ParseGlob("templates/*.gohtml")) } - func main() { http.HandleFunc("/", foo) http.Handle("/resources/", http.StripPrefix("/resources", http.FileServer(http.Dir("./assets")))) @@ -37,7 +36,7 @@ func foo(w http.ResponseWriter, r *http.Request) { fn = h.Filename - df, err := os.Create("./assets/"+fn) + df, err := os.Create("./assets/" + fn) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return @@ -53,5 +52,5 @@ func foo(w http.ResponseWriter, r *http.Request) { // w.Header().Set("Content-Type", "text/html; charset=utf-8") - tpl.ExecuteTemplate(w, "index.gohtml", fn) + tpl.ExecuteTemplate(w, "index.gohtml", fn) } diff --git a/000_temp/80-renamer/main.go b/000_temp/80-renamer/main.go index 513f5ff5..94cbf541 100644 --- a/000_temp/80-renamer/main.go +++ b/000_temp/80-renamer/main.go @@ -24,4 +24,4 @@ func main() { return } } -} \ No newline at end of file +} diff --git a/000_temp/81-cookie-counter/main.go b/000_temp/81-cookie-counter/main.go index e223f3cf..b56fd7eb 100644 --- a/000_temp/81-cookie-counter/main.go +++ b/000_temp/81-cookie-counter/main.go @@ -1,10 +1,10 @@ package main import ( - "net/http" + "fmt" "html/template" + "net/http" "strconv" - "fmt" ) var tpl *template.Template @@ -30,7 +30,6 @@ func foo(w http.ResponseWriter, r *http.Request) { tpl.ExecuteTemplate(w, "index.gohtml", c.Value) } - func bar(w http.ResponseWriter, r *http.Request) { c, err := cookieCounter(r) if err != nil { @@ -49,9 +48,9 @@ func cookieCounter(r *http.Request) (*http.Cookie, error) { c, err := r.Cookie("wackadoodle") if err != nil { c = &http.Cookie{ - Name: "wackadoodle", + Name: "wackadoodle", Value: "0", - Path: "/", + Path: "/", } } diff --git a/000_temp/82/main.go b/000_temp/82/main.go index 180f8828..0f767517 100644 --- a/000_temp/82/main.go +++ b/000_temp/82/main.go @@ -6,5 +6,5 @@ func main() { var x uint x = 18446744073709551615 fmt.Println(x) - fmt.Printf("%T\n\n",x) + fmt.Printf("%T\n\n", x) } diff --git a/000_temp/84-pg-query/main.go b/000_temp/84-pg-query/main.go index ca56408b..b0a5efc5 100644 --- a/000_temp/84-pg-query/main.go +++ b/000_temp/84-pg-query/main.go @@ -7,10 +7,10 @@ import ( ) type Book struct { - ISBN string - Title string + ISBN string + Title string Author string - Price float32 + Price float32 } func main() { @@ -38,11 +38,11 @@ func main() { xb = append(xb, b) if err = rs.Err(); err != nil { - panic(err) + panic(err) } } for i, v := range xb { fmt.Println(i, v) } -} \ No newline at end of file +} diff --git a/000_temp/85-pg-query-web/main.go b/000_temp/85-pg-query-web/main.go index 4711d308..2d549dd4 100644 --- a/000_temp/85-pg-query-web/main.go +++ b/000_temp/85-pg-query-web/main.go @@ -31,10 +31,10 @@ func init() { } type Book struct { - Isbn string - Title string + Isbn string + Title string Author string - Price float32 + Price float32 } func main() { @@ -58,8 +58,6 @@ func foo(w http.ResponseWriter, r *http.Request) { tpl.ExecuteTemplate(w, "books.gohtml", xb) } - - //rs, err := db.Query("select * from books;") //if err != nil { // http.Error(w, http.StatusText(500), 500) diff --git a/000_temp/88-whole-enchilada/main.go b/000_temp/88-whole-enchilada/main.go index 6c587e24..59c69841 100644 --- a/000_temp/88-whole-enchilada/main.go +++ b/000_temp/88-whole-enchilada/main.go @@ -1,16 +1,16 @@ package main import ( - "html/template" - "net/http" - _ "github.com/lib/pq" "database/sql" - "log" "fmt" + _ "github.com/lib/pq" + "html/template" + "log" + "net/http" ) type customer struct { - ID int + ID int First string } diff --git a/000_temp/92-whole-enchilada-2/main.go b/000_temp/92-whole-enchilada-2/main.go index 9bdcdea6..5f914568 100644 --- a/000_temp/92-whole-enchilada-2/main.go +++ b/000_temp/92-whole-enchilada-2/main.go @@ -1,16 +1,16 @@ package main import ( - "html/template" - "net/http" - _ "github.com/lib/pq" "database/sql" - "log" "fmt" + _ "github.com/lib/pq" + "html/template" + "log" + "net/http" ) type customer struct { - ID int + ID int First string } @@ -93,7 +93,6 @@ func create(w http.ResponseWriter, r *http.Request) { http.Redirect(w, r, "/", http.StatusFound) } - // UPDATE func up(w http.ResponseWriter, r *http.Request) { @@ -118,7 +117,6 @@ func up(w http.ResponseWriter, r *http.Request) { tpl.ExecuteTemplate(w, "update.gohtml", c) } - func ptoo(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { http.Redirect(w, r, "/", http.StatusFound) @@ -146,7 +144,6 @@ func ptoo(w http.ResponseWriter, r *http.Request) { http.Redirect(w, r, "/", http.StatusFound) } - // DELETE func del(w http.ResponseWriter, r *http.Request) { @@ -157,7 +154,6 @@ func del(w http.ResponseWriter, r *http.Request) { customerid := r.FormValue("recordid") - result, err := db.Exec("DELETE FROM customers WHERE cid = $1;", customerid) if err != nil { http.Error(w, "didn't delete", http.StatusInternalServerError) diff --git a/003_string-to-html/01_stdout/main.go b/003_string-to-html/01_stdout/main.go index 4c834a5f..e422be0b 100644 --- a/003_string-to-html/01_stdout/main.go +++ b/003_string-to-html/01_stdout/main.go @@ -4,6 +4,7 @@ import "fmt" func main() { name := "Todd McLeod" + age := "36" tpl := ` @@ -14,6 +15,7 @@ func main() {

` + name + `

+

` + age + `

` diff --git a/003_string-to-html/02_file/index.html b/003_string-to-html/02_file/index.html new file mode 100644 index 00000000..6a0c26ae --- /dev/null +++ b/003_string-to-html/02_file/index.html @@ -0,0 +1,12 @@ + + + + + +Hello World! + + +

Todd McLeod

+ + + \ No newline at end of file diff --git a/003_string-to-html/03_os-Args/index.html b/003_string-to-html/03_os-Args/index.html index 624e6494..ca1a7d82 100644 --- a/003_string-to-html/03_os-Args/index.html +++ b/003_string-to-html/03_os-Args/index.html @@ -6,7 +6,7 @@ Hello World! -

JAMES

+

LIU

\ No newline at end of file diff --git a/004_parse_execute/02_file/index.html b/004_parse_execute/02_file/index.html new file mode 100644 index 00000000..cd5f37ba --- /dev/null +++ b/004_parse_execute/02_file/index.html @@ -0,0 +1,10 @@ + + + + + Hello World! + + +

Hello

+ + \ No newline at end of file diff --git a/004_parse_execute/04_ParseGlob/main.go b/004_parse_execute/04_ParseGlob/main.go index e5674c29..96ee8ade 100644 --- a/004_parse_execute/04_ParseGlob/main.go +++ b/004_parse_execute/04_ParseGlob/main.go @@ -7,7 +7,7 @@ import ( ) func main() { - tpl, err := template.ParseGlob("templates/*") + tpl, err := template.ParseGlob("templates/*.gohtml") if err != nil { log.Fatalln(err) } diff --git a/006_variable/01/tpl.gohtml b/006_variable/01/tpl.gohtml index a8da4597..b6e45bcd 100644 --- a/006_variable/01/tpl.gohtml +++ b/006_variable/01/tpl.gohtml @@ -6,7 +6,9 @@ -{{$wisdom := .}} -

The meaning of life: {{$wisdom}}

+{{$motto := .}} +{{$gibberish := .}} +

The meaning of life: {{$motto}}

+

Here's some gibberish for you: {{$gibberish}}

\ No newline at end of file diff --git a/007_data-structures/01_slice/01/main.go b/007_data-structures/01_slice/01/main.go index 56b05f6f..91824d13 100644 --- a/007_data-structures/01_slice/01/main.go +++ b/007_data-structures/01_slice/01/main.go @@ -14,9 +14,10 @@ func init() { func main() { - sages := []string{"Gandhi", "MLK", "Buddha", "Jesus", "Muhammad"} + // sages := []string{"Gandhi", "MLK", "Buddha", "Jesus", "Muhammad"} + got := []string{"Danerys", "Little Finger", "Arya Stark", "Jamie Lannister"} - err := tpl.Execute(os.Stdout, sages) + err := tpl.Execute(os.Stdout, got) if err != nil { log.Fatalln(err) } diff --git a/008_func/01/main.go b/008_func/01/main.go index 41830345..4e941f30 100644 --- a/008_func/01/main.go +++ b/008_func/01/main.go @@ -31,7 +31,11 @@ var fm = template.FuncMap{ } func init() { + // The following line is called Chaining: template.New() returns a *Template, which can call Funcs, which also returns a *Template; which continues to call ParseFiles and returns (*Template, error), and can be caled by Must tpl = template.Must(template.New("").Funcs(fm).ParseFiles("tpl.gohtml")) + // Following 2 lines not working + // tpl = template.Must(template.ParseFiles("tpl.gohtml")) + // tpl = tpl.Funcs(fm) } func firstThree(s string) string { diff --git a/008_func/011/main.go b/008_func/011/main.go new file mode 100644 index 00000000..fff47d8f --- /dev/null +++ b/008_func/011/main.go @@ -0,0 +1,13 @@ +package main + +import ( + "os" + "text/template" +) + + +func main() { + tpl := template.Must(template.New("someth").Parse(`here is my template, yo`)) + tpl.ExecuteTemplate(os.Stdout, "Someth", nil) + +} diff --git a/008_func/02_date-formatting/main.go b/008_func/02_date-formatting/main.go index e051d5de..cfa5ee03 100644 --- a/008_func/02_date-formatting/main.go +++ b/008_func/02_date-formatting/main.go @@ -5,6 +5,7 @@ import ( "os" "text/template" "time" + "fmt" ) var tpl *template.Template @@ -17,8 +18,13 @@ func monthDayYear(t time.Time) string { return t.Format("01-02-2006") } +func yearMonthDay(t time.Time) string { + return t.Format("2006年01月02日") +} + var fm = template.FuncMap{ "fdateMDY": monthDayYear, + "fdateYMD": yearMonthDay, } func main() { @@ -27,4 +33,8 @@ func main() { if err != nil { log.Fatalln(err) } + t := time.Now() + fmt.Println(t) + tf := t.Format(time.RFC3339) + fmt.Println(tf) } diff --git a/008_func/02_date-formatting/tpl.gohtml b/008_func/02_date-formatting/tpl.gohtml index b216fc3e..73d2ceb5 100644 --- a/008_func/02_date-formatting/tpl.gohtml +++ b/008_func/02_date-formatting/tpl.gohtml @@ -8,7 +8,9 @@ Without date formatting: {{.}} -With date formatting: {{fdateMDY .}} +With Chinese date formatting: {{fdateYMD .}} + +With USA date formatting: {{fdateMDY .}} diff --git a/009_predefined-global-functions/01_index/01/tpl.gohtml b/009_predefined-global-functions/01_index/01/tpl.gohtml index b3adf4b7..e26d67da 100644 --- a/009_predefined-global-functions/01_index/01/tpl.gohtml +++ b/009_predefined-global-functions/01_index/01/tpl.gohtml @@ -7,7 +7,7 @@ {{index . 2}} -{{index . 0}} +{{index . 1}} {{index . 1}} + {{.AcaYear}} {{.Fall.Term}}
+ {{range .Fall.Courses}} + {{.Number}} -- {{.Name}} -- {{.Units}}
+ {{end}} + {{.AcaYear}} {{.Spring.Term}}
+ {{range .Spring.Courses}} + {{.Number}} -- {{.Name}} -- {{.Units}}
+ {{end}} {{end}} diff --git a/012_hands-on/03_hands-on/01_hotel_def/main.go b/012_hands-on/03_hands-on/01_hotel_def/main.go new file mode 100644 index 00000000..f6f521c6 --- /dev/null +++ b/012_hands-on/03_hands-on/01_hotel_def/main.go @@ -0,0 +1,49 @@ +package main + +import ( + "os" + "log" + "text/template" +) + +// type hotel struct { +// Name string +// Address string +// City string +// Zip string +// Region string +// } + +type hotel struct { + Name, Address, City, Zip, Region string +} + +var tpl *template.Template + +func init() { + tpl = template.Must(template.ParseFiles("tpl.gohtml")) +} + +func main() { + hotels := []hotel{ + hotel{ + Name: "Malibu Beach Resort", + Address: "Eagle Street 156", + City: "Malibu", + Zip: "93527", + Region: "Southern", + }, + hotel{ + Name: "SF Autobot", + Address: "Cybertron 38093", + City: "San Francisco", + Zip: "93823", + Region: "Central", + }, + } + + err := tpl.Execute(os.Stdout, hotels) + if err != nil { + log.Fatalln(err) + } +} \ No newline at end of file diff --git a/012_hands-on/03_hands-on/01_hotel_def/tpl.gohtml b/012_hands-on/03_hands-on/01_hotel_def/tpl.gohtml new file mode 100644 index 00000000..0b9d2046 --- /dev/null +++ b/012_hands-on/03_hands-on/01_hotel_def/tpl.gohtml @@ -0,0 +1,17 @@ + + + + + California Hotels + + + +{{range .}} + {{/* Insert your code here to show hotels */}} + Name: {{.Name}}
+ Address: {{.Address}} -- {{.City}} City -- {{.Zip}}
+ Region: {{.Region}} +{{end}} + + + \ No newline at end of file diff --git a/012_hands-on/03_hands-on/02_hotels_divided_by_region/main.go b/012_hands-on/03_hands-on/02_hotels_divided_by_region/main.go new file mode 100644 index 00000000..40a2d691 --- /dev/null +++ b/012_hands-on/03_hands-on/02_hotels_divided_by_region/main.go @@ -0,0 +1,47 @@ +package main + +import ( + "os" + "log" + "text/template" +) + +type hotel struct { + Name, Address, City, Zip string +} + +type region struct { + Region string + Hotels []hotel +} + +var tpl *template.Template + +func init() { + tpl = template.Must(template.ParseFiles("tpl.gohtml")) +} + +func main() { + r := region{ + Region: "Central", + Hotels: []hotel{ + hotel{ + Name: "Malibu Beach Resort", + Address: "Eagle Street 156", + City: "Malibu", + Zip: "93527", + }, + hotel{ + Name: "SF Autobot", + Address: "Cybertron 38093", + City: "San Francisco", + Zip: "93823", + }, + }, + } + + err := tpl.Execute(os.Stdout, r) + if err != nil { + log.Fatalln(err) + } +} \ No newline at end of file diff --git a/012_hands-on/03_hands-on/02_hotels_divided_by_region/tpl.gohtml b/012_hands-on/03_hands-on/02_hotels_divided_by_region/tpl.gohtml new file mode 100644 index 00000000..22c3472b --- /dev/null +++ b/012_hands-on/03_hands-on/02_hotels_divided_by_region/tpl.gohtml @@ -0,0 +1,16 @@ + + + + + Hotels + + + +{{.Region}} +{{range .Hotels}} + {{.Name}}
+ {{.Address}} {{.City}} {{.Zip}}
+{{end}} + + + \ No newline at end of file diff --git a/012_hands-on/07_hands-on/01/main.go b/012_hands-on/07_hands-on/01/main.go new file mode 100644 index 00000000..075e8da9 --- /dev/null +++ b/012_hands-on/07_hands-on/01/main.go @@ -0,0 +1,130 @@ +package main + +import ( + "log" + "os" + "text/template" +) + +type item struct { + Name, Descrip string + Price float64 +} + +type meal struct { + Meal string + Item []item +} + +type menu []meal + +type resturant struct { + Name, Address, City string + Menu menu +} + +var tpl *template.Template + +func init() { + tpl = template.Must(template.ParseFiles("tpl.gohtml")) +} + +func main() { + resturants := []resturant { + resturant{ + Name: "Jianqiao Diner", + Address: "Chongqing Internet Industrial Park 101", + City: "Chongqing", + Menu: menu { + meal{ + Meal: "Breakfast", + Item: []item{ + item{ + Name: "Xiao Mian", + Descrip:"Spicy", + Price: 6.5, + }, + item{ + Name: "Dou Jiang", + Descrip:"Smooth", + Price: 2.2, + }, + }, + }, + }, + }, + resturant{ + Name: "Malibu Cuisine", + Address: "Eagle Street 38203", + City: "Malibu", + Menu: menu { + meal{ + Meal: "Breakfast", + Item: []item{ + item{ + Name: "Oatmeal", + Descrip: "yum yum", + Price: 4.95, + }, + item{ + Name: "Cheerios", + Descrip: "American eating food traditional now", + Price: 3.95, + }, + item{ + Name: "Juice Orange", + Descrip: "Delicious drinking in throat squeezed fresh", + Price: 2.95, + }, + }, + }, + meal{ + Meal: "Lunch", + Item: []item{ + item{ + Name: "Hamburger", + Descrip: "Delicous good eating for you", + Price: 6.95, + }, + item{ + Name: "Cheese Melted Sandwich", + Descrip: "Make cheese bread melt grease hot", + Price: 3.95, + }, + item{ + Name: "French Fries", + Descrip: "French eat potatoe fingers", + Price: 2.95, + }, + }, + }, + meal{ + Meal: "Dinner", + Item: []item{ + item{ + Name: "Pasta Bolognese", + Descrip: "From Italy delicious eating", + Price: 7.95, + }, + item{ + Name: "Steak", + Descrip: "Dead cow grilled bloody", + Price: 13.95, + }, + item{ + Name: "Bistro Potatoe", + Descrip: "Bistro bar wood American bacon", + Price: 6.95, + }, + }, + }, + }, + }, + } + + + err := tpl.Execute(os.Stdout, resturants) + if err != nil { + log.Fatalln(err) + } +} diff --git a/012_hands-on/07_hands-on/01/tpl.gohtml b/012_hands-on/07_hands-on/01/tpl.gohtml new file mode 100644 index 00000000..ad8dee7f --- /dev/null +++ b/012_hands-on/07_hands-on/01/tpl.gohtml @@ -0,0 +1,20 @@ + + + + + Document + + + +{{range .}} + {{.Name}} - {{.Address}} -{{.City}} + {{range .Menu}} + {{.Meal}}
+ {{range .Item}} + {{.Name}} {{.Descrip}} {{.Price}}
+ {{end}} + {{end}} +{{end}} + + + \ No newline at end of file diff --git a/012_hands-on/09_hands-on/main.go b/012_hands-on/09_hands-on/main.go new file mode 100644 index 00000000..6edf73a0 --- /dev/null +++ b/012_hands-on/09_hands-on/main.go @@ -0,0 +1,65 @@ +package main + +import ( + "log" + "os" + "text/template" + "encoding/csv" + "time" + "strconv" +) + +type Record struct { + Date time.Time + Open float64 +} + +var tpl *template.Template + +func ParseRecords(filePath string) []Record { + src, err := os.Open(filePath) + if err != nil { + log.Fatalln(err) + } + defer src.Close() + + rdr := csv.NewReader(src) + rows, err := rdr.ReadAll() + if err != nil { + log.Fatalln(err) + } + // https://tour.golang.org/moretypes/13 + records := make([]Record, 0, len(rows)) + + for i, row := range rows { + if i == 0 { + continue + } + date, _ := time.Parse("2006-01-02", row[0]) + open, _ := strconv.ParseFloat(row[1], 64) + + records = append(records, Record{ + Date: date, + Open: open, + }) + } + return records +} + +func main() { + // 1. parse the csv file + var filePath = "table.csv" + records := ParseRecords(filePath) + + // 2. parse template + tpl, err := template.ParseFiles("tpl.gohtml") + if err != nil { + log.Fatalln(err) + } + + // 3. 把从csv读取到的数据表达在template里面 + err = tpl.Execute(os.Stdout, records) + if err != nil { + log.Fatalln(err) + } +} diff --git a/012_hands-on/09_hands-on/tpl.gohtml b/012_hands-on/09_hands-on/tpl.gohtml new file mode 100644 index 00000000..182fafe0 --- /dev/null +++ b/012_hands-on/09_hands-on/tpl.gohtml @@ -0,0 +1,15 @@ + + + + + Document + + + +{{range .}} + {{.}} + {{/* Put your own code for detailed iteration and printing */}} +{{end}} + + + \ No newline at end of file diff --git a/022_hands-on/01/01_hands-on/main.go b/022_hands-on/01/01_hands-on/main.go new file mode 100644 index 00000000..d73d6d1d --- /dev/null +++ b/022_hands-on/01/01_hands-on/main.go @@ -0,0 +1,27 @@ +package main + +import ( + "io" + "net/http" +) + +func index(w http.ResponseWriter, req *http.Request) { + io.WriteString(w, "Client visit the index") +} + +func dog(w http.ResponseWriter, req *http.Request) { + io.WriteString(w, "Client want a dog") +} + +func me(w http.ResponseWriter, req *http.Request) { + io.WriteString(w, "Hello Richard") +} + + +func main() { + http.HandleFunc("/", index) + http.HandleFunc("/dog/", dog) + http.HandleFunc("/me", me) + + http.ListenAndServe(":8080", nil) +} \ No newline at end of file diff --git a/022_hands-on/01/03_hands-on/main.go b/022_hands-on/01/03_hands-on/main.go new file mode 100644 index 00000000..624561e7 --- /dev/null +++ b/022_hands-on/01/03_hands-on/main.go @@ -0,0 +1,37 @@ +package main + +import ( + "io" + "log" + "net/http" + "html/template" +) + +func index(w http.ResponseWriter, req *http.Request) { + io.WriteString(w, "Client visit the index") +} + +func dog(w http.ResponseWriter, req *http.Request) { + io.WriteString(w, "Client want a dog") +} + +func me(w http.ResponseWriter, req *http.Request) { + tpl, err := template.ParseFiles("tpl.gohtml") + if err != nil { + log.Fatalln("error parsing template", err) + } + + err = tpl.ExecuteTemplate(w, "tpl.gohtml", "xxx") + if err != nil { + log.Fatalln("error parsing template", err) + } +} + + +func main() { + http.HandleFunc("/", index) + http.HandleFunc("/dog/", dog) + http.HandleFunc("/me/", me) + + http.ListenAndServe(":8080", nil) +} \ No newline at end of file diff --git a/022_hands-on/01/03_hands-on/tpl.gohtml b/022_hands-on/01/03_hands-on/tpl.gohtml new file mode 100644 index 00000000..6604c430 --- /dev/null +++ b/022_hands-on/01/03_hands-on/tpl.gohtml @@ -0,0 +1,21 @@ + + + + + Passing Value + + + + +{{if .}} +

Hello, {{.}}

+{{end}} + + +
+ + +
+ + + \ No newline at end of file diff --git a/022_hands-on/01/05_hands-on/main.go b/022_hands-on/01/05_hands-on/main.go new file mode 100644 index 00000000..3b29e4e6 --- /dev/null +++ b/022_hands-on/01/05_hands-on/main.go @@ -0,0 +1,37 @@ +package main + +import ( + "io" + "log" + "net/http" + "html/template" +) + +func index(w http.ResponseWriter, req *http.Request) { + io.WriteString(w, "Client visit the index") +} + +func dog(w http.ResponseWriter, req *http.Request) { + io.WriteString(w, "Client want a dog") +} + +func me(w http.ResponseWriter, req *http.Request) { + tpl, err := template.ParseFiles("tpl.gohtml") + if err != nil { + log.Fatalln("error parsing template", err) + } + + err = tpl.ExecuteTemplate(w, "tpl.gohtml", "xxx") + if err != nil { + log.Fatalln("error parsing template", err) + } +} + + +func main() { + http.Handle("/", http.HandlerFunc(index)) + http.Handle("/dog/", http.HandlerFunc(dog)) + http.Handle("/me/", http.HandlerFunc(me)) + + http.ListenAndServe(":8080", nil) +} \ No newline at end of file diff --git a/022_hands-on/01/05_hands-on/tpl.gohtml b/022_hands-on/01/05_hands-on/tpl.gohtml new file mode 100644 index 00000000..6604c430 --- /dev/null +++ b/022_hands-on/01/05_hands-on/tpl.gohtml @@ -0,0 +1,21 @@ + + + + + Passing Value + + + + +{{if .}} +

Hello, {{.}}

+{{end}} + + +
+ + +
+ + + \ No newline at end of file diff --git a/022_hands-on/02/01_hands-on/main.go b/022_hands-on/02/01_hands-on/main.go new file mode 100644 index 00000000..0727f7cc --- /dev/null +++ b/022_hands-on/02/01_hands-on/main.go @@ -0,0 +1,32 @@ +package main + +import ( + "log" + "io" + "net" +) + +func main() { + // It seems to yield a *TCPListener type + ln, err := net.Listen("tcp", ":8080") + if err != nil { + log.Fatalln("Cannot Listen: ", err) + } + defer ln.Close() + + for { + conn, err := ln.Accept() + if err != nil { + log.Println("Cannot Accept: ", err) + continue + } + io.WriteString(conn, "I see you connected.") + + conn.Close() + // 我之前是这下面写错了 + // err = ln.Close() + // if err != nil { + // log.Fatalln("Cannot Close: ", err) + // } + } +} \ No newline at end of file diff --git a/022_hands-on/02/03_hands-on/main.go b/022_hands-on/02/03_hands-on/main.go new file mode 100644 index 00000000..fe6d7acb --- /dev/null +++ b/022_hands-on/02/03_hands-on/main.go @@ -0,0 +1,42 @@ +package main + +import ( + "io" + "log" + "net" + "bufio" + "fmt" +) + +func main() { + l, err := net.Listen("tcp", ":8080") + if err != nil { + log.Fatalln(err) + } + defer l.Close() + + for { + conn, err := l.Accept() + if err != nil { + log.Println(err) + continue + } + // read from connection + scanner := bufio.NewScanner(conn) + // my solution + // fmt.Println(scanner.Bytes()) + // Todd's solution + for scanner.Scan() { + ln := scanner.Text() + fmt.Println(ln) + } + defer conn.Close() + + // write to connection + // we never get here because of the 29th line scanning loop of open stream + fmt.Println("Code got here.") + io.WriteString(conn, "I see you connected.") + + conn.Close() + } +} diff --git a/022_hands-on/02/05_hands-on/main.go b/022_hands-on/02/05_hands-on/main.go new file mode 100644 index 00000000..bc4f3e3c --- /dev/null +++ b/022_hands-on/02/05_hands-on/main.go @@ -0,0 +1,40 @@ +package main + +import ( + "bufio" + "fmt" + "io" + "log" + "net" +) + +func main() { + l, err := net.Listen("tcp", ":8080") + if err != nil { + log.Fatalln(err) + } + defer l.Close() + + for { + conn, err := l.Accept() + if err != nil { + log.Println(err) + continue + } + + scanner := bufio.NewScanner(conn) + for scanner.Scan() { + ln := scanner.Text() + fmt.Println(ln) + if ln == "" { + break + } + } + defer conn.Close() + + fmt.Println("Code got here.") + io.WriteString(conn, "I see you connected.") + + conn.Close() + } +} diff --git a/022_hands-on/02/07_hands-on/main.go b/022_hands-on/02/07_hands-on/main.go new file mode 100644 index 00000000..844f5fe9 --- /dev/null +++ b/022_hands-on/02/07_hands-on/main.go @@ -0,0 +1,43 @@ +package main + +import ( + "bufio" + "fmt" + "log" + "net" +) + +func serve(conn net.Conn) { + // 因为defer会等到同级都执行完才执行,所以这里可以把关闭作为一个语法糖,写到最前面 + defer conn.Close() + scanner := bufio.NewScanner(conn) + for scanner.Scan() { + ln := scanner.Text() + fmt.Println(ln) + if ln == "" { + // when ln is empty, header is done + fmt.Println("THIS IS THE END OF THE HTTP REQUEST HEADERS") + break + } + } + // fmt.Println("Code got here.") + // io.WriteString(conn, "I see you connected.") +} + +func main() { + l, err := net.Listen("tcp", ":8080") + if err != nil { + log.Fatalln(err) + } + defer l.Close() + + for { + conn, err := l.Accept() + if err != nil { + log.Println(err) + continue + } + + go serve(conn) + } +} diff --git a/022_hands-on/02/09_hands-on/main.go b/022_hands-on/02/09_hands-on/main.go new file mode 100644 index 00000000..1a588e25 --- /dev/null +++ b/022_hands-on/02/09_hands-on/main.go @@ -0,0 +1,46 @@ +package main + +import ( + "bufio" + "fmt" + "log" + "net" + "io" +) + +func serve(conn net.Conn) { + // 因为defer会等到同级都执行完才执行,所以这里可以把关闭作为一个语法糖,写到最前面 + defer conn.Close() + scanner := bufio.NewScanner(conn) + for scanner.Scan() { + ln := scanner.Text() + fmt.Println(ln) + if ln == "" { + // when ln is empty, header is done + fmt.Println("THIS IS THE END OF THE HTTP REQUEST HEADERS") + break + } + } + fmt.Println("Code got here.") + io.WriteString(conn, "I see you connected.") +} + + + +func main() { + l, err := net.Listen("tcp", ":8080") + if err != nil { + log.Fatalln(err) + } + defer l.Close() + + for { + conn, err := l.Accept() + if err != nil { + log.Println(err) + continue + } + + go serve(conn) + } +} diff --git a/022_hands-on/02/11_hands-on/main.go b/022_hands-on/02/11_hands-on/main.go new file mode 100644 index 00000000..aba381a1 --- /dev/null +++ b/022_hands-on/02/11_hands-on/main.go @@ -0,0 +1,49 @@ +package main + +import ( + "bufio" + "fmt" + "io" + "log" + "net" +) + +func main() { + l, err := net.Listen("tcp", ":8080") + if err != nil { + log.Fatalln(err) + } + defer l.Close() + + for { + conn, err := l.Accept() + if err != nil { + log.Println(err) + continue + } + serve(conn) + } +} + +func serve(c net.Conn) { + defer c.Close() + scanner := bufio.NewScanner(c) + for scanner.Scan() { + ln := scanner.Text() + fmt.Println(ln) + if ln == "" { + // when ln is empty, header is done + fmt.Println("THIS IS THE END OF THE HTTP REQUEST HEADERS") + break + } + } + + body := "Quick brown fox jumps over lazy dog." + // 这里是生成了一个http协议的报文头 + io.WriteString(c, "HTTP/1.1 200 OK\r\n") + fmt.Fprintf(c, "Content-Length: %d\r\n", len(body)) + fmt.Fprint(c, "Content-Type: text/plain\r\n") + io.WriteString(c, "\r\n") + // http header以一个空行结束, 所以下一行会开始body + io.WriteString(c, body) +} diff --git a/022_hands-on/02/13_hands-on/main.go b/022_hands-on/02/13_hands-on/main.go new file mode 100644 index 00000000..1428f6d0 --- /dev/null +++ b/022_hands-on/02/13_hands-on/main.go @@ -0,0 +1,64 @@ +package main + +import ( + "bufio" + "fmt" + "io" + "log" + "net" + "strings" +) + +func main() { + l, err := net.Listen("tcp", ":8080") + if err != nil { + log.Fatalln(err) + } + defer l.Close() + + for { + conn, err := l.Accept() + if err != nil { + log.Println(err) + continue + } + serve(conn) + } +} + +func serve(c net.Conn) { + defer c.Close() + scanner := bufio.NewScanner(c) + var i int + var rMethod, rURI string + for scanner.Scan() { + ln := scanner.Text() + fmt.Println(ln) + if i == 0 { + xs := strings.Fields(ln) + rMethod = xs[0] + rURI = xs[1] + fmt.Println("METHOD: ", rMethod) + fmt.Println("URI: ", rURI) + } + if ln == "" { + // when ln is empty, header is done + fmt.Println("THIS IS THE END OF THE HTTP REQUEST HEADERS") + break + } + i++ + } + + body := "Quick brown fox jumps over lazy dog." + body += "\n" + body += rMethod + body += "\n" + body += rURI + // 这里是生成了一个http协议的报文头 + io.WriteString(c, "HTTP/1.1 200 OK\r\n") + fmt.Fprintf(c, "Content-Length: %d\r\n", len(body)) + fmt.Fprint(c, "Content-Type: text/plain\r\n") + io.WriteString(c, "\r\n") + // http header以一个空行结束, 所以下一行会开始body + io.WriteString(c, body) +} diff --git a/022_hands-on/02/15_hands-on/main.go b/022_hands-on/02/15_hands-on/main.go new file mode 100644 index 00000000..2a11cf57 --- /dev/null +++ b/022_hands-on/02/15_hands-on/main.go @@ -0,0 +1,74 @@ +package main + +import ( + "bufio" + "fmt" + "io" + "log" + "net" + "strings" +) + +func main() { + l, err := net.Listen("tcp", ":8080") + if err != nil { + log.Fatalln(err) + } + defer l.Close() + + for { + conn, err := l.Accept() + if err != nil { + log.Println(err) + continue + } + serve(conn) + } +} + +func serve(c net.Conn) { + defer c.Close() + scanner := bufio.NewScanner(c) + var i int + var rMethod, rURI string + for scanner.Scan() { + ln := scanner.Text() + fmt.Println(ln) + if i == 0 { + // we're in REQUEST LINE + xs := strings.Fields(ln) + rMethod = xs[0] + rURI = xs[1] + fmt.Println("METHOD:", rMethod) + fmt.Println("URI:", rURI) + } + if ln == "" { + // when ln is empty, header is done + fmt.Println("THIS IS THE END OF THE HTTP REQUEST HEADERS") + break + } + i++ + } + // body := "CHECK OUT THE RESPONSE BODY PAYLOAD" + // body += "\n" + // body += rMethod + // body += "\n" + // body += rURI + body := ` + + + + + Code Gangsta + + +

"HOLY COW THIS IS LOW LEVEL"

+ + + ` + io.WriteString(c, "HTTP/1.1 200 OK\r\n") + fmt.Fprintf(c, "Content-Length: %d\r\n", len(body)) + fmt.Fprint(c, "Content-Type: text/html\r\n") + io.WriteString(c, "\r\n") + io.WriteString(c, body) +} diff --git a/022_hands-on/02/17_hands-on/main.go b/022_hands-on/02/17_hands-on/main.go new file mode 100644 index 00000000..197e2263 --- /dev/null +++ b/022_hands-on/02/17_hands-on/main.go @@ -0,0 +1,81 @@ +package main + +import ( + "bufio" + "fmt" + "io" + "log" + "net" + "strings" +) + +func main() { + l, err := net.Listen("tcp", ":8080") + if err != nil { + log.Fatalln(err) + } + defer l.Close() + + for { + conn, err := l.Accept() + if err != nil { + log.Println(err) + continue + } + go serve(conn) + } +} + +func serve(c net.Conn) { + defer c.Close() + var i int + var rMethod, rURI string + scanner := bufio.NewScanner(c) + for scanner.Scan() { + ln := scanner.Text() + fmt.Println(ln) + if i == 0 { + // we're in REQUEST LINE + xs := strings.Fields(ln) + rMethod = xs[0] + rURI = xs[1] + fmt.Println("METHOD:", rMethod) + fmt.Println("URI:", rURI) + } + if ln == "" { + fmt.Println("THIS IS THE END OF THE HTTP REQUEST HEADERS") + break + } + i++ + } + + body := "" + //Add code to respond to the following METHODS & ROUTES: + if rMethod == "GET" && rURI == "/" { + body = "Responding to GET /" + } + if rMethod == "GET" && rURI == "/apply" { + body = "Responding to GET /apply" + } + if rMethod == "POST" && rURI == "/apply" { + body = "Responding to GET /apply" + } + + // body := ` + // + // + // + // + // Code Gangsta + // + // + //

"HOLY COW THIS IS LOW LEVEL"

+ // + // + // ` + io.WriteString(c, "HTTP/1.1 200 OK\r\n") + fmt.Fprintf(c, "Content-Length: %d\r\n", len(body)) + fmt.Fprint(c, "Content-Type: text/html\r\n") + io.WriteString(c, "\r\n") + io.WriteString(c, body) +} diff --git a/029_cookies/01_set_get/main.go b/029_cookies/01_set_get/main.go index 69873249..2859b9f4 100644 --- a/029_cookies/01_set_get/main.go +++ b/029_cookies/01_set_get/main.go @@ -16,7 +16,7 @@ func set(w http.ResponseWriter, req *http.Request) { http.SetCookie(w, &http.Cookie{ Name: "my-cookie", Value: "some value", - Path: "/", + Path: "/", }) fmt.Fprintln(w, "COOKIE WRITTEN - CHECK YOUR BROWSER") fmt.Fprintln(w, "in chrome go to: dev tools / application / cookies") diff --git a/029_cookies/02_multiple/main.go b/029_cookies/02_multiple/main.go index 379d69d2..6df883ea 100644 --- a/029_cookies/02_multiple/main.go +++ b/029_cookies/02_multiple/main.go @@ -18,7 +18,7 @@ func set(w http.ResponseWriter, req *http.Request) { http.SetCookie(w, &http.Cookie{ Name: "my-cookie", Value: "some value", - Path: "/", + Path: "/", }) fmt.Fprintln(w, "COOKIE WRITTEN - CHECK YOUR BROWSER") fmt.Fprintln(w, "in chrome go to: dev tools / application / cookies") diff --git a/029_cookies/04_solution/main.go b/029_cookies/04_solution/main.go index 653ef59b..90445d0e 100644 --- a/029_cookies/04_solution/main.go +++ b/029_cookies/04_solution/main.go @@ -21,7 +21,7 @@ func foo(res http.ResponseWriter, req *http.Request) { cookie = &http.Cookie{ Name: "my-cookie", Value: "0", - Path: "/", + Path: "/", } } diff --git a/029_cookies/05_maxage/main.go b/029_cookies/05_maxage/main.go index 2c8373c5..8d067860 100644 --- a/029_cookies/05_maxage/main.go +++ b/029_cookies/05_maxage/main.go @@ -21,7 +21,7 @@ func set(w http.ResponseWriter, req *http.Request) { http.SetCookie(w, &http.Cookie{ Name: "session", Value: "some value", - Path: "/", + Path: "/", }) fmt.Fprintln(w, `

read

`) } diff --git a/030_sessions/01_uuid/main.go b/030_sessions/01_uuid/main.go index ad36b1f6..7bbd1c84 100644 --- a/030_sessions/01_uuid/main.go +++ b/030_sessions/01_uuid/main.go @@ -18,7 +18,7 @@ func main() { func index(w http.ResponseWriter, req *http.Request) { cookie, err := req.Cookie("session") if err != nil { - id := uuid.NewV4() + id, _ := uuid.NewV4() cookie = &http.Cookie{ Name: "session", Value: id.String(),