diff --git a/000_temp/44_class/03_uuid/main.go b/000_temp/44_class/03_uuid/main.go index 73380fcc..b009522c 100644 --- a/000_temp/44_class/03_uuid/main.go +++ b/000_temp/44_class/03_uuid/main.go @@ -15,7 +15,7 @@ func main() { func foo(w http.ResponseWriter, req *http.Request) { c, err := req.Cookie("session") if err != nil { - sID := uuid.NewV4() + sID, _ := uuid.NewV4() c = &http.Cookie{ Name: "session", Value: sID.String(), diff --git a/000_temp/44_class/04_session/main.go b/000_temp/44_class/04_session/main.go index 121b4fc2..089b10de 100644 --- a/000_temp/44_class/04_session/main.go +++ b/000_temp/44_class/04_session/main.go @@ -29,7 +29,7 @@ func foo(w http.ResponseWriter, req *http.Request) { // get cookie c, err := req.Cookie("session") if err != nil { - sID := uuid.NewV4() + sID, _ := uuid.NewV4() c = &http.Cookie{ Name: "session", Value: sID.String(), diff --git a/000_temp/44_class/07_hands-on_login/starting-code/main.go b/000_temp/44_class/07_hands-on_login/starting-code/main.go index c1d54275..3840d71f 100644 --- a/000_temp/44_class/07_hands-on_login/starting-code/main.go +++ b/000_temp/44_class/07_hands-on_login/starting-code/main.go @@ -35,7 +35,7 @@ func index(w http.ResponseWriter, req *http.Request) { // get cookie c, err := req.Cookie("session") if err != nil { - sID := uuid.NewV4() + sID, _ := uuid.NewV4() c = &http.Cookie{ Name: "session", Value: sID.String(), @@ -97,7 +97,7 @@ func login(w http.ResponseWriter, req *http.Request) { return } // create a session - sID := uuid.NewV4() + sID, _ := uuid.NewV4() c := &http.Cookie{ Name: "session", Value: sID.String(), diff --git a/000_temp/44_class/11_hello-world/main.go b/000_temp/44_class/11_hello-world/main.go index 40076ea6..7c091dc9 100644 --- a/000_temp/44_class/11_hello-world/main.go +++ b/000_temp/44_class/11_hello-world/main.go @@ -1,8 +1,8 @@ package main import ( - "net/http" "io" + "net/http" ) func main() { @@ -12,4 +12,4 @@ func main() { func index(w http.ResponseWriter, r *http.Request) { io.WriteString(w, "Hello world") -} \ No newline at end of file +} diff --git a/000_temp/44_class/13_interface/cache/main.go b/000_temp/44_class/13_interface/cache/main.go index d3459138..ed67f989 100644 --- a/000_temp/44_class/13_interface/cache/main.go +++ b/000_temp/44_class/13_interface/cache/main.go @@ -4,4 +4,3 @@ type Cache interface { Set(k string, val interface{}) Get(k string) interface{} } - diff --git a/000_temp/44_class/13_interface/main.go b/000_temp/44_class/13_interface/main.go index f978b8a2..6875697f 100644 --- a/000_temp/44_class/13_interface/main.go +++ b/000_temp/44_class/13_interface/main.go @@ -1,8 +1,8 @@ package main import ( - "github.com/GoesToEleven/golang-web-dev/000_temp/44_class/13_interface/memcache" "github.com/GoesToEleven/golang-web-dev/000_temp/44_class/13_interface/cmd" + "github.com/GoesToEleven/golang-web-dev/000_temp/44_class/13_interface/memcache" ) func main() { diff --git a/000_temp/44_class/14_data-structure/main.go b/000_temp/44_class/14_data-structure/main.go index b2a8ba8a..f26aa833 100644 --- a/000_temp/44_class/14_data-structure/main.go +++ b/000_temp/44_class/14_data-structure/main.go @@ -6,7 +6,7 @@ import ( type person struct { first string - last string + last string } type secretAgent struct { @@ -47,4 +47,4 @@ func main() { } sa1.greeting() sayMore(sa1) -} \ No newline at end of file +} diff --git a/000_temp/44_class/15/main.go b/000_temp/44_class/15/main.go index 0afb9777..7106dd3f 100644 --- a/000_temp/44_class/15/main.go +++ b/000_temp/44_class/15/main.go @@ -1,9 +1,9 @@ package main import ( - "net/http" - "io" "fmt" + "io" + "net/http" ) func init() { @@ -18,4 +18,4 @@ func main() { func index(w http.ResponseWriter, r *http.Request) { io.WriteString(w, "Hello world") -} \ No newline at end of file +} diff --git a/000_temp/45_pagination/main.go b/000_temp/45_pagination/main.go index b0a1ad50..966b6986 100644 --- a/000_temp/45_pagination/main.go +++ b/000_temp/45_pagination/main.go @@ -1,25 +1,24 @@ package main import ( + "fmt" "html/template" "net/http" - "fmt" "strconv" ) type Env struct { - Data []int - From int - To int - Amount int - ForwardStart int + Data []int + From int + To int + Amount int + ForwardStart int BackwardStart int } var tpl *template.Template var xi []int - func init() { tpl = template.Must(template.ParseGlob("templates/*.gohtml")) } @@ -62,7 +61,7 @@ func index(w http.ResponseWriter, r *http.Request) { // then the next group of records would start at "to" // but only if there are such records var fs int - if to + recordsPerPageToShow <= len(xi) { + if to+recordsPerPageToShow <= len(xi) { fs = to } else { fs = len(xi) - recordsPerPageToShow @@ -73,22 +72,22 @@ func index(w http.ResponseWriter, r *http.Request) { // then the previous group of records would start at "from" minus ten (records shown) // but only if there are such records var bs int - if from - recordsPerPageToShow >= 0 { + if from-recordsPerPageToShow >= 0 { bs = from - recordsPerPageToShow } else { bs = 0 } data := Env{ - Data: xx, - From: from, - To: to - 1, - Amount: recordsPerPageToShow, - ForwardStart: fs, + Data: xx, + From: from, + To: to - 1, + Amount: recordsPerPageToShow, + ForwardStart: fs, BackwardStart: bs, } err = tpl.ExecuteTemplate(w, "index.gohtml", data) if err != nil { fmt.Println(err) } -} \ No newline at end of file +} diff --git a/000_temp/46_sp17/01/main.go b/000_temp/46_sp17/01/main.go index ad6e0c87..11327e8c 100644 --- a/000_temp/46_sp17/01/main.go +++ b/000_temp/46_sp17/01/main.go @@ -1,8 +1,8 @@ package main import ( - "net/http" "io" + "net/http" ) func main() { diff --git a/000_temp/46_sp17/02/main.go b/000_temp/46_sp17/02/main.go index aebc7b46..e1479422 100644 --- a/000_temp/46_sp17/02/main.go +++ b/000_temp/46_sp17/02/main.go @@ -10,4 +10,4 @@ func main() { func foo() { fmt.Println("hello") -} \ No newline at end of file +} diff --git a/000_temp/46_sp17/03_handle_handler_handlefunc/01_handle/main.go b/000_temp/46_sp17/03_handle_handler_handlefunc/01_handle/main.go index d04712f9..25d43891 100644 --- a/000_temp/46_sp17/03_handle_handler_handlefunc/01_handle/main.go +++ b/000_temp/46_sp17/03_handle_handler_handlefunc/01_handle/main.go @@ -1,13 +1,13 @@ package main import ( - "net/http" "io" + "net/http" ) type hotdog int -func(hotdog) ServeHTTP(w http.ResponseWriter, r *http.Request) { +func (hotdog) ServeHTTP(w http.ResponseWriter, r *http.Request) { io.WriteString(w, "Hello from hotdogger") } diff --git a/000_temp/46_sp17/03_handle_handler_handlefunc/02_handlefunc/main.go b/000_temp/46_sp17/03_handle_handler_handlefunc/02_handlefunc/main.go index 5fe09f46..f21302b4 100644 --- a/000_temp/46_sp17/03_handle_handler_handlefunc/02_handlefunc/main.go +++ b/000_temp/46_sp17/03_handle_handler_handlefunc/02_handlefunc/main.go @@ -1,8 +1,8 @@ package main import ( - "net/http" "io" + "net/http" ) func main() { @@ -12,4 +12,4 @@ func main() { func foo(w http.ResponseWriter, r *http.Request) { io.WriteString(w, "Hello from hotdiggity dogger") -} \ No newline at end of file +} diff --git a/000_temp/46_sp17/03_handle_handler_handlefunc/03_handlerfunc/main.go b/000_temp/46_sp17/03_handle_handler_handlefunc/03_handlerfunc/main.go index 2951ffdd..a8448ce7 100644 --- a/000_temp/46_sp17/03_handle_handler_handlefunc/03_handlerfunc/main.go +++ b/000_temp/46_sp17/03_handle_handler_handlefunc/03_handlerfunc/main.go @@ -1,8 +1,8 @@ package main import ( - "net/http" "io" + "net/http" ) func main() { @@ -13,4 +13,3 @@ func main() { func foo(w http.ResponseWriter, r *http.Request) { io.WriteString(w, "Holy diggity dogger") } - diff --git a/000_temp/46_sp17/04/main.go b/000_temp/46_sp17/04/main.go index e24da7ef..ecc7100c 100644 --- a/000_temp/46_sp17/04/main.go +++ b/000_temp/46_sp17/04/main.go @@ -3,6 +3,6 @@ package main import "fmt" func main() { - m := map[string]int{"Bond":32, "Moneypenny": 24} + m := map[string]int{"Bond": 32, "Moneypenny": 24} fmt.Println(m) -} \ No newline at end of file +} diff --git a/000_temp/46_sp17/05/main.go b/000_temp/46_sp17/05/main.go index b3ae853c..d28c1f94 100644 --- a/000_temp/46_sp17/05/main.go +++ b/000_temp/46_sp17/05/main.go @@ -10,4 +10,4 @@ func main() { func foo(x int) { fmt.Println("hello", x) -} \ No newline at end of file +} diff --git a/000_temp/46_sp17/06/main.go b/000_temp/46_sp17/06/main.go index ef271fa8..fc739a16 100644 --- a/000_temp/46_sp17/06/main.go +++ b/000_temp/46_sp17/06/main.go @@ -11,5 +11,5 @@ func main() { } func foo() { - fmt.Println(z,z,z,z,z) -} \ No newline at end of file + fmt.Println(z, z, z, z, z) +} diff --git a/000_temp/46_sp17/07_bond/main.go b/000_temp/46_sp17/07_bond/main.go index ae6a9395..7fa71c24 100644 --- a/000_temp/46_sp17/07_bond/main.go +++ b/000_temp/46_sp17/07_bond/main.go @@ -29,10 +29,9 @@ func foo(h human) { fmt.Println("hello from foo") } - func main() { p1 := person{"Nina", "Simone", 25} sa1 := secretAgent{person{"Ian", "Fleming", 42}, false} foo(p1) foo(sa1) -} \ No newline at end of file +} diff --git a/000_temp/46_sp17/08_notfound-notfoundhandler/main.go b/000_temp/46_sp17/08_notfound-notfoundhandler/main.go index 9edc7563..22b89116 100644 --- a/000_temp/46_sp17/08_notfound-notfoundhandler/main.go +++ b/000_temp/46_sp17/08_notfound-notfoundhandler/main.go @@ -10,4 +10,4 @@ func main() { func index(w http.ResponseWriter, r *http.Request) { http.NotFound(w, r) -} \ No newline at end of file +} diff --git a/000_temp/46_sp17/09_fundamentals/01/main.go b/000_temp/46_sp17/09_fundamentals/01/main.go index dedb4201..b8c285f4 100644 --- a/000_temp/46_sp17/09_fundamentals/01/main.go +++ b/000_temp/46_sp17/09_fundamentals/01/main.go @@ -15,4 +15,4 @@ func main() { p2 := person{"Bond"} p1.speak() p2.speak() -} \ No newline at end of file +} diff --git a/000_temp/46_sp17/09_fundamentals/02/main.go b/000_temp/46_sp17/09_fundamentals/02/main.go index c4e3f090..93acc138 100644 --- a/000_temp/46_sp17/09_fundamentals/02/main.go +++ b/000_temp/46_sp17/09_fundamentals/02/main.go @@ -3,17 +3,17 @@ package main import "fmt" func main() { - xi := []int{4,3,5,7,3,4,5} + xi := []int{4, 3, 5, 7, 3, 4, 5} fmt.Println(xi) for i, v := range xi { fmt.Println(i, v) } - m := map[string]int{"mcleod":42, "bond":32} + m := map[string]int{"mcleod": 42, "bond": 32} fmt.Println(m) for k, v := range m { fmt.Println(k, v) } -} \ No newline at end of file +} diff --git a/000_temp/46_sp17/09_fundamentals/03/main.go b/000_temp/46_sp17/09_fundamentals/03/main.go index b747d020..a0670d16 100644 --- a/000_temp/46_sp17/09_fundamentals/03/main.go +++ b/000_temp/46_sp17/09_fundamentals/03/main.go @@ -2,7 +2,7 @@ package main import "fmt" -type human interface{ +type human interface { speak() } @@ -33,4 +33,4 @@ func main() { foo(p1) foo(p2) foo(c1) -} \ No newline at end of file +} diff --git a/000_temp/46_sp17/11/main.go b/000_temp/46_sp17/11/main.go index f9a4b1d8..587e74d0 100644 --- a/000_temp/46_sp17/11/main.go +++ b/000_temp/46_sp17/11/main.go @@ -1,8 +1,8 @@ package main import ( - "net/http" "fmt" + "net/http" ) func main() { diff --git a/000_temp/46_sp17/12/main.go b/000_temp/46_sp17/12/main.go index 08a89660..5ff7b320 100644 --- a/000_temp/46_sp17/12/main.go +++ b/000_temp/46_sp17/12/main.go @@ -1,8 +1,8 @@ package main import ( - "net/http" "fmt" + "net/http" ) func main() { @@ -34,7 +34,6 @@ Hello from index `) } - func about(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, ` @@ -57,7 +56,6 @@ Hello from about `) } - func contact(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, ` diff --git a/000_temp/46_sp17/13/main.go b/000_temp/46_sp17/13/main.go index 6173f7f4..5ea0c485 100644 --- a/000_temp/46_sp17/13/main.go +++ b/000_temp/46_sp17/13/main.go @@ -1,8 +1,8 @@ package main import ( - "net/http" "html/template" + "net/http" ) var tpl *template.Template @@ -22,12 +22,10 @@ func index(w http.ResponseWriter, r *http.Request) { tpl.ExecuteTemplate(w, "index.gohtml", nil) } - func about(w http.ResponseWriter, r *http.Request) { tpl.ExecuteTemplate(w, "about.gohtml", nil) } - func contact(w http.ResponseWriter, r *http.Request) { tpl.ExecuteTemplate(w, "contact.gohtml", nil) } diff --git a/000_temp/46_sp17/14/main.go b/000_temp/46_sp17/14/main.go index a169100f..afff1f5d 100644 --- a/000_temp/46_sp17/14/main.go +++ b/000_temp/46_sp17/14/main.go @@ -1,8 +1,8 @@ package main import ( - "net/http" "html/template" + "net/http" ) var tpl *template.Template @@ -18,4 +18,4 @@ func main() { func index(w http.ResponseWriter, r *http.Request) { tpl.ExecuteTemplate(w, "index.gohtml", nil) -} \ No newline at end of file +} diff --git a/000_temp/46_sp17/15/main.go b/000_temp/46_sp17/15/main.go index 345bce8b..2b12224e 100644 --- a/000_temp/46_sp17/15/main.go +++ b/000_temp/46_sp17/15/main.go @@ -1,8 +1,8 @@ package main import ( - "net/http" "html/template" + "net/http" ) var tpl *template.Template @@ -18,4 +18,4 @@ func main() { func index(w http.ResponseWriter, r *http.Request) { tpl.ExecuteTemplate(w, "index.gohtml", 42) -} \ No newline at end of file +} diff --git a/000_temp/46_sp17/16/main.go b/000_temp/46_sp17/16/main.go index f858b853..7d5f90b9 100644 --- a/000_temp/46_sp17/16/main.go +++ b/000_temp/46_sp17/16/main.go @@ -1,8 +1,8 @@ package main import ( - "net/http" "html/template" + "net/http" ) var tpl *template.Template @@ -18,4 +18,4 @@ func main() { func index(w http.ResponseWriter, r *http.Request) { tpl.ExecuteTemplate(w, "index.gohtml", "Todd") -} \ No newline at end of file +} diff --git a/000_temp/46_sp17/17/main.go b/000_temp/46_sp17/17/main.go index 738d37b0..ebb0316b 100644 --- a/000_temp/46_sp17/17/main.go +++ b/000_temp/46_sp17/17/main.go @@ -1,15 +1,15 @@ package main import ( - "net/http" "html/template" + "net/http" ) var tpl *template.Template type person struct { First string - Age int + Age int } func init() { @@ -27,4 +27,4 @@ func index(w http.ResponseWriter, r *http.Request) { 45, } tpl.ExecuteTemplate(w, "index.gohtml", p1) -} \ No newline at end of file +} diff --git a/000_temp/46_sp17/18/main.go b/000_temp/46_sp17/18/main.go index 1f7fb64e..5ea0c485 100644 --- a/000_temp/46_sp17/18/main.go +++ b/000_temp/46_sp17/18/main.go @@ -1,8 +1,8 @@ package main import ( - "net/http" "html/template" + "net/http" ) var tpl *template.Template @@ -28,4 +28,4 @@ func about(w http.ResponseWriter, r *http.Request) { func contact(w http.ResponseWriter, r *http.Request) { tpl.ExecuteTemplate(w, "contact.gohtml", nil) -} \ No newline at end of file +} diff --git a/000_temp/46_sp17/19/main.go b/000_temp/46_sp17/19/main.go index b04c0aff..c525d24c 100644 --- a/000_temp/46_sp17/19/main.go +++ b/000_temp/46_sp17/19/main.go @@ -1,8 +1,8 @@ package main import ( - "net/http" "io" + "net/http" ) func main() { diff --git a/000_temp/46_sp17/20-string-int/main.go b/000_temp/46_sp17/20-string-int/main.go index d6c59c92..5376afb5 100644 --- a/000_temp/46_sp17/20-string-int/main.go +++ b/000_temp/46_sp17/20-string-int/main.go @@ -1,8 +1,8 @@ package main import ( - "net/http" "html/template" + "net/http" ) var tpl *template.Template @@ -24,4 +24,4 @@ func index(w http.ResponseWriter, r *http.Request) { func about(w http.ResponseWriter, r *http.Request) { tpl.ExecuteTemplate(w, "about.gohtml", 42) -} \ No newline at end of file +} diff --git a/000_temp/46_sp17/21-struct-slice-map/main.go b/000_temp/46_sp17/21-struct-slice-map/main.go index 2ff72dd3..d9535c9f 100644 --- a/000_temp/46_sp17/21-struct-slice-map/main.go +++ b/000_temp/46_sp17/21-struct-slice-map/main.go @@ -1,13 +1,13 @@ package main import ( - "net/http" "html/template" + "net/http" ) type person struct { First string - Last string + Last string } var tpl *template.Template @@ -34,14 +34,14 @@ func index(w http.ResponseWriter, r *http.Request) { } func about(w http.ResponseWriter, r *http.Request) { - xi := []int{3,5,7,9,17,749} + xi := []int{3, 5, 7, 9, 17, 749} tpl.ExecuteTemplate(w, "about.gohtml", xi) } func contact(w http.ResponseWriter, r *http.Request) { m := map[string]int{ - "James":32, - "Moneypenny":24, + "James": 32, + "Moneypenny": 24, } tpl.ExecuteTemplate(w, "contact.gohtml", m) -} \ No newline at end of file +} diff --git a/000_temp/46_sp17/28/main.go b/000_temp/46_sp17/28/main.go index fe1fea93..9823dd23 100644 --- a/000_temp/46_sp17/28/main.go +++ b/000_temp/46_sp17/28/main.go @@ -6,7 +6,7 @@ import ( ) type person struct { - Name string + Name string Prefs []string } @@ -23,10 +23,10 @@ func main() { } func index(w http.ResponseWriter, r *http.Request) { - p1 := person{"bond", []string{"martinis", "shaken not stirred", "fast cars", "intelligence",},} - p2 := person{"moneypenny", []string{"bond",},} - p3 := person{"Q", []string{"gadgets", "bond",},} - xp := []person{p1,p2,p3} + p1 := person{"bond", []string{"martinis", "shaken not stirred", "fast cars", "intelligence"}} + p2 := person{"moneypenny", []string{"bond"}} + p3 := person{"Q", []string{"gadgets", "bond"}} + xp := []person{p1, p2, p3} tpl.ExecuteTemplate(w, "index.gohtml", xp) } diff --git a/000_temp/46_sp17/34_ServeFile/main.go b/000_temp/46_sp17/34_ServeFile/main.go index ed83667f..eda4fedc 100644 --- a/000_temp/46_sp17/34_ServeFile/main.go +++ b/000_temp/46_sp17/34_ServeFile/main.go @@ -1,8 +1,8 @@ package main import ( - "net/http" "io" + "net/http" ) func main() { @@ -18,4 +18,4 @@ func index(w http.ResponseWriter, r *http.Request) { func toby(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, "toby.jpg") -} \ No newline at end of file +} diff --git a/000_temp/46_sp17/35_ServeFile/main.go b/000_temp/46_sp17/35_ServeFile/main.go index 4f47b902..5c10c696 100644 --- a/000_temp/46_sp17/35_ServeFile/main.go +++ b/000_temp/46_sp17/35_ServeFile/main.go @@ -1,8 +1,8 @@ package main import ( - "net/http" "io" + "net/http" ) func main() { diff --git a/000_temp/46_sp17/36_ServeFile/main.go b/000_temp/46_sp17/36_ServeFile/main.go index cdf5bea7..aa5347f5 100644 --- a/000_temp/46_sp17/36_ServeFile/main.go +++ b/000_temp/46_sp17/36_ServeFile/main.go @@ -1,8 +1,8 @@ package main import ( - "net/http" "io" + "net/http" ) func main() { @@ -35,4 +35,4 @@ func css(w http.ResponseWriter, r *http.Request) { func bali(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, "public/img/bali.jpg") -} \ No newline at end of file +} diff --git a/000_temp/46_sp17/37_FileServer/main.go b/000_temp/46_sp17/37_FileServer/main.go index 7b0b2194..56e70d61 100644 --- a/000_temp/46_sp17/37_FileServer/main.go +++ b/000_temp/46_sp17/37_FileServer/main.go @@ -1,8 +1,8 @@ package main import ( - "net/http" "io" + "net/http" ) func main() { @@ -35,4 +35,4 @@ func css(w http.ResponseWriter, r *http.Request) { func bali(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, "public/img/bali.jpg") -} \ No newline at end of file +} diff --git a/000_temp/46_sp17/38_template/main.go b/000_temp/46_sp17/38_template/main.go index 6ce0fcdb..abdfb445 100644 --- a/000_temp/46_sp17/38_template/main.go +++ b/000_temp/46_sp17/38_template/main.go @@ -1,8 +1,8 @@ package main import ( - "net/http" "html/template" + "net/http" ) var tpl *template.Template @@ -20,4 +20,4 @@ func main() { func index(w http.ResponseWriter, r *http.Request) { tpl.ExecuteTemplate(w, "index.gohtml", nil) -} \ No newline at end of file +} diff --git a/000_temp/46_sp17/39_template-vars/main.go b/000_temp/46_sp17/39_template-vars/main.go index 3edd3370..105277f2 100644 --- a/000_temp/46_sp17/39_template-vars/main.go +++ b/000_temp/46_sp17/39_template-vars/main.go @@ -1,8 +1,8 @@ package main import ( - "net/http" "html/template" + "net/http" ) var tpl *template.Template @@ -24,6 +24,6 @@ func index(w http.ResponseWriter, r *http.Request) { } func vars(w http.ResponseWriter, r *http.Request) { - xs := []string{"James", "Moneypenny", "Q", "M",} + xs := []string{"James", "Moneypenny", "Q", "M"} tpl.ExecuteTemplate(w, "vars.gohtml", xs) -} \ No newline at end of file +} diff --git a/000_temp/46_sp17/42_cookie/01/main.go b/000_temp/46_sp17/42_cookie/01/main.go index 09d6ac33..ce846b2d 100644 --- a/000_temp/46_sp17/42_cookie/01/main.go +++ b/000_temp/46_sp17/42_cookie/01/main.go @@ -1,9 +1,9 @@ package main import ( - "net/http" - "html/template" "fmt" + "html/template" + "net/http" ) var tpl *template.Template @@ -32,9 +32,9 @@ func index(w http.ResponseWriter, r *http.Request) { func bowzer(w http.ResponseWriter, r *http.Request) { c := &http.Cookie{ - Name: "user-cookie", + Name: "user-cookie", Value: "this would be the value", - Path: "/dog/bowzer", + Path: "/dog/bowzer", } http.SetCookie(w, c) tpl.ExecuteTemplate(w, "bowzer.gohtml", c) @@ -56,4 +56,4 @@ func cat(w http.ResponseWriter, r *http.Request) { fmt.Printf("%T\n", c) } tpl.ExecuteTemplate(w, "cat.gohtml", c) -} \ No newline at end of file +} diff --git a/000_temp/46_sp17/42_cookie/02/main.go b/000_temp/46_sp17/42_cookie/02/main.go index bc2b7937..bb9726b8 100644 --- a/000_temp/46_sp17/42_cookie/02/main.go +++ b/000_temp/46_sp17/42_cookie/02/main.go @@ -1,9 +1,9 @@ package main import ( - "net/http" - "html/template" "fmt" + "html/template" + "net/http" ) var tpl *template.Template @@ -32,9 +32,9 @@ func index(w http.ResponseWriter, r *http.Request) { func bowzer(w http.ResponseWriter, r *http.Request) { c := &http.Cookie{ - Name: "user-cookie", + Name: "user-cookie", Value: "this would be the value", - Path: "/", + Path: "/", } http.SetCookie(w, c) tpl.ExecuteTemplate(w, "bowzer.gohtml", c) @@ -56,4 +56,4 @@ func cat(w http.ResponseWriter, r *http.Request) { fmt.Printf("%T\n", c) } tpl.ExecuteTemplate(w, "cat.gohtml", c) -} \ No newline at end of file +} diff --git a/000_temp/46_sp17/43_cookie/main.go b/000_temp/46_sp17/43_cookie/main.go index 4977bd40..6f35e088 100644 --- a/000_temp/46_sp17/43_cookie/main.go +++ b/000_temp/46_sp17/43_cookie/main.go @@ -1,9 +1,9 @@ package main import ( - "net/http" - "html/template" "github.com/satori/go.uuid" + "html/template" + "net/http" ) var tpl *template.Template @@ -22,9 +22,9 @@ func index(w http.ResponseWriter, r *http.Request) { if err != nil { u := uuid.NewV4() c = &http.Cookie{ - Name: "session", - Value: u.String(), - Path: "/", + Name: "session", + Value: u.String(), + Path: "/", HttpOnly: true, } } diff --git a/000_temp/46_sp17/44_map/main.go b/000_temp/46_sp17/44_map/main.go index 91279606..0cb04f35 100644 --- a/000_temp/46_sp17/44_map/main.go +++ b/000_temp/46_sp17/44_map/main.go @@ -1,23 +1,23 @@ package main import ( - "net/http" - "html/template" - "github.com/satori/go.uuid" "fmt" + "github.com/satori/go.uuid" + "html/template" + "net/http" ) type user struct { - Id string - First string - Last string - Email string + Id string + First string + Last string + Email string Password string } var tpl *template.Template -var muser = map[string]user{} // key is userid, value is user +var muser = map[string]user{} // key is userid, value is user var msession = map[string]string{} // key is sessionid, value is userid func init() { @@ -39,9 +39,9 @@ func index(w http.ResponseWriter, r *http.Request) { if err != nil { u := uuid.NewV4() c = &http.Cookie{ - Name: "mysess", - Value: u.String(), - Path: "/", + Name: "mysess", + Value: u.String(), + Path: "/", HttpOnly: true, } } @@ -57,13 +57,8 @@ func index(w http.ResponseWriter, r *http.Request) { func other(w http.ResponseWriter, r *http.Request) { c, err := r.Cookie("mysess") if err != nil { - u := uuid.NewV4() - c = &http.Cookie{ - Name: "mysess", - Value: u.String(), - Path: "/", - HttpOnly: true, - } + http.Redirect(w, r, "/", http.StatusSeeOther) + return } uid := msession[c.Value] usr := muser[uid] @@ -143,10 +138,10 @@ func signup(w http.ResponseWriter, r *http.Request) { pw := r.FormValue("password") u := uuid.NewV4() usr = user{ - Id: u.String(), - First: fn, - Last: ln, - Email: em, + Id: u.String(), + First: fn, + Last: ln, + Email: em, Password: pw, } muser[usr.Id] = usr @@ -156,4 +151,4 @@ func signup(w http.ResponseWriter, r *http.Request) { } tpl.ExecuteTemplate(w, "signup.gohtml", nil) -} \ No newline at end of file +} diff --git a/000_temp/46_sp17/45_session/01_cookie/main.go b/000_temp/46_sp17/45_session/01_cookie/main.go new file mode 100644 index 00000000..5046f745 --- /dev/null +++ b/000_temp/46_sp17/45_session/01_cookie/main.go @@ -0,0 +1,29 @@ +package main + +import ( + "fmt" + "github.com/satori/go.uuid" + "net/http" +) + +func main() { + http.HandleFunc("/", index) + http.Handle("/favicon.ico", http.NotFoundHandler()) + http.ListenAndServe(":8080", nil) +} + +func index(w http.ResponseWriter, r *http.Request) { + cookie, err := r.Cookie("session") + if err != nil { + id := uuid.NewV4() + cookie = &http.Cookie{ + Name: "session", + Value: id.String(), + HttpOnly: true, + Path: "/", + } + http.SetCookie(w, cookie) + } + fmt.Println(cookie) + fmt.Fprint(w, cookie) +} diff --git a/000_temp/46_sp17/45_session/02_session/main.go b/000_temp/46_sp17/45_session/02_session/main.go new file mode 100644 index 00000000..a598ab5b --- /dev/null +++ b/000_temp/46_sp17/45_session/02_session/main.go @@ -0,0 +1,76 @@ +package main + +import ( + "github.com/satori/go.uuid" + "html/template" + "net/http" +) + +type user struct { + UserName string + First string + Last string +} + +var tpl *template.Template +var dbUsers = map[string]user{} // user ID, user +var dbSessions = map[string]string{} // session ID, user ID (email) + +func init() { + tpl = template.Must(template.ParseGlob("templates/*.gohtml")) +} + +func main() { + http.HandleFunc("/", index) + http.HandleFunc("/bar", bar) + http.Handle("/favicon.ico", http.NotFoundHandler()) + http.ListenAndServe(":8080", nil) +} + +func index(w http.ResponseWriter, r *http.Request) { + c, err := r.Cookie("session") + if err != nil { + id := uuid.NewV4() + c = &http.Cookie{ + Name: "session", + Value: id.String(), + HttpOnly: true, + Path: "/", + } + http.SetCookie(w, c) + } + + var u user + if uid, ok := dbSessions[c.Value]; ok { + u = dbUsers[uid] + } + + // process form submission + if r.Method == http.MethodPost { + e := r.FormValue("email") + f := r.FormValue("first") + l := r.FormValue("last") + u = user{e, f, l} + dbSessions[c.Value] = e + dbUsers[e] = u + } + + tpl.ExecuteTemplate(w, "index.gohtml", u) +} + +func bar(w http.ResponseWriter, r *http.Request) { + c, err := r.Cookie("session") + if err != nil { + http.Redirect(w, r, "/", http.StatusSeeOther) + return + } + + e, ok := dbSessions[c.Value] + if !ok { + http.Redirect(w, r, "/", http.StatusSeeOther) + return + } + + u := dbUsers[e] + tpl.ExecuteTemplate(w, "bar.gohtml", u) +} diff --git a/000_temp/46_sp17/45_session/02_session/templates/bar.gohtml b/000_temp/46_sp17/45_session/02_session/templates/bar.gohtml new file mode 100644 index 00000000..884d3edd --- /dev/null +++ b/000_temp/46_sp17/45_session/02_session/templates/bar.gohtml @@ -0,0 +1,23 @@ + + + + + + + BAR PAGE + + + +

WELCOME TO THE BAR!

+ +{{if .First}} +Username {{.UserName}}
+First {{.First}}
+Last {{.Last}}
+{{end}} + +go to index + + + \ No newline at end of file diff --git a/000_temp/46_sp17/45_session/02_session/templates/index.gohtml b/000_temp/46_sp17/45_session/02_session/templates/index.gohtml new file mode 100644 index 00000000..49a26528 --- /dev/null +++ b/000_temp/46_sp17/45_session/02_session/templates/index.gohtml @@ -0,0 +1,28 @@ + + + + + + + INDEX PAGE + + + +{{if .First}} +Username {{.UserName}}
+First {{.First}}
+Last {{.Last}}
+{{end}} + +
+ + + + +
+ +go to bar + + + \ No newline at end of file diff --git a/000_temp/46_sp17/45_session/03_signup/main.go b/000_temp/46_sp17/45_session/03_signup/main.go new file mode 100644 index 00000000..c71be7c9 --- /dev/null +++ b/000_temp/46_sp17/45_session/03_signup/main.go @@ -0,0 +1,86 @@ +package main + +import ( + "github.com/satori/go.uuid" + "html/template" + "net/http" +) + +type user struct { + UserName string + Password string + First string + Last string +} + +var tpl *template.Template +var dbUsers = map[string]user{} // user ID, user +var dbSessions = map[string]string{} // session ID, user ID (email) + +func init() { + tpl = template.Must(template.ParseGlob("templates/*.gohtml")) +} + +func main() { + http.HandleFunc("/", index) + http.HandleFunc("/signup", signup) + http.HandleFunc("/bar", bar) + http.Handle("/favicon.ico", http.NotFoundHandler()) + http.ListenAndServe(":8080", nil) +} + +func index(w http.ResponseWriter, r *http.Request) { + u := getUser(w, r) + tpl.ExecuteTemplate(w, "index.gohtml", u) +} + +func signup(w http.ResponseWriter, r *http.Request) { + if alreadyLoggedIn(r) { + http.Redirect(w, r, "/", http.StatusSeeOther) + return + } + + // process form submission + if r.Method == http.MethodPost { + e := r.FormValue("email") + p := r.FormValue("password") + f := r.FormValue("first") + l := r.FormValue("last") + + // is username taken, eg, user already signed up? + if _, ok := dbUsers[e]; ok { + http.Error(w, "Username already taken", http.StatusForbidden) + return + } + + // create session + id := uuid.NewV4() + c := &http.Cookie{ + Name: "session", + Value: id.String(), + HttpOnly: true, + Path: "/", + } + http.SetCookie(w, c) + dbSessions[c.Value] = e + + // store user in dbUsers + u := user{e, p, f, l} + dbUsers[e] = u + + // all done processing form submission + http.Redirect(w, r, "/", http.StatusSeeOther) + return + } + + tpl.ExecuteTemplate(w, "signup.gohtml", nil) +} + +func bar(w http.ResponseWriter, r *http.Request) { + if !alreadyLoggedIn(r) { + http.Redirect(w, r, "/", http.StatusSeeOther) + return + } + u := getUser(w, r) + tpl.ExecuteTemplate(w, "bar.gohtml", u) +} diff --git a/000_temp/46_sp17/45_session/03_signup/session.go b/000_temp/46_sp17/45_session/03_signup/session.go new file mode 100644 index 00000000..6cf7975b --- /dev/null +++ b/000_temp/46_sp17/45_session/03_signup/session.go @@ -0,0 +1,36 @@ +package main + +import ( + "github.com/satori/go.uuid" + "net/http" +) + +func getUser(w http.ResponseWriter, r *http.Request) user { + c, err := r.Cookie("session") + if err != nil { + id := uuid.NewV4() + c = &http.Cookie{ + Name: "session", + Value: id.String(), + HttpOnly: true, + Path: "/", + } + http.SetCookie(w, c) + } + + var u user + if e, ok := dbSessions[c.Value]; ok { + u = dbUsers[e] + } + return u +} + +func alreadyLoggedIn(r *http.Request) bool { + c, err := r.Cookie("session") + if err != nil { + return false + } + e := dbSessions[c.Value] + _, ok := dbUsers[e] + return ok +} diff --git a/000_temp/46_sp17/45_session/03_signup/templates/bar.gohtml b/000_temp/46_sp17/45_session/03_signup/templates/bar.gohtml new file mode 100644 index 00000000..884d3edd --- /dev/null +++ b/000_temp/46_sp17/45_session/03_signup/templates/bar.gohtml @@ -0,0 +1,23 @@ + + + + + + + BAR PAGE + + + +

WELCOME TO THE BAR!

+ +{{if .First}} +Username {{.UserName}}
+First {{.First}}
+Last {{.Last}}
+{{end}} + +go to index + + + \ No newline at end of file diff --git a/000_temp/46_sp17/45_session/03_signup/templates/index.gohtml b/000_temp/46_sp17/45_session/03_signup/templates/index.gohtml new file mode 100644 index 00000000..9da7b4f2 --- /dev/null +++ b/000_temp/46_sp17/45_session/03_signup/templates/index.gohtml @@ -0,0 +1,22 @@ + + + + + + + INDEX PAGE + + + +{{if .First}} +Username {{.UserName}}
+First {{.First}}
+Last {{.Last}}
+{{end}} + +go to signup +go to bar + + + \ No newline at end of file diff --git a/000_temp/46_sp17/45_session/03_signup/templates/signup.gohtml b/000_temp/46_sp17/45_session/03_signup/templates/signup.gohtml new file mode 100644 index 00000000..5748cd84 --- /dev/null +++ b/000_temp/46_sp17/45_session/03_signup/templates/signup.gohtml @@ -0,0 +1,23 @@ + + + + + + + SIGNUP + + + +
+ + + + + +
+ +go to index + + + \ No newline at end of file diff --git a/000_temp/46_sp17/46_cookie/main.go b/000_temp/46_sp17/46_cookie/main.go new file mode 100644 index 00000000..1d731a2b --- /dev/null +++ b/000_temp/46_sp17/46_cookie/main.go @@ -0,0 +1,32 @@ +package main + +import ( + "io" + "net/http" +) + +func main() { + http.HandleFunc("/", foo) + http.HandleFunc("/bar", bar) + http.ListenAndServe(":8080", nil) +} + +func foo(w http.ResponseWriter, r *http.Request) { + c := &http.Cookie{ + Name: "wasssup", + Value: "tecate", + Path: "/", + HttpOnly: true, + } + http.SetCookie(w, c) + io.WriteString(w, "

hello

") +} + +func bar(w http.ResponseWriter, r *http.Request) { + c, err := r.Cookie("wasssup") + if err != nil { + http.Redirect(w, r, "/", http.StatusSeeOther) + return + } + io.WriteString(w, c.Value) +} diff --git a/000_temp/46_sp17/47_JSON/main.go b/000_temp/46_sp17/47_JSON/main.go new file mode 100644 index 00000000..048b1e1d --- /dev/null +++ b/000_temp/46_sp17/47_JSON/main.go @@ -0,0 +1,33 @@ +package main + +import ( + "encoding/json" + "fmt" +) + +type person struct { + First string + Last string + Ltk bool + Items []string +} + +func main() { + p1 := person{ + First: "James", + Last: "Bond", + Ltk: true, + Items: []string{ + "Martini", + "Walther PPK", + "Astin Martin", + }, + } + bs, err := json.Marshal(p1) + if err != nil { + fmt.Println(err) + } + fmt.Println(p1) + fmt.Println("------------") + fmt.Println(string(bs)) +} diff --git a/000_temp/46_sp17/48_json/01_marshal/main.go b/000_temp/46_sp17/48_json/01_marshal/main.go new file mode 100644 index 00000000..3099ab33 --- /dev/null +++ b/000_temp/46_sp17/48_json/01_marshal/main.go @@ -0,0 +1,33 @@ +package main + +import ( + "encoding/json" + "fmt" +) + +type person struct { + First string + Last string +} + +func main() { + p1 := person{ + First: "James", + Last: "Bond", + } + + p2 := person{ + First: "Miss", + Last: "Moneypenny", + } + + xp := []person{p1, p2} + + fmt.Printf("go data: %+v\n", xp) + + bs, err := json.Marshal(xp) + if err != nil { + fmt.Println(err) + } + fmt.Println("json:", string(bs)) +} diff --git a/000_temp/46_sp17/48_json/02_unmarshal/main.go b/000_temp/46_sp17/48_json/02_unmarshal/main.go new file mode 100644 index 00000000..a27aecaa --- /dev/null +++ b/000_temp/46_sp17/48_json/02_unmarshal/main.go @@ -0,0 +1,29 @@ +package main + +import ( + "encoding/json" + "fmt" +) + +type person struct { + First string + Last string +} + +func main() { + j := `[{"First":"James","Last":"Bond"},{"First":"Miss","Last":"Moneypenny"}]` + fmt.Println("json:", j) + + xp := []person{} + + err := json.Unmarshal([]byte(j), &xp) + if err != nil { + fmt.Println(err) + } + fmt.Printf("go data: %+v\n", xp) + + for i, v := range xp { + fmt.Println(i, v) + fmt.Println("\t", v.First) + } +} diff --git a/000_temp/46_sp17/48_json/03_tags/main.go b/000_temp/46_sp17/48_json/03_tags/main.go new file mode 100644 index 00000000..d513161d --- /dev/null +++ b/000_temp/46_sp17/48_json/03_tags/main.go @@ -0,0 +1,29 @@ +package main + +import ( + "encoding/json" + "fmt" +) + +type person struct { + First string `json:"fname"` + Last string +} + +func main() { + j := `[{"fname":"James","last":"Bond"},{"fname":"Miss","last":"Moneypenny"}]` + fmt.Println("json:", j) + + xp := []person{} + + err := json.Unmarshal([]byte(j), &xp) + if err != nil { + fmt.Println(err) + } + fmt.Printf("go data: %+v\n", xp) + + for i, v := range xp { + fmt.Println(i, v) + fmt.Println("\t", v.First) + } +} diff --git a/000_temp/46_sp17/48_json/04_encode/main.go b/000_temp/46_sp17/48_json/04_encode/main.go new file mode 100644 index 00000000..aa7dae49 --- /dev/null +++ b/000_temp/46_sp17/48_json/04_encode/main.go @@ -0,0 +1,36 @@ +package main + +import ( + "encoding/json" + "fmt" + "net/http" +) + +type person struct { + First string + Last string +} + +func main() { + http.HandleFunc("/", index) + http.ListenAndServe(":8080", nil) +} + +func index(w http.ResponseWriter, r *http.Request) { + p1 := person{ + First: "James", + Last: "Bond", + } + + p2 := person{ + First: "Miss", + Last: "Moneypenny", + } + + xp := []person{p1, p2} + + err := json.NewEncoder(w).Encode(xp) + if err != nil { + fmt.Println(err) + } +} diff --git a/000_temp/46_sp17/48_json/05_decode/main.go b/000_temp/46_sp17/48_json/05_decode/main.go new file mode 100644 index 00000000..dbafdb3e --- /dev/null +++ b/000_temp/46_sp17/48_json/05_decode/main.go @@ -0,0 +1,66 @@ +package main + +import ( + "encoding/json" + "fmt" + "html/template" + "net/http" +) + +type person struct { + First string + Last string +} + +var tpl *template.Template + +func init() { + tpl = template.Must(template.ParseGlob("templates/*.gohtml")) +} + +func main() { + http.HandleFunc("/", index) + http.HandleFunc("/send", send) + http.HandleFunc("/catch", catch) + http.ListenAndServe(":8080", nil) +} + +func index(w http.ResponseWriter, r *http.Request) { + p1 := person{ + First: "James", + Last: "Bond", + } + + p2 := person{ + First: "Miss", + Last: "Moneypenny", + } + + xp := []person{p1, p2} + + err := json.NewEncoder(w).Encode(xp) + if err != nil { + fmt.Println(err) + } +} + +func send(w http.ResponseWriter, r *http.Request) { + tpl.ExecuteTemplate(w, "send.gohtml", nil) +} + +var xp []person + +func catch(w http.ResponseWriter, r *http.Request) { + + if r.Method == http.MethodPost { + err := json.NewDecoder(r.Body).Decode(&xp) + if err != nil { + fmt.Println(err) + } + fmt.Println(xp) + } + + if r.Method == http.MethodGet { + tpl.ExecuteTemplate(w, "catch.gohtml", xp) + } +} diff --git a/000_temp/46_sp17/48_json/05_decode/templates/catch.gohtml b/000_temp/46_sp17/48_json/05_decode/templates/catch.gohtml new file mode 100644 index 00000000..74f50269 --- /dev/null +++ b/000_temp/46_sp17/48_json/05_decode/templates/catch.gohtml @@ -0,0 +1,20 @@ + + + + + + + Document + + + +{{range .}} +Here is the data: {{.}}
+FIRST: {{.First}}
+Last: {{.Last}} +

+{{end}} + + + \ No newline at end of file diff --git a/000_temp/46_sp17/48_json/05_decode/templates/send.gohtml b/000_temp/46_sp17/48_json/05_decode/templates/send.gohtml new file mode 100644 index 00000000..610483b0 --- /dev/null +++ b/000_temp/46_sp17/48_json/05_decode/templates/send.gohtml @@ -0,0 +1,49 @@ + + + + + + + Document + + + + +

We are now going to send data to the /catch path

+ + + + + \ No newline at end of file diff --git a/000_temp/46_sp17/48_json/06_cookie/main.go b/000_temp/46_sp17/48_json/06_cookie/main.go new file mode 100644 index 00000000..21a39e25 --- /dev/null +++ b/000_temp/46_sp17/48_json/06_cookie/main.go @@ -0,0 +1,94 @@ +package main + +import ( + "encoding/base64" + "encoding/json" + "fmt" + "html/template" + "io/ioutil" + "net/http" +) + +type person struct { + First string + Last string +} + +var tpl *template.Template + +func init() { + tpl = template.Must(template.ParseGlob("templates/*.gohtml")) +} + +func main() { + http.HandleFunc("/", index) + http.HandleFunc("/send", send) + http.HandleFunc("/catch", catch) + http.ListenAndServe(":8080", nil) +} + +func index(w http.ResponseWriter, r *http.Request) { + xp := getUsers(r) + + tpl.ExecuteTemplate(w, "index.gohtml", xp) +} + +func send(w http.ResponseWriter, r *http.Request) { + xp := getUsers(r) + + tpl.ExecuteTemplate(w, "send.gohtml", xp) +} + +func catch(w http.ResponseWriter, r *http.Request) { + xp := getUsers(r) + + if r.Method == http.MethodPost { + bs, err := ioutil.ReadAll(r.Body) + if err != nil { + fmt.Println(err) + } + + err = json.Unmarshal(bs, &xp) + if err != nil { + fmt.Println(err) + return + } + fmt.Println(xp) + + s := base64.StdEncoding.EncodeToString(bs) + fmt.Println(s) + + c := &http.Cookie{ + Name: "userdata", + Value: s, + Path: "/", + HttpOnly: true, + } + http.SetCookie(w, c) + } + + if r.Method == http.MethodGet { + tpl.ExecuteTemplate(w, "catch.gohtml", xp) + } +} + +func getUsers(r *http.Request) []person { + var xp []person + + c, err := r.Cookie("userdata") + if err == nil { + bs, err := base64.StdEncoding.DecodeString(c.Value) + if err != nil { + fmt.Println(err) + return xp + } + + fmt.Println("just after base64 decode:", string(bs)) + + err = json.Unmarshal(bs, &xp) + if err != nil { + fmt.Println("unmarshalling in decode from b64", err) + } + } + return xp +} diff --git a/000_temp/46_sp17/48_json/06_cookie/templates/catch.gohtml b/000_temp/46_sp17/48_json/06_cookie/templates/catch.gohtml new file mode 100644 index 00000000..bb497a18 --- /dev/null +++ b/000_temp/46_sp17/48_json/06_cookie/templates/catch.gohtml @@ -0,0 +1,20 @@ + + + + + + + CATCH + + + +{{range .}} +Here is the data: {{.}}
+FIRST: {{.First}}
+Last: {{.Last}} +

+{{end}} + + + \ No newline at end of file diff --git a/000_temp/46_sp17/48_json/06_cookie/templates/index.gohtml b/000_temp/46_sp17/48_json/06_cookie/templates/index.gohtml new file mode 100644 index 00000000..9ec8fd9c --- /dev/null +++ b/000_temp/46_sp17/48_json/06_cookie/templates/index.gohtml @@ -0,0 +1,20 @@ + + + + + + + INDEX + + + +{{range .}} +Here is the data: {{.}}
+FIRST: {{.First}}
+Last: {{.Last}} +

+{{end}} + + + \ No newline at end of file diff --git a/000_temp/46_sp17/48_json/06_cookie/templates/send.gohtml b/000_temp/46_sp17/48_json/06_cookie/templates/send.gohtml new file mode 100644 index 00000000..302b4b0e --- /dev/null +++ b/000_temp/46_sp17/48_json/06_cookie/templates/send.gohtml @@ -0,0 +1,56 @@ + + + + + + + SEND + + + + +

We are now going to send data to the /catch path

+

{{range .}} + Here is the data: {{.}}
+ FIRST: {{.First}}
+ Last: {{.Last}} +

+ {{end}} +

+ + + + + \ No newline at end of file diff --git a/000_temp/46_sp17/49_multiple-redirects/main.go b/000_temp/46_sp17/49_multiple-redirects/main.go new file mode 100644 index 00000000..97a5d8e2 --- /dev/null +++ b/000_temp/46_sp17/49_multiple-redirects/main.go @@ -0,0 +1,33 @@ +package main + +import ( + "html/template" + "net/http" +) + +var tpl *template.Template + +func init() { + tpl = template.Must(template.ParseGlob("templates/*.gohtml")) +} + +func main() { + http.HandleFunc("/", index) + http.HandleFunc("/foo", foo) + http.HandleFunc("/bar", bar) + http.ListenAndServe(":8080", nil) +} + +func index(w http.ResponseWriter, r *http.Request) { + tpl.ExecuteTemplate(w, "index.gohtml", nil) +} + +func foo(w http.ResponseWriter, r *http.Request) { + http.Redirect(w, r, "/", http.StatusSeeOther) + return // comment out and experiment to see what happens + http.Redirect(w, r, "/bar", http.StatusSeeOther) +} + +func bar(w http.ResponseWriter, r *http.Request) { + tpl.ExecuteTemplate(w, "bar.gohtml", nil) +} diff --git a/000_temp/46_sp17/49_multiple-redirects/templates/bar.gohtml b/000_temp/46_sp17/49_multiple-redirects/templates/bar.gohtml new file mode 100644 index 00000000..0522363c --- /dev/null +++ b/000_temp/46_sp17/49_multiple-redirects/templates/bar.gohtml @@ -0,0 +1,25 @@ + + + + + + + BAR + + + + +

You are at BAR

+ + + \ No newline at end of file diff --git a/000_temp/46_sp17/49_multiple-redirects/templates/index.gohtml b/000_temp/46_sp17/49_multiple-redirects/templates/index.gohtml new file mode 100644 index 00000000..27eea8db --- /dev/null +++ b/000_temp/46_sp17/49_multiple-redirects/templates/index.gohtml @@ -0,0 +1,26 @@ + + + + + + + index + + + + +

You are at INDEX

+ +

GO TO FOO

+ + \ No newline at end of file diff --git a/000_temp/46_sp17/50_mongo/main.go b/000_temp/46_sp17/50_mongo/main.go new file mode 100644 index 00000000..0e07afe5 --- /dev/null +++ b/000_temp/46_sp17/50_mongo/main.go @@ -0,0 +1,51 @@ +package main + +import ( + "fmt" + "gopkg.in/mgo.v2" + "gopkg.in/mgo.v2/bson" + "html/template" + "net/http" +) + +type customer struct { + Name string `json:"name" bson:"name"` +} + +var tpl *template.Template +var DB *mgo.Database +var Customer *mgo.Collection + +func init() { + tpl = template.Must(template.ParseGlob("templates/*.gohtml")) + + // get a mongo sessions + //s, err := mgo.Dial("mongodb://bond:moneypenny007@localhost/bookstore") + s, err := mgo.Dial("mongodb://localhost/bookstore") + if err != nil { + panic(err) + } + + if err = s.Ping(); err != nil { + panic(err) + } + + DB = s.DB("bookstore") + Customer = DB.C("customer") + + fmt.Println("You connected to your mongo database.") +} + +func main() { + http.HandleFunc("/", index) + http.ListenAndServe(":8080", nil) +} + +func index(w http.ResponseWriter, r *http.Request) { + customers := []customer{} + err := Customer.Find(bson.M{}).All(&customers) + if err != nil { + fmt.Println(err) + } + tpl.ExecuteTemplate(w, "index.gohtml", customers) +} diff --git a/000_temp/46_sp17/50_mongo/templates/index.gohtml b/000_temp/46_sp17/50_mongo/templates/index.gohtml new file mode 100644 index 00000000..7b16697b --- /dev/null +++ b/000_temp/46_sp17/50_mongo/templates/index.gohtml @@ -0,0 +1,15 @@ + + + + + + + INDEX + + + +

{{.}}

+ + + \ No newline at end of file diff --git a/000_temp/46_sp17/51_question/01/main.go b/000_temp/46_sp17/51_question/01/main.go new file mode 100644 index 00000000..15fa7006 --- /dev/null +++ b/000_temp/46_sp17/51_question/01/main.go @@ -0,0 +1,52 @@ +package main + +import ( + "fmt" + "math/rand" + "sync" + "time" +) + +var wg sync.WaitGroup + +type SafeCounter struct { + c int + sync.Mutex +} + +func (c *SafeCounter) Add() { + c.Lock() + defer c.Unlock() + c.c++ +} + +var counter *SafeCounter = &SafeCounter{} + +func main() { + + fmt.Println(&counter.c) + + wg.Add(2) + go incrementor("Foo:") + go incrementor("Bar:") + wg.Wait() + fmt.Println("Final Counter:", counter.c) +} + +func incrementor(s string) { + rand.Seed(time.Now().UnixNano()) + for i := 0; i < 20; i++ { + x := counter + fmt.Println("1----", counter) + fmt.Println("2----", x) + x.Add() + counter = x + time.Sleep(time.Duration(rand.Intn(3)) * time.Millisecond) + fmt.Println(s, i, "Counter:", counter.c) + } + wg.Done() +} + +// go run -race main.go +// vs +// go run main.go diff --git a/000_temp/46_sp17/51_question/02/main.go b/000_temp/46_sp17/51_question/02/main.go new file mode 100644 index 00000000..dd1fee0d --- /dev/null +++ b/000_temp/46_sp17/51_question/02/main.go @@ -0,0 +1,48 @@ +package main + +import ( + "fmt" + "math/rand" + "sync" + "time" +) + +var wg sync.WaitGroup + +type SafeCounter struct { + c int + m sync.Mutex +} + +func (sc *SafeCounter) Add() { + sc.m.Lock() + defer sc.m.Unlock() + sc.c++ +} + +var counter *SafeCounter = &SafeCounter{} + +func main() { + + fmt.Println(&counter.c) + + wg.Add(2) + go incrementor("Foo:") + go incrementor("Bar:") + wg.Wait() + fmt.Println("Final Counter:", counter.c) +} + +func incrementor(s string) { + rand.Seed(time.Now().UnixNano()) + for i := 0; i < 20; i++ { + counter.Add() + time.Sleep(time.Duration(rand.Intn(3)) * time.Millisecond) + //fmt.Println(s, i, "Counter:", counter.c) // causes a race; no lock here; multiple goroutines accessing to READ the value + } + wg.Done() +} + +// go run -race main.go +// vs +// go run main.go diff --git a/000_temp/46_sp17/52-review/01/main.go b/000_temp/46_sp17/52-review/01/main.go new file mode 100644 index 00000000..1c0890fd --- /dev/null +++ b/000_temp/46_sp17/52-review/01/main.go @@ -0,0 +1,15 @@ +package main + +import ( + "io" + "net/http" +) + +func main() { + http.HandleFunc("/", index) + http.ListenAndServe(":8080", nil) +} + +func index(w http.ResponseWriter, r *http.Request) { + io.WriteString(w, "some text hello world") +} diff --git a/000_temp/46_sp17/52-review/02/main.go b/000_temp/46_sp17/52-review/02/main.go new file mode 100644 index 00000000..a619af57 --- /dev/null +++ b/000_temp/46_sp17/52-review/02/main.go @@ -0,0 +1,154 @@ +package main + +import ( + "io" + "net/http" +) + +func main() { + http.HandleFunc("/", index) + http.ListenAndServe(":8080", nil) +} + +func index(w http.ResponseWriter, r *http.Request) { + io.WriteString(w, ` + + + + + Title + + + + +
+

hello world

+
+ + + + + `) +} diff --git a/000_temp/46_sp17/52-review/03/main.go b/000_temp/46_sp17/52-review/03/main.go new file mode 100644 index 00000000..7b469e6f --- /dev/null +++ b/000_temp/46_sp17/52-review/03/main.go @@ -0,0 +1,16 @@ +package main + +import ( + "io" + "net/http" +) + +func main() { + http.HandleFunc("/", index) + http.ListenAndServe(":8080", nil) +} + +func index(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/plain; charset=utf-8") + io.WriteString(w, `

Hello World

`) +} diff --git a/000_temp/46_sp17/52-review/04/main.go b/000_temp/46_sp17/52-review/04/main.go new file mode 100644 index 00000000..21ff5e92 --- /dev/null +++ b/000_temp/46_sp17/52-review/04/main.go @@ -0,0 +1,21 @@ +package main + +import ( + "html/template" + "net/http" +) + +var tpl *template.Template + +func init() { + tpl = template.Must(template.ParseGlob("templates/*.gohtml")) +} + +func main() { + http.HandleFunc("/", index) + http.ListenAndServe(":8080", nil) +} + +func index(w http.ResponseWriter, r *http.Request) { + tpl.ExecuteTemplate(w, "index.gohtml", nil) +} diff --git a/000_temp/46_sp17/52-review/04/templates/index.gohtml b/000_temp/46_sp17/52-review/04/templates/index.gohtml new file mode 100644 index 00000000..f0bd030e --- /dev/null +++ b/000_temp/46_sp17/52-review/04/templates/index.gohtml @@ -0,0 +1,138 @@ + + + + + Title + + + + +
+

hello world

+
+ + + + \ No newline at end of file diff --git a/000_temp/46_sp17/52-review/05/main.go b/000_temp/46_sp17/52-review/05/main.go new file mode 100644 index 00000000..f91303d3 --- /dev/null +++ b/000_temp/46_sp17/52-review/05/main.go @@ -0,0 +1,22 @@ +package main + +import ( + "html/template" + "net/http" +) + +var tpl *template.Template + +func init() { + tpl = template.Must(template.ParseGlob("templates/*.gohtml")) +} + +func main() { + http.HandleFunc("/", index) + http.ListenAndServe(":8080", nil) +} + +func index(w http.ResponseWriter, r *http.Request) { + n := "James Bond" + tpl.ExecuteTemplate(w, "index.gohtml", n) +} diff --git a/000_temp/46_sp17/52-review/05/templates/index.gohtml b/000_temp/46_sp17/52-review/05/templates/index.gohtml new file mode 100644 index 00000000..229adf46 --- /dev/null +++ b/000_temp/46_sp17/52-review/05/templates/index.gohtml @@ -0,0 +1,138 @@ + + + + + Title + + + + +
+

{{.}}

+
+ + + + \ No newline at end of file diff --git a/000_temp/46_sp17/52-review/06/main.go b/000_temp/46_sp17/52-review/06/main.go new file mode 100644 index 00000000..cbabd247 --- /dev/null +++ b/000_temp/46_sp17/52-review/06/main.go @@ -0,0 +1,40 @@ +package main + +import ( + "html/template" + "net/http" +) + +type person struct { + First string + Last string + Saying string +} + +var tpl *template.Template + +func init() { + tpl = template.Must(template.ParseGlob("templates/*.gohtml")) +} + +func main() { + http.HandleFunc("/", index) + http.ListenAndServe(":8080", nil) +} + +func index(w http.ResponseWriter, r *http.Request) { + p1 := person{ + First: "James", + Last: "Bond", + Saying: "Shaken, not stirred.", + } + + p2 := person{ + First: "Miss", + Last: "Moneypenny", + Saying: "Hello, James. It is sooooooooo good to see you.", + } + + xp := []person{p1, p2} + tpl.ExecuteTemplate(w, "index.gohtml", xp) +} diff --git a/000_temp/46_sp17/52-review/06/templates/index.gohtml b/000_temp/46_sp17/52-review/06/templates/index.gohtml new file mode 100644 index 00000000..0c3f8f59 --- /dev/null +++ b/000_temp/46_sp17/52-review/06/templates/index.gohtml @@ -0,0 +1,138 @@ + + + + + Title + + + + +
+ {{range .}} +

{{.First}} {{.Last}}

+

{{.Saying}}

+ {{end}} +
+ + + + \ No newline at end of file diff --git a/000_temp/46_sp17/52-review/07/main.go b/000_temp/46_sp17/52-review/07/main.go new file mode 100644 index 00000000..328f21e3 --- /dev/null +++ b/000_temp/46_sp17/52-review/07/main.go @@ -0,0 +1,41 @@ +package main + +import ( + "html/template" + "net/http" +) + +type person struct { + First string + Last string + Saying string +} + +var tpl *template.Template + +func init() { + tpl = template.Must(template.ParseGlob("templates/*.gohtml")) +} + +func main() { + http.HandleFunc("/", index) + http.Handle("/assets/", http.StripPrefix("/assets", http.FileServer(http.Dir("public")))) + http.ListenAndServe(":8080", nil) +} + +func index(w http.ResponseWriter, r *http.Request) { + p1 := person{ + First: "James", + Last: "Bond", + Saying: "Shaken, not stirred.", + } + + p2 := person{ + First: "Miss", + Last: "Moneypenny", + Saying: "Hello, James. It is sooooooooo good to see you.", + } + + xp := []person{p1, p2} + tpl.ExecuteTemplate(w, "index.gohtml", xp) +} diff --git a/000_temp/46_sp17/52-review/07/public/css/index.css b/000_temp/46_sp17/52-review/07/public/css/index.css new file mode 100644 index 00000000..1dd9f35f --- /dev/null +++ b/000_temp/46_sp17/52-review/07/public/css/index.css @@ -0,0 +1,16 @@ +html, body, h1 { + padding: 0; + border: 0; + margin: 0; + box-sizing: border-box; +} + +main { + display: flex; + flex-direction: column; + flex-wrap: nowrap; + justify-content: center; + align-items: center; + background-image: linear-gradient(red, yellow, blue); + height: 100vh; +} \ No newline at end of file diff --git a/000_temp/46_sp17/52-review/07/templates/index.gohtml b/000_temp/46_sp17/52-review/07/templates/index.gohtml new file mode 100644 index 00000000..0788480b --- /dev/null +++ b/000_temp/46_sp17/52-review/07/templates/index.gohtml @@ -0,0 +1,120 @@ + + + + + Title + + + + +
+ {{range .}} +

{{.First}} {{.Last}}

+

{{.Saying}}

+ {{end}} +
+ + + + \ No newline at end of file diff --git a/000_temp/46_sp17/52-review/08/main.go b/000_temp/46_sp17/52-review/08/main.go new file mode 100644 index 00000000..1c692706 --- /dev/null +++ b/000_temp/46_sp17/52-review/08/main.go @@ -0,0 +1,23 @@ +package main + +import ( + "html/template" + "net/http" +) + +var tpl *template.Template + +func init() { + tpl = template.Must(template.ParseGlob("templates/*.gohtml")) +} + +func main() { + http.HandleFunc("/", index) + http.Handle("/assets/", http.StripPrefix("/assets", http.FileServer(http.Dir("public")))) + http.ListenAndServe(":8080", nil) +} + +func index(w http.ResponseWriter, r *http.Request) { + s := "Paradise Found" + tpl.ExecuteTemplate(w, "index.gohtml", s) +} diff --git a/000_temp/46_sp17/52-review/08/public/css/index.css b/000_temp/46_sp17/52-review/08/public/css/index.css new file mode 100644 index 00000000..e51c29b4 --- /dev/null +++ b/000_temp/46_sp17/52-review/08/public/css/index.css @@ -0,0 +1,24 @@ +html, body, h1 { + padding: 0; + border: 0; + margin: 0; + box-sizing: border-box; +} + +main { + display: flex; + flex-direction: column; + flex-wrap: nowrap; + justify-content: center; + align-items: center; + background-image: url("http://github.com/assets/img/surf.png"); + background-size: cover; + background-repeat: no-repeat; + background-position: center; + height: 100vh; +} + +h1 { + font-size: 8rem; + color: white; +} \ No newline at end of file diff --git a/000_temp/46_sp17/52-review/08/public/img/surf.png b/000_temp/46_sp17/52-review/08/public/img/surf.png new file mode 100644 index 00000000..1636912e Binary files /dev/null and b/000_temp/46_sp17/52-review/08/public/img/surf.png differ diff --git a/000_temp/46_sp17/52-review/08/templates/index.gohtml b/000_temp/46_sp17/52-review/08/templates/index.gohtml new file mode 100644 index 00000000..386fb0fc --- /dev/null +++ b/000_temp/46_sp17/52-review/08/templates/index.gohtml @@ -0,0 +1,117 @@ + + + + + Title + + + + +
+

{{.}}

+
+ + + + \ No newline at end of file diff --git a/000_temp/46_sp17/52-review/09/app.yaml b/000_temp/46_sp17/52-review/09/app.yaml new file mode 100644 index 00000000..6816437c --- /dev/null +++ b/000_temp/46_sp17/52-review/09/app.yaml @@ -0,0 +1,2 @@ +runtime: go +env: flex \ No newline at end of file diff --git a/000_temp/46_sp17/52-review/09/main.go b/000_temp/46_sp17/52-review/09/main.go new file mode 100644 index 00000000..1c692706 --- /dev/null +++ b/000_temp/46_sp17/52-review/09/main.go @@ -0,0 +1,23 @@ +package main + +import ( + "html/template" + "net/http" +) + +var tpl *template.Template + +func init() { + tpl = template.Must(template.ParseGlob("templates/*.gohtml")) +} + +func main() { + http.HandleFunc("/", index) + http.Handle("/assets/", http.StripPrefix("/assets", http.FileServer(http.Dir("public")))) + http.ListenAndServe(":8080", nil) +} + +func index(w http.ResponseWriter, r *http.Request) { + s := "Paradise Found" + tpl.ExecuteTemplate(w, "index.gohtml", s) +} diff --git a/000_temp/46_sp17/52-review/09/public/css/index.css b/000_temp/46_sp17/52-review/09/public/css/index.css new file mode 100644 index 00000000..e51c29b4 --- /dev/null +++ b/000_temp/46_sp17/52-review/09/public/css/index.css @@ -0,0 +1,24 @@ +html, body, h1 { + padding: 0; + border: 0; + margin: 0; + box-sizing: border-box; +} + +main { + display: flex; + flex-direction: column; + flex-wrap: nowrap; + justify-content: center; + align-items: center; + background-image: url("http://github.com/assets/img/surf.png"); + background-size: cover; + background-repeat: no-repeat; + background-position: center; + height: 100vh; +} + +h1 { + font-size: 8rem; + color: white; +} \ No newline at end of file diff --git a/000_temp/46_sp17/52-review/09/public/img/surf.png b/000_temp/46_sp17/52-review/09/public/img/surf.png new file mode 100644 index 00000000..1636912e Binary files /dev/null and b/000_temp/46_sp17/52-review/09/public/img/surf.png differ diff --git a/000_temp/46_sp17/52-review/09/templates/index.gohtml b/000_temp/46_sp17/52-review/09/templates/index.gohtml new file mode 100644 index 00000000..386fb0fc --- /dev/null +++ b/000_temp/46_sp17/52-review/09/templates/index.gohtml @@ -0,0 +1,117 @@ + + + + + Title + + + + +
+

{{.}}

+
+ + + + \ No newline at end of file diff --git a/000_temp/47_ajax/01/main.go b/000_temp/47_ajax/01/main.go index fc3e6bf7..3c40f998 100644 --- a/000_temp/47_ajax/01/main.go +++ b/000_temp/47_ajax/01/main.go @@ -1,9 +1,9 @@ package main import ( - "net/http" - "html/template" "fmt" + "html/template" + "net/http" ) var tpl *template.Template @@ -23,6 +23,7 @@ func index(w http.ResponseWriter, r *http.Request) { } func process(w http.ResponseWriter, r *http.Request) { + subscribe := r.FormValue("subscribe") dessert := r.FormValue("dessert") dow := r.FormValue("dow") diff --git a/000_temp/48_gmail/main.go b/000_temp/48_gmail/main.go new file mode 100644 index 00000000..79058077 --- /dev/null +++ b/000_temp/48_gmail/main.go @@ -0,0 +1,5 @@ +package main + +func main() { + +} diff --git a/000_temp/49_interfaces/main.go b/000_temp/49_interfaces/main.go new file mode 100644 index 00000000..4fd7d004 --- /dev/null +++ b/000_temp/49_interfaces/main.go @@ -0,0 +1,44 @@ +package main + +import ( + "fmt" + "github.com/GoesToEleven/golang-web-dev/000_temp/49_interfaces/species" + _ "github.com/GoesToEleven/golang-web-dev/000_temp/49_interfaces/species" +) + +type person struct { + first string +} + +func (person) Speak() { + fmt.Println("I'm a person") +} + +type secretAgent struct { + person + ltk bool +} + +func (secretAgent) Speak() { + fmt.Println("I'm a person and a secret agent") +} + +func foo(h species.Human) { + h.Speak() +} + +func main() { + p1 := person{ + first: "Miss Moneypenny", + } + + p2 := secretAgent{ + person{ + "James", + }, + true, + } + + foo(p1) + foo(p2) +} diff --git a/000_temp/49_interfaces/species/species.go b/000_temp/49_interfaces/species/species.go new file mode 100644 index 00000000..c54fe271 --- /dev/null +++ b/000_temp/49_interfaces/species/species.go @@ -0,0 +1,5 @@ +package species + +type Human interface { + Speak() +} diff --git a/000_temp/50_disney/01/main.go b/000_temp/50_disney/01/main.go new file mode 100644 index 00000000..f1f3f7e2 --- /dev/null +++ b/000_temp/50_disney/01/main.go @@ -0,0 +1,15 @@ +package main + +import ( + "io" + "net/http" +) + +func main() { + http.HandleFunc("/", foo) + http.ListenAndServe(":8080", nil) +} + +func foo(w http.ResponseWriter, r *http.Request) { + io.WriteString(w, "Hellooooo!") +} diff --git a/000_temp/50_disney/02/main.go b/000_temp/50_disney/02/main.go new file mode 100644 index 00000000..d3b764c7 --- /dev/null +++ b/000_temp/50_disney/02/main.go @@ -0,0 +1,54 @@ +package main + +import ( + "encoding/json" + "fmt" + "html/template" + "net/http" +) + +var tpl *template.Template + +func init() { + tpl = template.Must(template.ParseGlob("templates/*.gohtml")) +} + +func main() { + http.HandleFunc("/", index) + http.HandleFunc("/about", about) + http.HandleFunc("/js", js) + http.ListenAndServe(":8080", nil) +} + +func index(w http.ResponseWriter, r *http.Request) { + tpl.ExecuteTemplate(w, "index.gohtml", nil) +} + +func about(w http.ResponseWriter, r *http.Request) { + tpl.ExecuteTemplate(w, "about.gohtml", nil) +} + +func js(w http.ResponseWriter, r *http.Request) { + nj := struct { + First string `json:"first-name"` + Last string `json:"last-name"` + }{ + "James", + "Bond", + } + + w.Header().Set("Content-Type", "application/json") + + err := json.NewEncoder(w).Encode(nj) + if err != nil { + fmt.Println(err) + return + } + //bs, err := json.Marshal(nj) + //if err != nil { + // fmt.Println(err) + // return + //} + //s := string(bs) + //tpl.ExecuteTemplate(w, "js.gohtml", s) +} diff --git a/000_temp/50_disney/02/templates/about.gohtml b/000_temp/50_disney/02/templates/about.gohtml new file mode 100644 index 00000000..e7bc7333 --- /dev/null +++ b/000_temp/50_disney/02/templates/about.gohtml @@ -0,0 +1,14 @@ + + + + + + About + + + +

All about Disney!

+ + + \ No newline at end of file diff --git a/000_temp/50_disney/02/templates/index.gohtml b/000_temp/50_disney/02/templates/index.gohtml new file mode 100644 index 00000000..edf0ff57 --- /dev/null +++ b/000_temp/50_disney/02/templates/index.gohtml @@ -0,0 +1,14 @@ + + + + + + Index + + + +

Hello Disney!

+ + + \ No newline at end of file diff --git a/000_temp/50_disney/02/templates/js.gohtml b/000_temp/50_disney/02/templates/js.gohtml new file mode 100644 index 00000000..41087b11 --- /dev/null +++ b/000_temp/50_disney/02/templates/js.gohtml @@ -0,0 +1,14 @@ + + + + + + About + + + +

{{.}}

+ + + \ No newline at end of file diff --git a/000_temp/50_disney/03/main.go b/000_temp/50_disney/03/main.go new file mode 100644 index 00000000..48f9645f --- /dev/null +++ b/000_temp/50_disney/03/main.go @@ -0,0 +1,13 @@ +package main + +import "fmt" + +var x int = 42 + +func init() { + fmt.Println("in init ", x) +} + +func main() { + fmt.Println("in main ", x) +} diff --git a/000_temp/50_disney/04/main.go b/000_temp/50_disney/04/main.go new file mode 100644 index 00000000..131d5fa6 --- /dev/null +++ b/000_temp/50_disney/04/main.go @@ -0,0 +1,31 @@ +package main + +import ( + "html/template" + "net/http" +) + +var tpl *template.Template + +func init() { + tpl = template.Must(template.ParseGlob("templates/*.gohtml")) +} + +func main() { + http.HandleFunc("/", index) + http.HandleFunc("/about", about) + http.HandleFunc("/dog", dog) + http.ListenAndServe(":8080", nil) +} + +func index(w http.ResponseWriter, r *http.Request) { + tpl.ExecuteTemplate(w, "index.gohtml", nil) +} + +func about(w http.ResponseWriter, r *http.Request) { + tpl.ExecuteTemplate(w, "about.gohtml", nil) +} + +func dog(w http.ResponseWriter, r *http.Request) { + http.ServeFile(w, r, "public/img/toby.jpg") +} diff --git a/000_temp/50_disney/04/public/img/toby.jpg b/000_temp/50_disney/04/public/img/toby.jpg new file mode 100644 index 00000000..16fe5330 Binary files /dev/null and b/000_temp/50_disney/04/public/img/toby.jpg differ diff --git a/000_temp/50_disney/04/templates/about.gohtml b/000_temp/50_disney/04/templates/about.gohtml new file mode 100644 index 00000000..e7bc7333 --- /dev/null +++ b/000_temp/50_disney/04/templates/about.gohtml @@ -0,0 +1,14 @@ + + + + + + About + + + +

All about Disney!

+ + + \ No newline at end of file diff --git a/000_temp/50_disney/04/templates/index.gohtml b/000_temp/50_disney/04/templates/index.gohtml new file mode 100644 index 00000000..eb1df9ba --- /dev/null +++ b/000_temp/50_disney/04/templates/index.gohtml @@ -0,0 +1,15 @@ + + + + + + Index + + + +

Hello Disney!

+ + + + \ No newline at end of file diff --git a/000_temp/50_disney/04/templates/js.gohtml b/000_temp/50_disney/04/templates/js.gohtml new file mode 100644 index 00000000..41087b11 --- /dev/null +++ b/000_temp/50_disney/04/templates/js.gohtml @@ -0,0 +1,14 @@ + + + + + + About + + + +

{{.}}

+ + + \ No newline at end of file diff --git a/000_temp/50_disney/05_to-illustrate-not-working/main.go b/000_temp/50_disney/05_to-illustrate-not-working/main.go new file mode 100644 index 00000000..749ae79d --- /dev/null +++ b/000_temp/50_disney/05_to-illustrate-not-working/main.go @@ -0,0 +1,32 @@ +package main + +import ( + "html/template" + "net/http" +) + +var tpl *template.Template + +func init() { + tpl = template.Must(template.ParseGlob("templates/*.gohtml")) +} + +func main() { + http.HandleFunc("/", index) + http.HandleFunc("/about", about) + http.HandleFunc("/dog", dog) + http.Handle("/public/", http.FileServer(http.Dir("./public"))) + http.ListenAndServe(":8080", nil) +} + +func index(w http.ResponseWriter, r *http.Request) { + tpl.ExecuteTemplate(w, "index.gohtml", nil) +} + +func about(w http.ResponseWriter, r *http.Request) { + tpl.ExecuteTemplate(w, "about.gohtml", nil) +} + +func dog(w http.ResponseWriter, r *http.Request) { + http.ServeFile(w, r, "public/img/toby.jpg") +} diff --git a/000_temp/50_disney/05_to-illustrate-not-working/public/img/toby.jpg b/000_temp/50_disney/05_to-illustrate-not-working/public/img/toby.jpg new file mode 100644 index 00000000..16fe5330 Binary files /dev/null and b/000_temp/50_disney/05_to-illustrate-not-working/public/img/toby.jpg differ diff --git a/000_temp/50_disney/05_to-illustrate-not-working/templates/about.gohtml b/000_temp/50_disney/05_to-illustrate-not-working/templates/about.gohtml new file mode 100644 index 00000000..e7bc7333 --- /dev/null +++ b/000_temp/50_disney/05_to-illustrate-not-working/templates/about.gohtml @@ -0,0 +1,14 @@ + + + + + + About + + + +

All about Disney!

+ + + \ No newline at end of file diff --git a/000_temp/50_disney/05_to-illustrate-not-working/templates/index.gohtml b/000_temp/50_disney/05_to-illustrate-not-working/templates/index.gohtml new file mode 100644 index 00000000..3a62d64b --- /dev/null +++ b/000_temp/50_disney/05_to-illustrate-not-working/templates/index.gohtml @@ -0,0 +1,15 @@ + + + + + + Index + + + +

Hello Disney!

+ + + + \ No newline at end of file diff --git a/000_temp/50_disney/05_to-illustrate-not-working/templates/js.gohtml b/000_temp/50_disney/05_to-illustrate-not-working/templates/js.gohtml new file mode 100644 index 00000000..41087b11 --- /dev/null +++ b/000_temp/50_disney/05_to-illustrate-not-working/templates/js.gohtml @@ -0,0 +1,14 @@ + + + + + + About + + + +

{{.}}

+ + + \ No newline at end of file diff --git a/000_temp/50_disney/06/main.go b/000_temp/50_disney/06/main.go new file mode 100644 index 00000000..8361c048 --- /dev/null +++ b/000_temp/50_disney/06/main.go @@ -0,0 +1,32 @@ +package main + +import ( + "html/template" + "net/http" +) + +var tpl *template.Template + +func init() { + tpl = template.Must(template.ParseGlob("templates/*.gohtml")) +} + +func main() { + http.HandleFunc("/", index) + http.HandleFunc("/about", about) + http.HandleFunc("/dog", dog) + http.Handle("/assets/", http.StripPrefix("/assets", http.FileServer(http.Dir("./public")))) + http.ListenAndServe(":8080", nil) +} + +func index(w http.ResponseWriter, r *http.Request) { + tpl.ExecuteTemplate(w, "index.gohtml", nil) +} + +func about(w http.ResponseWriter, r *http.Request) { + tpl.ExecuteTemplate(w, "about.gohtml", nil) +} + +func dog(w http.ResponseWriter, r *http.Request) { + http.ServeFile(w, r, "public/img/toby.jpg") +} diff --git a/000_temp/50_disney/06/public/img/toby.jpg b/000_temp/50_disney/06/public/img/toby.jpg new file mode 100644 index 00000000..16fe5330 Binary files /dev/null and b/000_temp/50_disney/06/public/img/toby.jpg differ diff --git a/000_temp/50_disney/06/templates/about.gohtml b/000_temp/50_disney/06/templates/about.gohtml new file mode 100644 index 00000000..e7bc7333 --- /dev/null +++ b/000_temp/50_disney/06/templates/about.gohtml @@ -0,0 +1,14 @@ + + + + + + About + + + +

All about Disney!

+ + + \ No newline at end of file diff --git a/000_temp/50_disney/06/templates/index.gohtml b/000_temp/50_disney/06/templates/index.gohtml new file mode 100644 index 00000000..7b9a9951 --- /dev/null +++ b/000_temp/50_disney/06/templates/index.gohtml @@ -0,0 +1,15 @@ + + + + + + Index + + + +

Hello Disney!

+ + + + \ No newline at end of file diff --git a/000_temp/50_disney/06/templates/js.gohtml b/000_temp/50_disney/06/templates/js.gohtml new file mode 100644 index 00000000..41087b11 --- /dev/null +++ b/000_temp/50_disney/06/templates/js.gohtml @@ -0,0 +1,14 @@ + + + + + + About + + + +

{{.}}

+ + + \ No newline at end of file diff --git a/000_temp/50_disney/07/main.go b/000_temp/50_disney/07/main.go new file mode 100644 index 00000000..8361c048 --- /dev/null +++ b/000_temp/50_disney/07/main.go @@ -0,0 +1,32 @@ +package main + +import ( + "html/template" + "net/http" +) + +var tpl *template.Template + +func init() { + tpl = template.Must(template.ParseGlob("templates/*.gohtml")) +} + +func main() { + http.HandleFunc("/", index) + http.HandleFunc("/about", about) + http.HandleFunc("/dog", dog) + http.Handle("/assets/", http.StripPrefix("/assets", http.FileServer(http.Dir("./public")))) + http.ListenAndServe(":8080", nil) +} + +func index(w http.ResponseWriter, r *http.Request) { + tpl.ExecuteTemplate(w, "index.gohtml", nil) +} + +func about(w http.ResponseWriter, r *http.Request) { + tpl.ExecuteTemplate(w, "about.gohtml", nil) +} + +func dog(w http.ResponseWriter, r *http.Request) { + http.ServeFile(w, r, "public/img/toby.jpg") +} diff --git a/000_temp/50_disney/07/public/css/index.css b/000_temp/50_disney/07/public/css/index.css new file mode 100644 index 00000000..f12e4201 --- /dev/null +++ b/000_temp/50_disney/07/public/css/index.css @@ -0,0 +1,4 @@ + +h1 { + color: red; +} \ No newline at end of file diff --git a/000_temp/50_disney/07/public/img/toby.jpg b/000_temp/50_disney/07/public/img/toby.jpg new file mode 100644 index 00000000..16fe5330 Binary files /dev/null and b/000_temp/50_disney/07/public/img/toby.jpg differ diff --git a/000_temp/50_disney/07/templates/about.gohtml b/000_temp/50_disney/07/templates/about.gohtml new file mode 100644 index 00000000..e7bc7333 --- /dev/null +++ b/000_temp/50_disney/07/templates/about.gohtml @@ -0,0 +1,14 @@ + + + + + + About + + + +

All about Disney!

+ + + \ No newline at end of file diff --git a/000_temp/50_disney/07/templates/index.gohtml b/000_temp/50_disney/07/templates/index.gohtml new file mode 100644 index 00000000..5d448c0b --- /dev/null +++ b/000_temp/50_disney/07/templates/index.gohtml @@ -0,0 +1,16 @@ + + + + + + Index + + + + +

Hello Disney!

+ + + + \ No newline at end of file diff --git a/000_temp/50_disney/07/templates/js.gohtml b/000_temp/50_disney/07/templates/js.gohtml new file mode 100644 index 00000000..41087b11 --- /dev/null +++ b/000_temp/50_disney/07/templates/js.gohtml @@ -0,0 +1,14 @@ + + + + + + About + + + +

{{.}}

+ + + \ No newline at end of file diff --git a/000_temp/50_disney/08/main.go b/000_temp/50_disney/08/main.go new file mode 100644 index 00000000..8361c048 --- /dev/null +++ b/000_temp/50_disney/08/main.go @@ -0,0 +1,32 @@ +package main + +import ( + "html/template" + "net/http" +) + +var tpl *template.Template + +func init() { + tpl = template.Must(template.ParseGlob("templates/*.gohtml")) +} + +func main() { + http.HandleFunc("/", index) + http.HandleFunc("/about", about) + http.HandleFunc("/dog", dog) + http.Handle("/assets/", http.StripPrefix("/assets", http.FileServer(http.Dir("./public")))) + http.ListenAndServe(":8080", nil) +} + +func index(w http.ResponseWriter, r *http.Request) { + tpl.ExecuteTemplate(w, "index.gohtml", nil) +} + +func about(w http.ResponseWriter, r *http.Request) { + tpl.ExecuteTemplate(w, "about.gohtml", nil) +} + +func dog(w http.ResponseWriter, r *http.Request) { + http.ServeFile(w, r, "public/img/toby.jpg") +} diff --git a/000_temp/50_disney/08/public/css/index.css b/000_temp/50_disney/08/public/css/index.css new file mode 100644 index 00000000..9c412bd7 --- /dev/null +++ b/000_temp/50_disney/08/public/css/index.css @@ -0,0 +1,25 @@ +html, body, h1 { + padding: 0; + border: 0; + margin: 0; + box-sizing: border-box; +} + +h1 { + color: blue; +} + +main { + height: 100vh; + display: flex; + flex-direction: row; + flex-wrap: nowrap; + justify-content: center; + align-items: center; + font-size: 5vw; + background-image: url("http://github.com/assets/img/neu.jpg"); + background-size: cover; + background-attachment: fixed; + background-repeat: no-repeat; + background-position: center; +} \ No newline at end of file diff --git a/000_temp/50_disney/08/public/img/neu.jpg b/000_temp/50_disney/08/public/img/neu.jpg new file mode 100644 index 00000000..cc2a09cd Binary files /dev/null and b/000_temp/50_disney/08/public/img/neu.jpg differ diff --git a/000_temp/50_disney/08/templates/about.gohtml b/000_temp/50_disney/08/templates/about.gohtml new file mode 100644 index 00000000..e7bc7333 --- /dev/null +++ b/000_temp/50_disney/08/templates/about.gohtml @@ -0,0 +1,14 @@ + + + + + + About + + + +

All about Disney!

+ + + \ No newline at end of file diff --git a/000_temp/50_disney/08/templates/index.gohtml b/000_temp/50_disney/08/templates/index.gohtml new file mode 100644 index 00000000..1b274b04 --- /dev/null +++ b/000_temp/50_disney/08/templates/index.gohtml @@ -0,0 +1,220 @@ + + + + + + Index + + + + +
+

Hello From Disney!

+
+ + + + + \ No newline at end of file diff --git a/000_temp/50_disney/08/templates/js.gohtml b/000_temp/50_disney/08/templates/js.gohtml new file mode 100644 index 00000000..41087b11 --- /dev/null +++ b/000_temp/50_disney/08/templates/js.gohtml @@ -0,0 +1,14 @@ + + + + + + About + + + +

{{.}}

+ + + \ No newline at end of file diff --git a/000_temp/51_bcrypt/main.go b/000_temp/51_bcrypt/main.go new file mode 100644 index 00000000..c417ff68 --- /dev/null +++ b/000_temp/51_bcrypt/main.go @@ -0,0 +1,26 @@ +package main + +import ( + "fmt" + "golang.org/x/crypto/bcrypt" +) + +func main() { + s := `password123` + bs, err := bcrypt.GenerateFromPassword([]byte(s), bcrypt.MinCost) + if err != nil { + fmt.Println(err) + } + fmt.Println(s) + fmt.Println(bs) + + loginPword1 := `password1234` + + err = bcrypt.CompareHashAndPassword(bs, []byte(loginPword1)) + if err != nil { + fmt.Println("YOU CAN'T LOGIN") + return + } + + fmt.Println("You're logged in") +} diff --git a/000_temp/52-race-condition/01/main.go b/000_temp/52-race-condition/01/main.go new file mode 100644 index 00000000..3b3bc863 --- /dev/null +++ b/000_temp/52-race-condition/01/main.go @@ -0,0 +1,33 @@ +package _1 + +import ( + "fmt" + "runtime" + "sync" +) + +var counter = 0 +var mu sync.Mutex + +func main() { + + const gs = 4 + var wg sync.WaitGroup + wg.Add(gs) + + for i := 0; i < gs; i++ { + go func() { + mu.Lock() + v := counter + // time.Sleep(time.Second * 2) + runtime.Gosched() // tells runtime to yield processor to other goroutine + v++ + counter = v + mu.Unlock() + wg.Done() + }() + } + wg.Wait() + + fmt.Println(counter) +} diff --git a/000_temp/52-race-condition/02/main.go b/000_temp/52-race-condition/02/main.go new file mode 100644 index 00000000..75458660 --- /dev/null +++ b/000_temp/52-race-condition/02/main.go @@ -0,0 +1,33 @@ +package main + +import ( + "fmt" + "runtime" + "sync" +) + +var wg sync.WaitGroup +var counter int + +func main() { + fmt.Println(runtime.NumCPU()) + + const gs = 100 + wg.Add(gs) + + for i := 0; i < gs; i++ { + go func() { + v := counter + // time.Sleep(time.Second) + runtime.Gosched() + v++ + counter = v + wg.Done() + }() + fmt.Println(runtime.NumGoroutine()) + fmt.Println(runtime.NumCPU()) + } + fmt.Println(runtime.NumGoroutine()) + wg.Wait() + fmt.Println(counter) +} diff --git a/000_temp/52-race-condition/03_race/main.go b/000_temp/52-race-condition/03_race/main.go new file mode 100644 index 00000000..991a46a6 --- /dev/null +++ b/000_temp/52-race-condition/03_race/main.go @@ -0,0 +1,33 @@ +package main + +import ( + "fmt" + "runtime" + "sync" +) + +func main() { + fmt.Println("CPUs:", runtime.NumCPU()) + fmt.Println("Goroutines:", runtime.NumGoroutine()) + + counter := 0 + + const gs = 100 + var wg sync.WaitGroup + wg.Add(gs) + + for i := 0; i < gs; i++ { + go func() { + v := counter + // time.Sleep(time.Second) + runtime.Gosched() + v++ + counter = v + wg.Done() + }() + fmt.Println("Goroutines:", runtime.NumGoroutine()) + } + wg.Wait() + fmt.Println("Goroutines:", runtime.NumGoroutine()) + fmt.Println("count:", counter) +} diff --git a/000_temp/52-race-condition/04_mutex/main.go b/000_temp/52-race-condition/04_mutex/main.go new file mode 100644 index 00000000..27f64acc --- /dev/null +++ b/000_temp/52-race-condition/04_mutex/main.go @@ -0,0 +1,37 @@ +package main + +import ( + "fmt" + "runtime" + "sync" +) + +func main() { + fmt.Println("CPUs:", runtime.NumCPU()) + fmt.Println("Goroutines:", runtime.NumGoroutine()) + + counter := 0 + + const gs = 100 + var wg sync.WaitGroup + wg.Add(gs) + + var mu sync.Mutex + + for i := 0; i < gs; i++ { + go func() { + mu.Lock() + v := counter + // time.Sleep(time.Second) + runtime.Gosched() + v++ + counter = v + mu.Unlock() + wg.Done() + }() + fmt.Println("Goroutines:", runtime.NumGoroutine()) + } + wg.Wait() + fmt.Println("Goroutines:", runtime.NumGoroutine()) + fmt.Println("count:", counter) +} diff --git a/000_temp/52-race-condition/05_atomic/main.go b/000_temp/52-race-condition/05_atomic/main.go new file mode 100644 index 00000000..77c01daf --- /dev/null +++ b/000_temp/52-race-condition/05_atomic/main.go @@ -0,0 +1,32 @@ +package main + +import ( + "fmt" + "runtime" + "sync" + "sync/atomic" +) + +func main() { + fmt.Println("CPUs:", runtime.NumCPU()) + fmt.Println("Goroutines:", runtime.NumGoroutine()) + + var counter int64 + + const gs = 100 + var wg sync.WaitGroup + wg.Add(gs) + + for i := 0; i < gs; i++ { + go func() { + atomic.AddInt64(&counter, 1) + runtime.Gosched() + fmt.Println("Counter\t", atomic.LoadInt64(&counter)) + wg.Done() + }() + fmt.Println("Goroutines:", runtime.NumGoroutine()) + } + wg.Wait() + fmt.Println("Goroutines:", runtime.NumGoroutine()) + fmt.Println("count:", counter) +} diff --git a/000_temp/52-race-condition/06_chan-pre-lecture/main.go b/000_temp/52-race-condition/06_chan-pre-lecture/main.go new file mode 100644 index 00000000..e3083e04 --- /dev/null +++ b/000_temp/52-race-condition/06_chan-pre-lecture/main.go @@ -0,0 +1,33 @@ +package main + +import ( + "fmt" + "runtime" + "sync" +) + +func main() { + counter := 0 + + c := make(chan int) + + const gs = 100 + var wg sync.WaitGroup + wg.Add(gs) + + go func() { + counter += <-c + fmt.Println("counter:\t", counter) + }() + + for i := 0; i < gs; i++ { + go func() { + c <- 1 + wg.Done() + }() + fmt.Println("Goroutines:", runtime.NumGoroutine()) + } + wg.Wait() + close(c) + fmt.Println("total count:", counter) +} diff --git a/000_temp/53-hello-world/main.go b/000_temp/53-hello-world/main.go new file mode 100644 index 00000000..68b2a816 --- /dev/null +++ b/000_temp/53-hello-world/main.go @@ -0,0 +1,7 @@ +package main + +import "fmt" + +func main() { + fmt.Println("Hello WORLD") +} diff --git a/000_temp/53_chan/main.go b/000_temp/53_chan/main.go new file mode 100644 index 00000000..e0372a36 --- /dev/null +++ b/000_temp/53_chan/main.go @@ -0,0 +1,31 @@ +package main + +import "fmt" + +func foo(f, b chan int) { + for i := 0; i < 10000; i++ { + select { + case v := <-f: + fmt.Println("from foo:", v) + case v := <-b: + fmt.Println("from bar:", v) + } + } + fmt.Println("about to exit") +} + +func main() { + f := make(chan int) + b := make(chan int) + go func() { + for i := 0; i < 5000; i++ { + f <- i + } + }() + go func() { + for i := 0; i < 5000; i++ { + b <- i + } + }() + foo(f, b) +} diff --git a/000_temp/54-paradise-html-css/01_html-css/index.html b/000_temp/54-paradise-html-css/01_html-css/index.html new file mode 100644 index 00000000..f68b77f0 --- /dev/null +++ b/000_temp/54-paradise-html-css/01_html-css/index.html @@ -0,0 +1,227 @@ + + + + + Title + + + + +
+
Company Logo
+
+ About + Contact + Sales +
+
+ +
+

PARADISE

+
+ + + + + \ No newline at end of file diff --git a/000_temp/54-paradise-html-css/01_html-css/main.css b/000_temp/54-paradise-html-css/01_html-css/main.css new file mode 100644 index 00000000..35b0ccc9 --- /dev/null +++ b/000_temp/54-paradise-html-css/01_html-css/main.css @@ -0,0 +1,55 @@ +html, body, header, main, h1, div { + border: 0; + padding: 0; + margin: 0; +} + + +header { + position: fixed; + top: 0; + left: 0; + width: 100vw; + height: 6vh; + border-bottom: 1px solid gray; + display: flex; + justify-content: space-between; + align-items: center; + background-color: rgba(21, 85, 138, 0.25); +} + +header > div { + /*border: 2px solid orange;*/ + height: 100%; + display: flex; + justify-content: center; + align-items: center; + font-size: 2.5rem; + color: white; + padding: 0 2rem; +} + +header > div > span { + padding: 0 2rem; +} + +header > div > span:hover { + color: blue; +} + +main { + height: 100vh; + display: flex; + justify-content: center; + align-items: center; + background-image: url(http://github.com/paradise.jpeg); + background-position: center; + background-repeat: no-repeat; + background-attachment: fixed; + background-size: cover; +} + +h1 { + color: white; + font-size: 12vw; +} \ No newline at end of file diff --git a/000_temp/54-paradise-html-css/01_html-css/main.go b/000_temp/54-paradise-html-css/01_html-css/main.go new file mode 100644 index 00000000..b05a38b6 --- /dev/null +++ b/000_temp/54-paradise-html-css/01_html-css/main.go @@ -0,0 +1 @@ +package _1_html_cs diff --git a/000_temp/55-website/files/css/index.css b/000_temp/55-website/files/css/index.css new file mode 100644 index 00000000..724561ab --- /dev/null +++ b/000_temp/55-website/files/css/index.css @@ -0,0 +1,15 @@ +main { + height: 100vh; + display: flex; + justify-content: center; + align-items: center; + background-image: url("http://github.com/imgs/paradise.jpeg"); + background-size: cover; + background-repeat: no-repeat; + background-position: center; +} + +h1 { + font-size: 8rem; + color: white; +} \ No newline at end of file diff --git a/000_temp/55-website/files/css/reset.css b/000_temp/55-website/files/css/reset.css new file mode 100644 index 00000000..f4670548 --- /dev/null +++ b/000_temp/55-website/files/css/reset.css @@ -0,0 +1,61 @@ +html, body, div, span, h1, h2, h3, h4, h5, h6, button, p, blockquote, pre, a, abbr, address, cite, code, del, dfn, em, img, ins, kbd, q, s, samp, small, strong, sub, sup, b, u, i, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td, article, aside, canvas, figure, figcaption, footer, header, nav, output, section, time, mark, audio, video, input, textarea, select { + margin: 0; + padding: 0; + border: 0; + /* inherits the browser's font properties: font-size 16px */ + font: inherit; + /* specifies the vertical alignment of an inline element */ + vertical-align: baseline; + + box-sizing: border-box; + + /* specifies the height of line boxes within the element. */ + line-height: 1; + + border-radius: 0; + + /* no outline around anything */ + outline: none; + + /* inherit the color value of the parent */ + color: inherit; +} + +ol, ul { + /* no bullets in lists */ + list-style: none; +} + +a { + /* no underline */ + text-decoration: none; +} + +button { + cursor: pointer; + background-color: transparent; +} + +blockquote, q { + /* no quotes */ + quotes: none; +} + +table { + /* no spacing between cells*/ + border-spacing: 0; + /*borders are collapsed - adjacent table cells share borders */ + border-collapse: collapse; +} + +input, select, progress{ + -webkit-appearance: none; + -moz-appearance: none; + appearance: none; +} + +svg { + width: 1.9em; + height: 1.9em; + fill: currentColor; +} \ No newline at end of file diff --git a/000_temp/55-website/files/imgs/paradise.jpeg b/000_temp/55-website/files/imgs/paradise.jpeg new file mode 100644 index 00000000..8e2b075a Binary files /dev/null and b/000_temp/55-website/files/imgs/paradise.jpeg differ diff --git a/000_temp/55-website/main.go b/000_temp/55-website/main.go new file mode 100644 index 00000000..51e61ade --- /dev/null +++ b/000_temp/55-website/main.go @@ -0,0 +1,17 @@ +package main + +import ( + "html/template" + "net/http" +) + +var tpl *template.Template + +func init() { + tpl = template.Must(template.ParseGlob("templates/*.gohtml")) +} + +func main() { + loadRoutes() + http.ListenAndServe(":8080", nil) +} diff --git a/000_temp/55-website/rAbout.go b/000_temp/55-website/rAbout.go new file mode 100644 index 00000000..12e1a845 --- /dev/null +++ b/000_temp/55-website/rAbout.go @@ -0,0 +1,13 @@ +package main + +import ( + "log" + "net/http" +) + +func about(w http.ResponseWriter, r *http.Request) { + err := tpl.ExecuteTemplate(w, "about.gohtml", nil) + if err != nil { + log.Println(err) + } +} diff --git a/000_temp/55-website/rIndex.go b/000_temp/55-website/rIndex.go new file mode 100644 index 00000000..fbf9401c --- /dev/null +++ b/000_temp/55-website/rIndex.go @@ -0,0 +1,13 @@ +package main + +import ( + "log" + "net/http" +) + +func index(w http.ResponseWriter, r *http.Request) { + err := tpl.ExecuteTemplate(w, "index.gohtml", nil) + if err != nil { + log.Println(err) + } +} diff --git a/000_temp/55-website/routes.go b/000_temp/55-website/routes.go new file mode 100644 index 00000000..fa2bc17a --- /dev/null +++ b/000_temp/55-website/routes.go @@ -0,0 +1,9 @@ +package main + +import "net/http" + +func loadRoutes() { + http.HandleFunc("/", index) + http.HandleFunc("/about", about) + http.Handle("/assets/", http.StripPrefix("/assets", http.FileServer(http.Dir("./files")))) +} diff --git a/000_temp/55-website/templates/about.gohtml b/000_temp/55-website/templates/about.gohtml new file mode 100644 index 00000000..36065e4a --- /dev/null +++ b/000_temp/55-website/templates/about.gohtml @@ -0,0 +1,14 @@ + + + + + + + Document + + + +

About

+ + \ No newline at end of file diff --git a/000_temp/55-website/templates/index.gohtml b/000_temp/55-website/templates/index.gohtml new file mode 100644 index 00000000..a4698a8a --- /dev/null +++ b/000_temp/55-website/templates/index.gohtml @@ -0,0 +1,19 @@ + + + + + + + Document + + + + + +
+

PARADISE

+
+ + + \ No newline at end of file diff --git a/000_temp/56_SVCC-17/01a/main.go b/000_temp/56_SVCC-17/01a/main.go new file mode 100644 index 00000000..4900f12c --- /dev/null +++ b/000_temp/56_SVCC-17/01a/main.go @@ -0,0 +1,15 @@ +package main + +import ( + "io" + "net/http" +) + +func main() { + http.HandleFunc("/", foo) + http.ListenAndServe(":8080", nil) +} + +func foo(w http.ResponseWriter, r *http.Request) { + io.WriteString(w, "Hello world") +} diff --git a/000_temp/56_SVCC-17/01b/main.go b/000_temp/56_SVCC-17/01b/main.go new file mode 100644 index 00000000..279761d5 --- /dev/null +++ b/000_temp/56_SVCC-17/01b/main.go @@ -0,0 +1,15 @@ +package main + +import ( + "io" + "net/http" +) + +func main() { + http.HandleFunc("/", foo) + http.ListenAndServe(":8080", nil) +} + +func foo(w http.ResponseWriter, r *http.Request) { + io.WriteString(w, "hello world") +} diff --git a/000_temp/56_SVCC-17/01c/main.go b/000_temp/56_SVCC-17/01c/main.go new file mode 100644 index 00000000..c48636a0 --- /dev/null +++ b/000_temp/56_SVCC-17/01c/main.go @@ -0,0 +1,15 @@ +package main + +import ( + "io" + "net/http" +) + +func main() { + http.HandleFunc("/", index) + http.ListenAndServe(":8080", nil) +} + +func index(w http.ResponseWriter, r *http.Request) { + io.WriteString(w, "Hello SVCC from Paypal") +} diff --git a/000_temp/56_SVCC-17/01d/main.go b/000_temp/56_SVCC-17/01d/main.go new file mode 100644 index 00000000..68f251c9 --- /dev/null +++ b/000_temp/56_SVCC-17/01d/main.go @@ -0,0 +1,15 @@ +package main + +import ( + "io" + "net/http" +) + +func main() { + http.HandleFunc("/", index) + http.ListenAndServe(":8080", nil) +} + +func index(w http.ResponseWriter, r *http.Request) { + io.WriteString(w, "Hello GDG Fresno") +} diff --git a/000_temp/56_SVCC-17/02a/main.go b/000_temp/56_SVCC-17/02a/main.go new file mode 100644 index 00000000..ffcebebe --- /dev/null +++ b/000_temp/56_SVCC-17/02a/main.go @@ -0,0 +1,21 @@ +package main + +import ( + "html/template" + "net/http" +) + +var tpl *template.Template + +func init() { + tpl = template.Must(template.ParseGlob("templates/*")) +} + +func main() { + http.HandleFunc("/", foo) + http.ListenAndServe(":8080", nil) +} + +func foo(w http.ResponseWriter, r *http.Request) { + tpl.ExecuteTemplate(w, "index.gohtml", nil) +} diff --git a/000_temp/56_SVCC-17/02a/templates/index.gohtml b/000_temp/56_SVCC-17/02a/templates/index.gohtml new file mode 100644 index 00000000..a905aa5b --- /dev/null +++ b/000_temp/56_SVCC-17/02a/templates/index.gohtml @@ -0,0 +1,12 @@ + + + + + Document + + + +

Hello world

+ + + \ No newline at end of file diff --git a/000_temp/56_SVCC-17/02b/main.go b/000_temp/56_SVCC-17/02b/main.go new file mode 100644 index 00000000..ffcebebe --- /dev/null +++ b/000_temp/56_SVCC-17/02b/main.go @@ -0,0 +1,21 @@ +package main + +import ( + "html/template" + "net/http" +) + +var tpl *template.Template + +func init() { + tpl = template.Must(template.ParseGlob("templates/*")) +} + +func main() { + http.HandleFunc("/", foo) + http.ListenAndServe(":8080", nil) +} + +func foo(w http.ResponseWriter, r *http.Request) { + tpl.ExecuteTemplate(w, "index.gohtml", nil) +} diff --git a/000_temp/56_SVCC-17/02b/templates/index.gohtml b/000_temp/56_SVCC-17/02b/templates/index.gohtml new file mode 100644 index 00000000..f86b947c --- /dev/null +++ b/000_temp/56_SVCC-17/02b/templates/index.gohtml @@ -0,0 +1,12 @@ + + + + + Document + + + +

HELLO FROM PAYPAL IN CALIFORNIA

+ + + \ No newline at end of file diff --git a/000_temp/56_SVCC-17/02c/main.go b/000_temp/56_SVCC-17/02c/main.go new file mode 100644 index 00000000..3c978b33 --- /dev/null +++ b/000_temp/56_SVCC-17/02c/main.go @@ -0,0 +1,21 @@ +package main + +import ( + "html/template" + "net/http" +) + +var tpl *template.Template + +func init() { + tpl = template.Must(template.ParseGlob("templates/*")) +} + +func main() { + http.HandleFunc("/", index) + http.ListenAndServe(":8080", nil) +} + +func index(w http.ResponseWriter, r *http.Request) { + tpl.ExecuteTemplate(w, "index.gohtml", nil) +} diff --git a/000_temp/56_SVCC-17/02c/templates/index.gohtml b/000_temp/56_SVCC-17/02c/templates/index.gohtml new file mode 100644 index 00000000..30bdf087 --- /dev/null +++ b/000_temp/56_SVCC-17/02c/templates/index.gohtml @@ -0,0 +1,12 @@ + + + + + Document + + + +

HELLO FROM SVCC 2017 WOOHOOO!!!

+ + + \ No newline at end of file diff --git a/000_temp/56_SVCC-17/02d/main.go b/000_temp/56_SVCC-17/02d/main.go new file mode 100644 index 00000000..21ff5e92 --- /dev/null +++ b/000_temp/56_SVCC-17/02d/main.go @@ -0,0 +1,21 @@ +package main + +import ( + "html/template" + "net/http" +) + +var tpl *template.Template + +func init() { + tpl = template.Must(template.ParseGlob("templates/*.gohtml")) +} + +func main() { + http.HandleFunc("/", index) + http.ListenAndServe(":8080", nil) +} + +func index(w http.ResponseWriter, r *http.Request) { + tpl.ExecuteTemplate(w, "index.gohtml", nil) +} diff --git a/000_temp/56_SVCC-17/02d/templates/index.gohtml b/000_temp/56_SVCC-17/02d/templates/index.gohtml new file mode 100644 index 00000000..8e1962ae --- /dev/null +++ b/000_temp/56_SVCC-17/02d/templates/index.gohtml @@ -0,0 +1,15 @@ + + + + + + + Document + + + +

HELLO FRESNO GDG

+ + + \ No newline at end of file diff --git a/000_temp/56_SVCC-17/03a/main.go b/000_temp/56_SVCC-17/03a/main.go new file mode 100644 index 00000000..ffcebebe --- /dev/null +++ b/000_temp/56_SVCC-17/03a/main.go @@ -0,0 +1,21 @@ +package main + +import ( + "html/template" + "net/http" +) + +var tpl *template.Template + +func init() { + tpl = template.Must(template.ParseGlob("templates/*")) +} + +func main() { + http.HandleFunc("/", foo) + http.ListenAndServe(":8080", nil) +} + +func foo(w http.ResponseWriter, r *http.Request) { + tpl.ExecuteTemplate(w, "index.gohtml", nil) +} diff --git a/000_temp/56_SVCC-17/03a/templates/incl-footer.gohtml b/000_temp/56_SVCC-17/03a/templates/incl-footer.gohtml new file mode 100644 index 00000000..37bdaade --- /dev/null +++ b/000_temp/56_SVCC-17/03a/templates/incl-footer.gohtml @@ -0,0 +1,4 @@ +{{define "footer"}} + + +{{end}} \ No newline at end of file diff --git a/000_temp/56_SVCC-17/03a/templates/incl-header.gohtml b/000_temp/56_SVCC-17/03a/templates/incl-header.gohtml new file mode 100644 index 00000000..66c6b210 --- /dev/null +++ b/000_temp/56_SVCC-17/03a/templates/incl-header.gohtml @@ -0,0 +1,9 @@ +{{define "header"}} + + + + + GOODNESS + + +{{end}} \ No newline at end of file diff --git a/000_temp/56_SVCC-17/03a/templates/index.gohtml b/000_temp/56_SVCC-17/03a/templates/index.gohtml new file mode 100644 index 00000000..febe83e7 --- /dev/null +++ b/000_temp/56_SVCC-17/03a/templates/index.gohtml @@ -0,0 +1,5 @@ +{{template "header"}} + +

Hello world

+ +{{template "footer"}} diff --git a/000_temp/56_SVCC-17/03b/main.go b/000_temp/56_SVCC-17/03b/main.go new file mode 100644 index 00000000..ffcebebe --- /dev/null +++ b/000_temp/56_SVCC-17/03b/main.go @@ -0,0 +1,21 @@ +package main + +import ( + "html/template" + "net/http" +) + +var tpl *template.Template + +func init() { + tpl = template.Must(template.ParseGlob("templates/*")) +} + +func main() { + http.HandleFunc("/", foo) + http.ListenAndServe(":8080", nil) +} + +func foo(w http.ResponseWriter, r *http.Request) { + tpl.ExecuteTemplate(w, "index.gohtml", nil) +} diff --git a/000_temp/56_SVCC-17/03b/templates/incl-footer.gohtml b/000_temp/56_SVCC-17/03b/templates/incl-footer.gohtml new file mode 100644 index 00000000..37bdaade --- /dev/null +++ b/000_temp/56_SVCC-17/03b/templates/incl-footer.gohtml @@ -0,0 +1,4 @@ +{{define "footer"}} + + +{{end}} \ No newline at end of file diff --git a/000_temp/56_SVCC-17/03b/templates/incl-header.gohtml b/000_temp/56_SVCC-17/03b/templates/incl-header.gohtml new file mode 100644 index 00000000..66c6b210 --- /dev/null +++ b/000_temp/56_SVCC-17/03b/templates/incl-header.gohtml @@ -0,0 +1,9 @@ +{{define "header"}} + + + + + GOODNESS + + +{{end}} \ No newline at end of file diff --git a/000_temp/56_SVCC-17/03b/templates/index.gohtml b/000_temp/56_SVCC-17/03b/templates/index.gohtml new file mode 100644 index 00000000..a060f207 --- /dev/null +++ b/000_temp/56_SVCC-17/03b/templates/index.gohtml @@ -0,0 +1,4 @@ + +{{template "header"}} +

HELLO FROM PAYPAL IN CALIFORNIA

+{{template "footer"}} diff --git a/000_temp/56_SVCC-17/03c/main.go b/000_temp/56_SVCC-17/03c/main.go new file mode 100644 index 00000000..3c978b33 --- /dev/null +++ b/000_temp/56_SVCC-17/03c/main.go @@ -0,0 +1,21 @@ +package main + +import ( + "html/template" + "net/http" +) + +var tpl *template.Template + +func init() { + tpl = template.Must(template.ParseGlob("templates/*")) +} + +func main() { + http.HandleFunc("/", index) + http.ListenAndServe(":8080", nil) +} + +func index(w http.ResponseWriter, r *http.Request) { + tpl.ExecuteTemplate(w, "index.gohtml", nil) +} diff --git a/000_temp/56_SVCC-17/03c/templates/incl-footer.gohtml b/000_temp/56_SVCC-17/03c/templates/incl-footer.gohtml new file mode 100644 index 00000000..37bdaade --- /dev/null +++ b/000_temp/56_SVCC-17/03c/templates/incl-footer.gohtml @@ -0,0 +1,4 @@ +{{define "footer"}} + + +{{end}} \ No newline at end of file diff --git a/000_temp/56_SVCC-17/03c/templates/incl-header.gohtml b/000_temp/56_SVCC-17/03c/templates/incl-header.gohtml new file mode 100644 index 00000000..e3ce1c54 --- /dev/null +++ b/000_temp/56_SVCC-17/03c/templates/incl-header.gohtml @@ -0,0 +1,7 @@ +{{define "header"}} + + + + + Document +{{end}} \ No newline at end of file diff --git a/000_temp/56_SVCC-17/03c/templates/index.gohtml b/000_temp/56_SVCC-17/03c/templates/index.gohtml new file mode 100644 index 00000000..43777d3f --- /dev/null +++ b/000_temp/56_SVCC-17/03c/templates/index.gohtml @@ -0,0 +1,7 @@ +{{template "header"}} + + + +

HELLO FROM SVCC 2017 WOOHOOO!!!

+ +{{template "footer"}} \ No newline at end of file diff --git a/000_temp/56_SVCC-17/03d/main.go b/000_temp/56_SVCC-17/03d/main.go new file mode 100644 index 00000000..21ff5e92 --- /dev/null +++ b/000_temp/56_SVCC-17/03d/main.go @@ -0,0 +1,21 @@ +package main + +import ( + "html/template" + "net/http" +) + +var tpl *template.Template + +func init() { + tpl = template.Must(template.ParseGlob("templates/*.gohtml")) +} + +func main() { + http.HandleFunc("/", index) + http.ListenAndServe(":8080", nil) +} + +func index(w http.ResponseWriter, r *http.Request) { + tpl.ExecuteTemplate(w, "index.gohtml", nil) +} diff --git a/000_temp/56_SVCC-17/03d/templates/inc-footer.gohtml b/000_temp/56_SVCC-17/03d/templates/inc-footer.gohtml new file mode 100644 index 00000000..37bdaade --- /dev/null +++ b/000_temp/56_SVCC-17/03d/templates/inc-footer.gohtml @@ -0,0 +1,4 @@ +{{define "footer"}} + + +{{end}} \ No newline at end of file diff --git a/000_temp/56_SVCC-17/03d/templates/inc-header.gohtml b/000_temp/56_SVCC-17/03d/templates/inc-header.gohtml new file mode 100644 index 00000000..757bd31c --- /dev/null +++ b/000_temp/56_SVCC-17/03d/templates/inc-header.gohtml @@ -0,0 +1,9 @@ +{{define "header"}} + + + + + + +{{end}} \ No newline at end of file diff --git a/000_temp/56_SVCC-17/03d/templates/index.gohtml b/000_temp/56_SVCC-17/03d/templates/index.gohtml new file mode 100644 index 00000000..412ba59a --- /dev/null +++ b/000_temp/56_SVCC-17/03d/templates/index.gohtml @@ -0,0 +1,8 @@ +{{template "header"}} + Document + + + +

HELLO FRESNO GDG

+ +{{template "footer"}} \ No newline at end of file diff --git a/000_temp/56_SVCC-17/04a/main.go b/000_temp/56_SVCC-17/04a/main.go new file mode 100644 index 00000000..d24c8f45 --- /dev/null +++ b/000_temp/56_SVCC-17/04a/main.go @@ -0,0 +1,26 @@ +package main + +import ( + "html/template" + "net/http" +) + +var tpl *template.Template + +func init() { + tpl = template.Must(template.ParseGlob("templates/*")) +} + +func main() { + http.HandleFunc("/", index) + http.HandleFunc("/about", about) + http.ListenAndServe(":8080", nil) +} + +func index(w http.ResponseWriter, r *http.Request) { + tpl.ExecuteTemplate(w, "index.gohtml", "ACME, INC") +} + +func about(w http.ResponseWriter, r *http.Request) { + tpl.ExecuteTemplate(w, "about.gohtml", "OUR TEAM") +} diff --git a/000_temp/56_SVCC-17/04a/templates/about.gohtml b/000_temp/56_SVCC-17/04a/templates/about.gohtml new file mode 100644 index 00000000..4b76c606 --- /dev/null +++ b/000_temp/56_SVCC-17/04a/templates/about.gohtml @@ -0,0 +1,5 @@ +{{template "header" .}} + +

ABOUT OUR TEAM

+ +{{template "footer"}} diff --git a/000_temp/56_SVCC-17/04a/templates/incl-footer.gohtml b/000_temp/56_SVCC-17/04a/templates/incl-footer.gohtml new file mode 100644 index 00000000..37bdaade --- /dev/null +++ b/000_temp/56_SVCC-17/04a/templates/incl-footer.gohtml @@ -0,0 +1,4 @@ +{{define "footer"}} + + +{{end}} \ No newline at end of file diff --git a/000_temp/56_SVCC-17/04a/templates/incl-header.gohtml b/000_temp/56_SVCC-17/04a/templates/incl-header.gohtml new file mode 100644 index 00000000..16cfc4a3 --- /dev/null +++ b/000_temp/56_SVCC-17/04a/templates/incl-header.gohtml @@ -0,0 +1,9 @@ +{{define "header"}} + + + + + {{.}} + + +{{end}} \ No newline at end of file diff --git a/000_temp/56_SVCC-17/04a/templates/index.gohtml b/000_temp/56_SVCC-17/04a/templates/index.gohtml new file mode 100644 index 00000000..3ccfd708 --- /dev/null +++ b/000_temp/56_SVCC-17/04a/templates/index.gohtml @@ -0,0 +1,5 @@ +{{template "header" .}} + +

Hello from ACME INC

+ +{{template "footer"}} diff --git a/000_temp/56_SVCC-17/04b/main.go b/000_temp/56_SVCC-17/04b/main.go new file mode 100644 index 00000000..a6b8f5ca --- /dev/null +++ b/000_temp/56_SVCC-17/04b/main.go @@ -0,0 +1,26 @@ +package main + +import ( + "html/template" + "net/http" +) + +var tpl *template.Template + +func init() { + tpl = template.Must(template.ParseGlob("templates/*")) +} + +func main() { + http.HandleFunc("/", index) + http.HandleFunc("/about", about) + http.ListenAndServe(":8080", nil) +} + +func index(w http.ResponseWriter, r *http.Request) { + tpl.ExecuteTemplate(w, "index.gohtml", "ACME INC") +} + +func about(w http.ResponseWriter, r *http.Request) { + tpl.ExecuteTemplate(w, "about.gohtml", "THE TEAM") +} diff --git a/000_temp/56_SVCC-17/04b/templates/about.gohtml b/000_temp/56_SVCC-17/04b/templates/about.gohtml new file mode 100644 index 00000000..d0cd00ab --- /dev/null +++ b/000_temp/56_SVCC-17/04b/templates/about.gohtml @@ -0,0 +1,4 @@ + +{{template "header" .}} +

all about our team

+{{template "footer"}} diff --git a/000_temp/56_SVCC-17/04b/templates/incl-footer.gohtml b/000_temp/56_SVCC-17/04b/templates/incl-footer.gohtml new file mode 100644 index 00000000..37bdaade --- /dev/null +++ b/000_temp/56_SVCC-17/04b/templates/incl-footer.gohtml @@ -0,0 +1,4 @@ +{{define "footer"}} + + +{{end}} \ No newline at end of file diff --git a/000_temp/56_SVCC-17/04b/templates/incl-header.gohtml b/000_temp/56_SVCC-17/04b/templates/incl-header.gohtml new file mode 100644 index 00000000..16cfc4a3 --- /dev/null +++ b/000_temp/56_SVCC-17/04b/templates/incl-header.gohtml @@ -0,0 +1,9 @@ +{{define "header"}} + + + + + {{.}} + + +{{end}} \ No newline at end of file diff --git a/000_temp/56_SVCC-17/04b/templates/index.gohtml b/000_temp/56_SVCC-17/04b/templates/index.gohtml new file mode 100644 index 00000000..1abb7d76 --- /dev/null +++ b/000_temp/56_SVCC-17/04b/templates/index.gohtml @@ -0,0 +1,4 @@ + +{{template "header" .}} +

all about acme inc

+{{template "footer"}} diff --git a/000_temp/56_SVCC-17/04c/main.go b/000_temp/56_SVCC-17/04c/main.go new file mode 100644 index 00000000..dfb4b33f --- /dev/null +++ b/000_temp/56_SVCC-17/04c/main.go @@ -0,0 +1,26 @@ +package main + +import ( + "html/template" + "net/http" +) + +var tpl *template.Template + +func init() { + tpl = template.Must(template.ParseGlob("templates/*")) +} + +func main() { + http.HandleFunc("/", index) + http.HandleFunc("/about", about) + http.ListenAndServe(":8080", nil) +} + +func index(w http.ResponseWriter, r *http.Request) { + tpl.ExecuteTemplate(w, "index.gohtml", "ACME INC") +} + +func about(w http.ResponseWriter, r *http.Request) { + tpl.ExecuteTemplate(w, "about.gohtml", "ABOUT OUR TEAM") +} diff --git a/000_temp/56_SVCC-17/04c/templates/about.gohtml b/000_temp/56_SVCC-17/04c/templates/about.gohtml new file mode 100644 index 00000000..22f0b58b --- /dev/null +++ b/000_temp/56_SVCC-17/04c/templates/about.gohtml @@ -0,0 +1,7 @@ +{{template "header" .}} + + + +

ABOUT US

+ +{{template "footer"}} \ No newline at end of file diff --git a/000_temp/56_SVCC-17/04c/templates/incl-footer.gohtml b/000_temp/56_SVCC-17/04c/templates/incl-footer.gohtml new file mode 100644 index 00000000..37bdaade --- /dev/null +++ b/000_temp/56_SVCC-17/04c/templates/incl-footer.gohtml @@ -0,0 +1,4 @@ +{{define "footer"}} + + +{{end}} \ No newline at end of file diff --git a/000_temp/56_SVCC-17/04c/templates/incl-header.gohtml b/000_temp/56_SVCC-17/04c/templates/incl-header.gohtml new file mode 100644 index 00000000..c9b0ef1e --- /dev/null +++ b/000_temp/56_SVCC-17/04c/templates/incl-header.gohtml @@ -0,0 +1,7 @@ +{{define "header"}} + + + + + {{.}} +{{end}} \ No newline at end of file diff --git a/000_temp/56_SVCC-17/04c/templates/index.gohtml b/000_temp/56_SVCC-17/04c/templates/index.gohtml new file mode 100644 index 00000000..f904703d --- /dev/null +++ b/000_temp/56_SVCC-17/04c/templates/index.gohtml @@ -0,0 +1,7 @@ +{{template "header" .}} + + + +

HELLO FROM SVCC 2017 WOOHOOO!!!

+ +{{template "footer"}} \ No newline at end of file diff --git a/000_temp/56_SVCC-17/04d/main.go b/000_temp/56_SVCC-17/04d/main.go new file mode 100644 index 00000000..99d15727 --- /dev/null +++ b/000_temp/56_SVCC-17/04d/main.go @@ -0,0 +1,26 @@ +package main + +import ( + "html/template" + "net/http" +) + +var tpl *template.Template + +func init() { + tpl = template.Must(template.ParseGlob("templates/*.gohtml")) +} + +func main() { + http.HandleFunc("/", index) + http.HandleFunc("/about/", about) + http.ListenAndServe(":8080", nil) +} + +func index(w http.ResponseWriter, r *http.Request) { + tpl.ExecuteTemplate(w, "index.gohtml", nil) +} + +func about(w http.ResponseWriter, r *http.Request) { + tpl.ExecuteTemplate(w, "about.gohtml", nil) +} diff --git a/000_temp/56_SVCC-17/04d/templates/about.gohtml b/000_temp/56_SVCC-17/04d/templates/about.gohtml new file mode 100644 index 00000000..25c194ca --- /dev/null +++ b/000_temp/56_SVCC-17/04d/templates/about.gohtml @@ -0,0 +1,8 @@ +{{template "header"}} + ABOUT + + + +

ABOUT

+ +{{template "footer"}} \ No newline at end of file diff --git a/000_temp/56_SVCC-17/04d/templates/inc-footer.gohtml b/000_temp/56_SVCC-17/04d/templates/inc-footer.gohtml new file mode 100644 index 00000000..37bdaade --- /dev/null +++ b/000_temp/56_SVCC-17/04d/templates/inc-footer.gohtml @@ -0,0 +1,4 @@ +{{define "footer"}} + + +{{end}} \ No newline at end of file diff --git a/000_temp/56_SVCC-17/04d/templates/inc-header.gohtml b/000_temp/56_SVCC-17/04d/templates/inc-header.gohtml new file mode 100644 index 00000000..757bd31c --- /dev/null +++ b/000_temp/56_SVCC-17/04d/templates/inc-header.gohtml @@ -0,0 +1,9 @@ +{{define "header"}} + + + + + + +{{end}} \ No newline at end of file diff --git a/000_temp/56_SVCC-17/04d/templates/index.gohtml b/000_temp/56_SVCC-17/04d/templates/index.gohtml new file mode 100644 index 00000000..412ba59a --- /dev/null +++ b/000_temp/56_SVCC-17/04d/templates/index.gohtml @@ -0,0 +1,8 @@ +{{template "header"}} + Document + + + +

HELLO FRESNO GDG

+ +{{template "footer"}} \ No newline at end of file diff --git a/000_temp/56_SVCC-17/05a/main.go b/000_temp/56_SVCC-17/05a/main.go new file mode 100644 index 00000000..1ea5b277 --- /dev/null +++ b/000_temp/56_SVCC-17/05a/main.go @@ -0,0 +1,36 @@ +package main + +import ( + "html/template" + "net/http" +) + +var tpl *template.Template + +func init() { + tpl = template.Must(template.ParseGlob("templates/*")) +} + +func main() { + http.HandleFunc("/", index) + http.HandleFunc("/about", about) + http.ListenAndServe(":8080", nil) +} + +func index(w http.ResponseWriter, r *http.Request) { + tpl.ExecuteTemplate(w, "index.gohtml", "ACME, INC") +} + +func about(w http.ResponseWriter, r *http.Request) { + type customData struct { + Title string + Members []string + } + + cd := customData{ + Title: "OUR TEAM", + Members: []string{"Moneypenny", "Bond", "Q", "M"}, + } + + tpl.ExecuteTemplate(w, "about.gohtml", cd) +} diff --git a/000_temp/56_SVCC-17/05a/templates/about.gohtml b/000_temp/56_SVCC-17/05a/templates/about.gohtml new file mode 100644 index 00000000..55ae202e --- /dev/null +++ b/000_temp/56_SVCC-17/05a/templates/about.gohtml @@ -0,0 +1,9 @@ +{{template "header" .Title}} + +

ABOUT OUR TEAM

+ +{{range .Members}} +{{.}}
+{{end}} + +{{template "footer"}} diff --git a/000_temp/56_SVCC-17/05a/templates/incl-footer.gohtml b/000_temp/56_SVCC-17/05a/templates/incl-footer.gohtml new file mode 100644 index 00000000..37bdaade --- /dev/null +++ b/000_temp/56_SVCC-17/05a/templates/incl-footer.gohtml @@ -0,0 +1,4 @@ +{{define "footer"}} + + +{{end}} \ No newline at end of file diff --git a/000_temp/56_SVCC-17/05a/templates/incl-header.gohtml b/000_temp/56_SVCC-17/05a/templates/incl-header.gohtml new file mode 100644 index 00000000..16cfc4a3 --- /dev/null +++ b/000_temp/56_SVCC-17/05a/templates/incl-header.gohtml @@ -0,0 +1,9 @@ +{{define "header"}} + + + + + {{.}} + + +{{end}} \ No newline at end of file diff --git a/000_temp/56_SVCC-17/05a/templates/index.gohtml b/000_temp/56_SVCC-17/05a/templates/index.gohtml new file mode 100644 index 00000000..3ccfd708 --- /dev/null +++ b/000_temp/56_SVCC-17/05a/templates/index.gohtml @@ -0,0 +1,5 @@ +{{template "header" .}} + +

Hello from ACME INC

+ +{{template "footer"}} diff --git a/000_temp/56_SVCC-17/05b/main.go b/000_temp/56_SVCC-17/05b/main.go new file mode 100644 index 00000000..4f1d2d8a --- /dev/null +++ b/000_temp/56_SVCC-17/05b/main.go @@ -0,0 +1,36 @@ +package main + +import ( + "html/template" + "net/http" +) + +var tpl *template.Template + +func init() { + tpl = template.Must(template.ParseGlob("templates/*")) +} + +func main() { + http.HandleFunc("/", index) + http.HandleFunc("/about", about) + http.ListenAndServe(":8080", nil) +} + +func index(w http.ResponseWriter, r *http.Request) { + tpl.ExecuteTemplate(w, "index.gohtml", "ACME INC") +} + +func about(w http.ResponseWriter, r *http.Request) { + type customData struct { + Title string + Members []string + } + + cd := customData{ + Title: "THE TEAM", + Members: []string{"Moneypenny", "Bond", "Q", "M"}, + } + + tpl.ExecuteTemplate(w, "about.gohtml", cd) +} diff --git a/000_temp/56_SVCC-17/05b/templates/about.gohtml b/000_temp/56_SVCC-17/05b/templates/about.gohtml new file mode 100644 index 00000000..e145ec66 --- /dev/null +++ b/000_temp/56_SVCC-17/05b/templates/about.gohtml @@ -0,0 +1,7 @@ + +{{template "header" .Title}} +

all about our team

+{{range .Members}} +{{.}}
+{{end}} +{{template "footer"}} diff --git a/000_temp/56_SVCC-17/05b/templates/incl-footer.gohtml b/000_temp/56_SVCC-17/05b/templates/incl-footer.gohtml new file mode 100644 index 00000000..37bdaade --- /dev/null +++ b/000_temp/56_SVCC-17/05b/templates/incl-footer.gohtml @@ -0,0 +1,4 @@ +{{define "footer"}} + + +{{end}} \ No newline at end of file diff --git a/000_temp/56_SVCC-17/05b/templates/incl-header.gohtml b/000_temp/56_SVCC-17/05b/templates/incl-header.gohtml new file mode 100644 index 00000000..16cfc4a3 --- /dev/null +++ b/000_temp/56_SVCC-17/05b/templates/incl-header.gohtml @@ -0,0 +1,9 @@ +{{define "header"}} + + + + + {{.}} + + +{{end}} \ No newline at end of file diff --git a/000_temp/56_SVCC-17/05b/templates/index.gohtml b/000_temp/56_SVCC-17/05b/templates/index.gohtml new file mode 100644 index 00000000..1abb7d76 --- /dev/null +++ b/000_temp/56_SVCC-17/05b/templates/index.gohtml @@ -0,0 +1,4 @@ + +{{template "header" .}} +

all about acme inc

+{{template "footer"}} diff --git a/000_temp/56_SVCC-17/05c/main.go b/000_temp/56_SVCC-17/05c/main.go new file mode 100644 index 00000000..f96b5c90 --- /dev/null +++ b/000_temp/56_SVCC-17/05c/main.go @@ -0,0 +1,36 @@ +package main + +import ( + "html/template" + "net/http" +) + +var tpl *template.Template + +func init() { + tpl = template.Must(template.ParseGlob("templates/*")) +} + +func main() { + http.HandleFunc("/", index) + http.HandleFunc("/about", about) + http.ListenAndServe(":8080", nil) +} + +func index(w http.ResponseWriter, r *http.Request) { + tpl.ExecuteTemplate(w, "index.gohtml", "ACME INC") +} + +func about(w http.ResponseWriter, r *http.Request) { + type customData struct { + Title string + Members []string + } + + cd := customData{ + Title: "ABOUT OUR TEAM", + Members: []string{"Moneypenny", "Bond", "Q", "M"}, + } + + tpl.ExecuteTemplate(w, "about.gohtml", cd) +} diff --git a/000_temp/56_SVCC-17/05c/templates/about.gohtml b/000_temp/56_SVCC-17/05c/templates/about.gohtml new file mode 100644 index 00000000..4a05bdf1 --- /dev/null +++ b/000_temp/56_SVCC-17/05c/templates/about.gohtml @@ -0,0 +1,10 @@ +{{template "header" .Title}} + + + +

ABOUT US

+{{range .Members}} +{{.}}
+{{end}} + +{{template "footer"}} \ No newline at end of file diff --git a/000_temp/56_SVCC-17/05c/templates/incl-footer.gohtml b/000_temp/56_SVCC-17/05c/templates/incl-footer.gohtml new file mode 100644 index 00000000..37bdaade --- /dev/null +++ b/000_temp/56_SVCC-17/05c/templates/incl-footer.gohtml @@ -0,0 +1,4 @@ +{{define "footer"}} + + +{{end}} \ No newline at end of file diff --git a/000_temp/56_SVCC-17/05c/templates/incl-header.gohtml b/000_temp/56_SVCC-17/05c/templates/incl-header.gohtml new file mode 100644 index 00000000..c9b0ef1e --- /dev/null +++ b/000_temp/56_SVCC-17/05c/templates/incl-header.gohtml @@ -0,0 +1,7 @@ +{{define "header"}} + + + + + {{.}} +{{end}} \ No newline at end of file diff --git a/000_temp/56_SVCC-17/05c/templates/index.gohtml b/000_temp/56_SVCC-17/05c/templates/index.gohtml new file mode 100644 index 00000000..f904703d --- /dev/null +++ b/000_temp/56_SVCC-17/05c/templates/index.gohtml @@ -0,0 +1,7 @@ +{{template "header" .}} + + + +

HELLO FROM SVCC 2017 WOOHOOO!!!

+ +{{template "footer"}} \ No newline at end of file diff --git a/000_temp/56_SVCC-17/05d/main.go b/000_temp/56_SVCC-17/05d/main.go new file mode 100644 index 00000000..d0b8460f --- /dev/null +++ b/000_temp/56_SVCC-17/05d/main.go @@ -0,0 +1,36 @@ +package main + +import ( + "html/template" + "net/http" +) + +var tpl *template.Template + +func init() { + tpl = template.Must(template.ParseGlob("templates/*.gohtml")) +} + +func main() { + http.HandleFunc("/", index) + http.HandleFunc("/about/", about) + http.ListenAndServe(":8080", nil) +} + +func index(w http.ResponseWriter, r *http.Request) { + tpl.ExecuteTemplate(w, "index.gohtml", 42) +} + +func about(w http.ResponseWriter, r *http.Request) { + type customData struct { + Title string + Members []string + } + + cd := customData{ + Title: "ABOUT OUR TEAM", + Members: []string{"Moneypenny", "Bond", "Q", "M"}, + } + + tpl.ExecuteTemplate(w, "about.gohtml", cd) +} diff --git a/000_temp/56_SVCC-17/05d/templates/about.gohtml b/000_temp/56_SVCC-17/05d/templates/about.gohtml new file mode 100644 index 00000000..260fa388 --- /dev/null +++ b/000_temp/56_SVCC-17/05d/templates/about.gohtml @@ -0,0 +1,16 @@ +{{template "header"}} + {{.Title}} + + + +

{{.}}

+

{{.Title}}

+

{{.Members}}

+ + + +{{template "footer"}} \ No newline at end of file diff --git a/000_temp/56_SVCC-17/05d/templates/inc-footer.gohtml b/000_temp/56_SVCC-17/05d/templates/inc-footer.gohtml new file mode 100644 index 00000000..37bdaade --- /dev/null +++ b/000_temp/56_SVCC-17/05d/templates/inc-footer.gohtml @@ -0,0 +1,4 @@ +{{define "footer"}} + + +{{end}} \ No newline at end of file diff --git a/000_temp/56_SVCC-17/05d/templates/inc-header.gohtml b/000_temp/56_SVCC-17/05d/templates/inc-header.gohtml new file mode 100644 index 00000000..757bd31c --- /dev/null +++ b/000_temp/56_SVCC-17/05d/templates/inc-header.gohtml @@ -0,0 +1,9 @@ +{{define "header"}} + + + + + + +{{end}} \ No newline at end of file diff --git a/000_temp/56_SVCC-17/05d/templates/index.gohtml b/000_temp/56_SVCC-17/05d/templates/index.gohtml new file mode 100644 index 00000000..28444399 --- /dev/null +++ b/000_temp/56_SVCC-17/05d/templates/index.gohtml @@ -0,0 +1,8 @@ +{{template "header"}} + Document + + + +

{{.}}

+ +{{template "footer"}} \ No newline at end of file diff --git a/000_temp/56_SVCC-17/06a/assets/index.css b/000_temp/56_SVCC-17/06a/assets/index.css new file mode 100644 index 00000000..4717ad4b --- /dev/null +++ b/000_temp/56_SVCC-17/06a/assets/index.css @@ -0,0 +1,3 @@ +h1 { + color: red; +} \ No newline at end of file diff --git a/000_temp/56_SVCC-17/06a/assets/paradise.jpeg b/000_temp/56_SVCC-17/06a/assets/paradise.jpeg new file mode 100644 index 00000000..8e2b075a Binary files /dev/null and b/000_temp/56_SVCC-17/06a/assets/paradise.jpeg differ diff --git a/000_temp/56_SVCC-17/06a/main.go b/000_temp/56_SVCC-17/06a/main.go new file mode 100644 index 00000000..2c2eb857 --- /dev/null +++ b/000_temp/56_SVCC-17/06a/main.go @@ -0,0 +1,37 @@ +package main + +import ( + "html/template" + "net/http" +) + +var tpl *template.Template + +func init() { + tpl = template.Must(template.ParseGlob("templates/*")) +} + +func main() { + http.HandleFunc("/", index) + http.HandleFunc("/about", about) + http.Handle("/assets/", http.StripPrefix("/assets", http.FileServer(http.Dir("./assets")))) + http.ListenAndServe(":8080", nil) +} + +func index(w http.ResponseWriter, r *http.Request) { + tpl.ExecuteTemplate(w, "index.gohtml", "ACME, INC") +} + +func about(w http.ResponseWriter, r *http.Request) { + type customData struct { + Title string + Members []string + } + + cd := customData{ + Title: "OUR TEAM", + Members: []string{"Moneypenny", "Bond", "Q", "M"}, + } + + tpl.ExecuteTemplate(w, "about.gohtml", cd) +} diff --git a/000_temp/56_SVCC-17/06a/templates/about.gohtml b/000_temp/56_SVCC-17/06a/templates/about.gohtml new file mode 100644 index 00000000..55ae202e --- /dev/null +++ b/000_temp/56_SVCC-17/06a/templates/about.gohtml @@ -0,0 +1,9 @@ +{{template "header" .Title}} + +

ABOUT OUR TEAM

+ +{{range .Members}} +{{.}}
+{{end}} + +{{template "footer"}} diff --git a/000_temp/56_SVCC-17/06a/templates/incl-footer.gohtml b/000_temp/56_SVCC-17/06a/templates/incl-footer.gohtml new file mode 100644 index 00000000..37bdaade --- /dev/null +++ b/000_temp/56_SVCC-17/06a/templates/incl-footer.gohtml @@ -0,0 +1,4 @@ +{{define "footer"}} + + +{{end}} \ No newline at end of file diff --git a/000_temp/56_SVCC-17/06a/templates/incl-header.gohtml b/000_temp/56_SVCC-17/06a/templates/incl-header.gohtml new file mode 100644 index 00000000..f4a24be7 --- /dev/null +++ b/000_temp/56_SVCC-17/06a/templates/incl-header.gohtml @@ -0,0 +1,10 @@ +{{define "header"}} + + + + + {{.}} + + + +{{end}} \ No newline at end of file diff --git a/000_temp/56_SVCC-17/06a/templates/index.gohtml b/000_temp/56_SVCC-17/06a/templates/index.gohtml new file mode 100644 index 00000000..4e33f167 --- /dev/null +++ b/000_temp/56_SVCC-17/06a/templates/index.gohtml @@ -0,0 +1,7 @@ +{{template "header" .}} + +

Hello from ACME INC

+ + + +{{template "footer"}} diff --git a/000_temp/56_SVCC-17/06b/assets/paradise.jpeg b/000_temp/56_SVCC-17/06b/assets/paradise.jpeg new file mode 100644 index 00000000..8e2b075a Binary files /dev/null and b/000_temp/56_SVCC-17/06b/assets/paradise.jpeg differ diff --git a/000_temp/56_SVCC-17/06b/main.go b/000_temp/56_SVCC-17/06b/main.go new file mode 100644 index 00000000..029717f3 --- /dev/null +++ b/000_temp/56_SVCC-17/06b/main.go @@ -0,0 +1,37 @@ +package main + +import ( + "html/template" + "net/http" +) + +var tpl *template.Template + +func init() { + tpl = template.Must(template.ParseGlob("templates/*")) +} + +func main() { + http.HandleFunc("/", index) + http.HandleFunc("/about", about) + http.Handle("/assets/", http.StripPrefix("/assets", http.FileServer(http.Dir("./assets")))) + http.ListenAndServe(":8080", nil) +} + +func index(w http.ResponseWriter, r *http.Request) { + tpl.ExecuteTemplate(w, "index.gohtml", "ACME INC") +} + +func about(w http.ResponseWriter, r *http.Request) { + type customData struct { + Title string + Members []string + } + + cd := customData{ + Title: "THE TEAM", + Members: []string{"Moneypenny", "Bond", "Q", "M"}, + } + + tpl.ExecuteTemplate(w, "about.gohtml", cd) +} diff --git a/000_temp/56_SVCC-17/06b/templates/about.gohtml b/000_temp/56_SVCC-17/06b/templates/about.gohtml new file mode 100644 index 00000000..e145ec66 --- /dev/null +++ b/000_temp/56_SVCC-17/06b/templates/about.gohtml @@ -0,0 +1,7 @@ + +{{template "header" .Title}} +

all about our team

+{{range .Members}} +{{.}}
+{{end}} +{{template "footer"}} diff --git a/000_temp/56_SVCC-17/06b/templates/incl-footer.gohtml b/000_temp/56_SVCC-17/06b/templates/incl-footer.gohtml new file mode 100644 index 00000000..37bdaade --- /dev/null +++ b/000_temp/56_SVCC-17/06b/templates/incl-footer.gohtml @@ -0,0 +1,4 @@ +{{define "footer"}} + + +{{end}} \ No newline at end of file diff --git a/000_temp/56_SVCC-17/06b/templates/incl-header.gohtml b/000_temp/56_SVCC-17/06b/templates/incl-header.gohtml new file mode 100644 index 00000000..16cfc4a3 --- /dev/null +++ b/000_temp/56_SVCC-17/06b/templates/incl-header.gohtml @@ -0,0 +1,9 @@ +{{define "header"}} + + + + + {{.}} + + +{{end}} \ No newline at end of file diff --git a/000_temp/56_SVCC-17/06b/templates/index.gohtml b/000_temp/56_SVCC-17/06b/templates/index.gohtml new file mode 100644 index 00000000..dbf294c5 --- /dev/null +++ b/000_temp/56_SVCC-17/06b/templates/index.gohtml @@ -0,0 +1,5 @@ + +{{template "header" .}} +

all about acme inc

+ +{{template "footer"}} diff --git a/000_temp/56_SVCC-17/06c/assets/index.css b/000_temp/56_SVCC-17/06c/assets/index.css new file mode 100644 index 00000000..46d4f979 --- /dev/null +++ b/000_temp/56_SVCC-17/06c/assets/index.css @@ -0,0 +1,22 @@ +html, body, h1 { + padding: 0; + border: 0; + margin: 0; + box-sizing: border-box; +} + +body { + display: flex; + justify-content: center; + align-items: center; + background-image: url("http://github.com/assets/paradise.jpeg"); + background-position: center; + background-size: cover; + background-repeat: no-repeat; + height: 100vh; +} + +h1 { + font-size: 12vw; + color: white; +} \ No newline at end of file diff --git a/000_temp/56_SVCC-17/06c/assets/paradise.jpeg b/000_temp/56_SVCC-17/06c/assets/paradise.jpeg new file mode 100644 index 00000000..8e2b075a Binary files /dev/null and b/000_temp/56_SVCC-17/06c/assets/paradise.jpeg differ diff --git a/000_temp/56_SVCC-17/06c/main.go b/000_temp/56_SVCC-17/06c/main.go new file mode 100644 index 00000000..2052430b --- /dev/null +++ b/000_temp/56_SVCC-17/06c/main.go @@ -0,0 +1,37 @@ +package main + +import ( + "html/template" + "net/http" +) + +var tpl *template.Template + +func init() { + tpl = template.Must(template.ParseGlob("templates/*")) +} + +func main() { + http.HandleFunc("/", index) + http.HandleFunc("/about", about) + http.Handle("/assets/", http.StripPrefix("/assets", http.FileServer(http.Dir("./assets")))) + http.ListenAndServe(":8080", nil) +} + +func index(w http.ResponseWriter, r *http.Request) { + tpl.ExecuteTemplate(w, "index.gohtml", "ACME INC") +} + +func about(w http.ResponseWriter, r *http.Request) { + type customData struct { + Title string + Members []string + } + + cd := customData{ + Title: "ABOUT OUR TEAM", + Members: []string{"Moneypenny", "Bond", "Q", "M"}, + } + + tpl.ExecuteTemplate(w, "about.gohtml", cd) +} diff --git a/000_temp/56_SVCC-17/06c/templates/about.gohtml b/000_temp/56_SVCC-17/06c/templates/about.gohtml new file mode 100644 index 00000000..4a05bdf1 --- /dev/null +++ b/000_temp/56_SVCC-17/06c/templates/about.gohtml @@ -0,0 +1,10 @@ +{{template "header" .Title}} + + + +

ABOUT US

+{{range .Members}} +{{.}}
+{{end}} + +{{template "footer"}} \ No newline at end of file diff --git a/000_temp/56_SVCC-17/06c/templates/incl-footer.gohtml b/000_temp/56_SVCC-17/06c/templates/incl-footer.gohtml new file mode 100644 index 00000000..37bdaade --- /dev/null +++ b/000_temp/56_SVCC-17/06c/templates/incl-footer.gohtml @@ -0,0 +1,4 @@ +{{define "footer"}} + + +{{end}} \ No newline at end of file diff --git a/000_temp/56_SVCC-17/06c/templates/incl-header.gohtml b/000_temp/56_SVCC-17/06c/templates/incl-header.gohtml new file mode 100644 index 00000000..c9b0ef1e --- /dev/null +++ b/000_temp/56_SVCC-17/06c/templates/incl-header.gohtml @@ -0,0 +1,7 @@ +{{define "header"}} + + + + + {{.}} +{{end}} \ No newline at end of file diff --git a/000_temp/56_SVCC-17/06c/templates/index.gohtml b/000_temp/56_SVCC-17/06c/templates/index.gohtml new file mode 100644 index 00000000..32700220 --- /dev/null +++ b/000_temp/56_SVCC-17/06c/templates/index.gohtml @@ -0,0 +1,8 @@ +{{template "header" .}} + + + + +

PARADISE

+ +{{template "footer"}} \ No newline at end of file diff --git a/000_temp/56_SVCC-17/07-concurrency/main.go b/000_temp/56_SVCC-17/07-concurrency/main.go new file mode 100644 index 00000000..ad6b5cf4 --- /dev/null +++ b/000_temp/56_SVCC-17/07-concurrency/main.go @@ -0,0 +1,11 @@ +package main + +import "fmt" + +func main() { + c := make(chan int) + go func() { + c <- 42 + }() + fmt.Println(<-c) +} diff --git a/000_temp/57-form/01/main.go b/000_temp/57-form/01/main.go new file mode 100644 index 00000000..7b7e8f29 --- /dev/null +++ b/000_temp/57-form/01/main.go @@ -0,0 +1,15 @@ +package _1 + +import ( + "io" + "net/http" +) + +func main() { + http.HandleFunc("/", index) + http.ListenAndServe(":8080", nil) +} + +func index(w http.ResponseWriter, r *http.Request) { + io.WriteString(w, "Hello fcc") +} diff --git a/000_temp/57-form/02/main.go b/000_temp/57-form/02/main.go new file mode 100644 index 00000000..68b3556c --- /dev/null +++ b/000_temp/57-form/02/main.go @@ -0,0 +1,49 @@ +package main + +import ( + "html/template" + "net/http" +) + +var tpl *template.Template + +func init() { + tpl = template.Must(template.ParseGlob("templates/*.gohtml")) +} + +func main() { + http.HandleFunc("/", index) + http.HandleFunc("/process", processor) + http.ListenAndServe(":8080", nil) +} + +func index(w http.ResponseWriter, r *http.Request) { + tpl.ExecuteTemplate(w, "index.gohtml", nil) +} + +func processor(w http.ResponseWriter, r *http.Request) { + if r.Method != "POST" { + http.Redirect(w, r, "/", http.StatusSeeOther) + return + } + + fname := r.FormValue("firster") + lname := r.FormValue("laster") + surf := r.FormValue("surf") + snow := r.FormValue("snow") + skate := r.FormValue("skate") + radio := r.FormValue("cow") + + d := struct { + First, Last, Surf, Snow, Skate, Radio string + }{ + First: fname, + Last: lname, + Surf: surf, + Snow: snow, + Skate: skate, + Radio: radio, + } + + tpl.ExecuteTemplate(w, "processor.gohtml", d) +} diff --git a/000_temp/57-form/02/templates/index.gohtml b/000_temp/57-form/02/templates/index.gohtml new file mode 100644 index 00000000..ae69cdd6 --- /dev/null +++ b/000_temp/57-form/02/templates/index.gohtml @@ -0,0 +1,55 @@ + + + + + + INDEX + + + +

OUR FORM EXAMPLE

+ +
+
+ + +
+ + + +
+ + + +
+ + + +
+ + + +
+ + +

RADIO BUTTON

+ + + + + + + + + + + + +
+ + + + \ No newline at end of file diff --git a/000_temp/57-form/02/templates/processor.gohtml b/000_temp/57-form/02/templates/processor.gohtml new file mode 100644 index 00000000..9fdda392 --- /dev/null +++ b/000_temp/57-form/02/templates/processor.gohtml @@ -0,0 +1,20 @@ + + + + + + PROCESSOR + + + +

{{.First}} {{.Last}}

+ +

Surf {{.Surf}}

+

Snow {{.Snow}}

+

Skate {{.Skate}}

+ +

radio button {{.Radio}}

+ + + \ No newline at end of file diff --git a/000_temp/58-simple/main.go b/000_temp/58-simple/main.go new file mode 100644 index 00000000..99fd8050 --- /dev/null +++ b/000_temp/58-simple/main.go @@ -0,0 +1,7 @@ +package main + +import "fmt" + +func main() { + fmt.Println("Hello") +} diff --git a/000_temp/59-html-form/assets/form.css b/000_temp/59-html-form/assets/form.css new file mode 100644 index 00000000..65d03ff9 --- /dev/null +++ b/000_temp/59-html-form/assets/form.css @@ -0,0 +1,9 @@ +form { + display: flex; + flex-direction: column; + font-size: 6rem; +} + +input { + height: 5vh; +} \ No newline at end of file diff --git a/000_temp/59-html-form/assets/main.css b/000_temp/59-html-form/assets/main.css new file mode 100644 index 00000000..af55a75d --- /dev/null +++ b/000_temp/59-html-form/assets/main.css @@ -0,0 +1,58 @@ +html, body, main, header, div, a { + padding: 0; + border: 0; + margin: 0; + box-sizing: border-box; +} + +main { + height: 100vh; + background-image: url("http://github.com/assets/paradise.jpeg"); + background-repeat: no-repeat; + background-size: cover; + background-position: center; + display: flex; + justify-content: center; + align-items: center; + color: white; + font-size: 12rem; +} + +header { + position: fixed; + top: 0; + left: 0; + height: 4vh; + background-color: rgba(121, 200, 255, 0.39); + display: flex; + justify-content: space-between; + align-items: center; + width: 100%; + border-bottom: 1px solid gray; +} + +header > div { + height: 100%; + font-size: 1.5rem; + color: white; + display: flex; + flex-direction: row; + justify-content: center; + align-items: center; + margin: 20px; +} + +header > div > a { + padding: 20px; + text-decoration: none; + color: white; +} + +header > div > a:hover { + color: blue; +} + +footer { + font-size: 12rem; + color: black; +} \ No newline at end of file diff --git a/000_temp/59-html-form/assets/paradise.jpeg b/000_temp/59-html-form/assets/paradise.jpeg new file mode 100644 index 00000000..8e2b075a Binary files /dev/null and b/000_temp/59-html-form/assets/paradise.jpeg differ diff --git a/000_temp/59-html-form/main.go b/000_temp/59-html-form/main.go new file mode 100644 index 00000000..992111d9 --- /dev/null +++ b/000_temp/59-html-form/main.go @@ -0,0 +1,74 @@ +package main + +import ( + "html/template" + "net/http" +) + +type GData struct { + Title string +} + +var tpl *template.Template + +func init() { + tpl = template.Must(template.ParseGlob("templates/*")) +} + +func main() { + http.HandleFunc("/", index) + http.HandleFunc("/about", about) + http.HandleFunc("/contact", contact) + http.HandleFunc("/signup", signup) + http.HandleFunc("/process", process) + http.Handle("/assets/", http.StripPrefix("/assets", http.FileServer(http.Dir("./assets")))) + http.ListenAndServe(":8080", nil) +} + +func index(w http.ResponseWriter, r *http.Request) { + gd := GData{ + Title: "Acme Inc", + } + tpl.ExecuteTemplate(w, "index.gohtml", gd) +} + +func about(w http.ResponseWriter, r *http.Request) { + gd := GData{ + Title: "ABOUT", + } + tpl.ExecuteTemplate(w, "about.gohtml", gd) +} + +func contact(w http.ResponseWriter, r *http.Request) { + gd := GData{ + Title: "CONTACT", + } + tpl.ExecuteTemplate(w, "contact.gohtml", gd) +} + +func signup(w http.ResponseWriter, r *http.Request) { + gd := GData{ + Title: "SIGNUP", + } + tpl.ExecuteTemplate(w, "signup.gohtml", gd) +} + +func process(w http.ResponseWriter, r *http.Request) { + + fn := r.FormValue("first") + ln := r.FormValue("last") + + d := struct { + GData + First string + Last string + }{ + GData: GData{ + Title: "PROCESS", + }, + First: fn, + Last: ln, + } + + tpl.ExecuteTemplate(w, "process.gohtml", d) +} diff --git a/000_temp/59-html-form/templates/about.gohtml b/000_temp/59-html-form/templates/about.gohtml new file mode 100644 index 00000000..1c6a92c4 --- /dev/null +++ b/000_temp/59-html-form/templates/about.gohtml @@ -0,0 +1,20 @@ + + + + + + {{.Title}} + + + + +{{template "header"}} + +
+

{{.Title}}

+
+ + + + \ No newline at end of file diff --git a/000_temp/59-html-form/templates/contact.gohtml b/000_temp/59-html-form/templates/contact.gohtml new file mode 100644 index 00000000..94e82bcd --- /dev/null +++ b/000_temp/59-html-form/templates/contact.gohtml @@ -0,0 +1,21 @@ + + + + + + {{.Title}} + + + + +{{template "header"}} + +
+

{{.Title}}

+
+ + + + + \ No newline at end of file diff --git a/000_temp/59-html-form/templates/incl-header.gohtml b/000_temp/59-html-form/templates/incl-header.gohtml new file mode 100644 index 00000000..00c340cf --- /dev/null +++ b/000_temp/59-html-form/templates/incl-header.gohtml @@ -0,0 +1,11 @@ +{{define "header"}} +
+
Company Logo
+
+ home
+ about
+ contact
+ signup
+
+
+{{end}} \ No newline at end of file diff --git a/000_temp/59-html-form/templates/incl-links.gohtml b/000_temp/59-html-form/templates/incl-links.gohtml new file mode 100644 index 00000000..fcf7cd42 --- /dev/null +++ b/000_temp/59-html-form/templates/incl-links.gohtml @@ -0,0 +1,3 @@ +{{define "links"}} + +{{end}} \ No newline at end of file diff --git a/000_temp/59-html-form/templates/index.gohtml b/000_temp/59-html-form/templates/index.gohtml new file mode 100644 index 00000000..94e82bcd --- /dev/null +++ b/000_temp/59-html-form/templates/index.gohtml @@ -0,0 +1,21 @@ + + + + + + {{.Title}} + + + + +{{template "header"}} + +
+

{{.Title}}

+
+ + + + + \ No newline at end of file diff --git a/000_temp/59-html-form/templates/process.gohtml b/000_temp/59-html-form/templates/process.gohtml new file mode 100644 index 00000000..b315f758 --- /dev/null +++ b/000_temp/59-html-form/templates/process.gohtml @@ -0,0 +1,25 @@ + + + + + + {{.Title}} + + + + +{{template "header"}} + +
+

{{.Title}}

+
+ + + + + + \ No newline at end of file diff --git a/000_temp/59-html-form/templates/signup.gohtml b/000_temp/59-html-form/templates/signup.gohtml new file mode 100644 index 00000000..e1b7145b --- /dev/null +++ b/000_temp/59-html-form/templates/signup.gohtml @@ -0,0 +1,38 @@ + + + + + + {{.Title}} + + + + + +{{template "header"}} + +
+

{{.Title}}

+
+ +
+ + + + + + + + + +
+ + + + + + + \ No newline at end of file diff --git a/000_temp/60-redirect/main.go b/000_temp/60-redirect/main.go new file mode 100644 index 00000000..57fad399 --- /dev/null +++ b/000_temp/60-redirect/main.go @@ -0,0 +1,21 @@ +package main + +import ( + "io" + "net/http" +) + +func main() { + http.HandleFunc("/", index) + http.HandleFunc("/new", newplace) + http.ListenAndServe(":8080", nil) +} + +func index(w http.ResponseWriter, r *http.Request) { + http.Redirect(w, r, "/new", http.StatusSeeOther) + io.WriteString(w, "You are at index") +} + +func newplace(w http.ResponseWriter, r *http.Request) { + io.WriteString(w, "You are at newplace") +} diff --git a/000_temp/61-assertion/main.go b/000_temp/61-assertion/main.go new file mode 100644 index 00000000..9fd92895 --- /dev/null +++ b/000_temp/61-assertion/main.go @@ -0,0 +1,16 @@ +package main + +import "fmt" + +func main() { + var s interface{} + s = struct { + name string + }{ + name: "james bond", + } + fmt.Printf("%T\n", s) + s = "James Bond" + fmt.Printf("%T\n", s) + fmt.Println(s) +} diff --git a/000_temp/62-kelowna/00-prep/01/main.go b/000_temp/62-kelowna/00-prep/01/main.go new file mode 100644 index 00000000..f98e0ff9 --- /dev/null +++ b/000_temp/62-kelowna/00-prep/01/main.go @@ -0,0 +1,57 @@ +package main + +import "fmt" + +type person struct { + first string + last string +} + +type secretAgent struct { + person + ltk bool +} + +func (p person) speak() { + fmt.Println("hello from", p.first) +} + +func (sa secretAgent) speak() { + fmt.Println("hello from", sa.first) +} + +type human interface { + speak() +} + +func foo(h human) { + h.speak() +} + +func bar(h human) { + h.speak() +} + +func main() { + x := person{ + first: "miss", + last: "money", + } + + y := secretAgent{ + person: person{ + first: "james", + last: "bond", + }, + ltk: true, + } + + x.speak() + y.speak() + + foo(x) + foo(y) + + fmt.Println("Hello", x) + fmt.Println("Hello", y) +} diff --git a/000_temp/62-kelowna/00-prep/02/main.go b/000_temp/62-kelowna/00-prep/02/main.go new file mode 100644 index 00000000..96ff7e59 --- /dev/null +++ b/000_temp/62-kelowna/00-prep/02/main.go @@ -0,0 +1,20 @@ +package main + +import "fmt" + +func main() { + xi := []int{4, 5, 6, 7} + + for n, i := range xi { + fmt.Println(n, i) + } + + m := map[string]int{ + "mcleod": 47, + "bond": 27, + } + + for k, v := range m { + fmt.Println(k, v) + } +} diff --git a/000_temp/62-kelowna/01-present/01/main.go b/000_temp/62-kelowna/01-present/01/main.go new file mode 100644 index 00000000..f7689e16 --- /dev/null +++ b/000_temp/62-kelowna/01-present/01/main.go @@ -0,0 +1,27 @@ +package main + +import ( + "html/template" + "net/http" +) + +var tpl *template.Template + +func init() { + tpl = template.Must(template.ParseGlob("templates/*.gohtml")) +} + +func main() { + http.HandleFunc("/", index) + http.HandleFunc("/about", about) + http.ListenAndServe(":8080", nil) +} + +func index(w http.ResponseWriter, r *http.Request) { + tpl.ExecuteTemplate(w, "index.gohtml", 42) + //io.WriteString(w, "Hello Kelowna") +} + +func about(w http.ResponseWriter, r *http.Request) { + tpl.ExecuteTemplate(w, "about.gohtml", "JAMES BOND") +} diff --git a/000_temp/62-kelowna/01-present/01/templates/about.gohtml b/000_temp/62-kelowna/01-present/01/templates/about.gohtml new file mode 100644 index 00000000..47cd1272 --- /dev/null +++ b/000_temp/62-kelowna/01-present/01/templates/about.gohtml @@ -0,0 +1,14 @@ + + + + + ABOUT + + + +

This is the about page

+ +{{.}} + + + \ No newline at end of file diff --git a/000_temp/62-kelowna/01-present/01/templates/index.gohtml b/000_temp/62-kelowna/01-present/01/templates/index.gohtml new file mode 100644 index 00000000..0966d1d4 --- /dev/null +++ b/000_temp/62-kelowna/01-present/01/templates/index.gohtml @@ -0,0 +1,14 @@ + + + + + INDEX + + + +

Hello, Kelowna

+ +{{.}} + + + \ No newline at end of file diff --git a/000_temp/62-kelowna/01-present/02/main.go b/000_temp/62-kelowna/01-present/02/main.go new file mode 100644 index 00000000..96ff7e59 --- /dev/null +++ b/000_temp/62-kelowna/01-present/02/main.go @@ -0,0 +1,20 @@ +package main + +import "fmt" + +func main() { + xi := []int{4, 5, 6, 7} + + for n, i := range xi { + fmt.Println(n, i) + } + + m := map[string]int{ + "mcleod": 47, + "bond": 27, + } + + for k, v := range m { + fmt.Println(k, v) + } +} diff --git a/000_temp/62-kelowna/01-present/03/main.go b/000_temp/62-kelowna/01-present/03/main.go new file mode 100644 index 00000000..39635b81 --- /dev/null +++ b/000_temp/62-kelowna/01-present/03/main.go @@ -0,0 +1,59 @@ +package main + +import "fmt" + +// https://play.golang.org/p/gLdpEzVm54U + +type person struct { + first string + last string +} + +type secretAgent struct { + person + ltk bool +} + +// func (r receiver) identifier(parameters) (returns) {code} + +func (p person) speak() { + fmt.Println(p.first, `says, "Why, James. Helllllo."`) +} + +func (sa secretAgent) speak() { + fmt.Println(sa.first, `says, "Why Miss Moneypenny. So good to see you. One martini please. Shaken, not stirred."`) +} + +type human interface { + speak() +} + +func foo(h human) { + h.speak() +} + +func main() { + p1 := person{ + first: "Missy", + last: "Moneypenny", + } + + p2 := secretAgent{ + person: person{ + first: "James", + last: "Bond", + }, + ltk: true, + } + + fmt.Println(p1) + + fmt.Println(p2) + + p1.speak() + + p2.speak() + + foo(p1) + foo(p2) +} diff --git a/000_temp/62-kelowna/02/temp.txt b/000_temp/62-kelowna/02/temp.txt new file mode 100644 index 00000000..20c8f38e --- /dev/null +++ b/000_temp/62-kelowna/02/temp.txt @@ -0,0 +1,55 @@ +INSERT INTO customers VALUES (12, 'Jamesx', 8000, 'CA'), (13, 'Jennyx', 11000, 'WA'), (14, 'Toddx', 9950, 'OR'), (15, 'Rickx', 10500, 'WA'), (16, 'Stevex', 10250, 'CA'), (17, 'Andrewx', 7750, 'OR'), (18, 'Josephinex', 13000, 'WA'), (19, 'Maxx', 12000, 'CA'), (20, 'Stephaniex', 8000, 'OR'), (21, 'Agnesx', 9999, 'WA'); + + + +INSERT INTO ingredients VALUES (1, 'Kale', '1 cup, pulled from stem'), (2, 'blueberries', 'a handful'), (3, 'pineapple', 'a handful'), (4, 'ice', 'a handful'), (5, 'juice', '1 cup'), (6, 'water', 'as much as needed'), (7, 'peanut butter', '3/4 cup'), (8, 'old-fashioned rolled oats', '1.5 cups'), (9, 'all-purpose flour', '2 cups'), (10, 'baking powder', '1 teaspoon'), (11, 'baking soda', '1 teaspon'), (12, 'salt', '1 teaspoon'), (13, 'butter', '2 sticks, softended'), (14, 'granulated sugar', '1 cup'), (15, 'brown sugar', '1 cup firmly packed'), (16, 'vanilla extract', '1 tablespoon'), (17, 'eggs', '2 large'), (18, 'semisweet chocolate chips', '12 ounces'), (19, 'semisweet chocolate, grated', '8 ounces'); + +INSERT INTO recipeingredients VALUES (1, 2, 1), (2, 2, 2), (3, 2, 3), (4, 2, 4), (5, 2, 5), (6, 2, 6), (7, 1, 7), (8,1,8), (9,1,9), (10,1,10), (11,1,11), (12,1,12), (13,1,13), (14,1,14), (15,1,15), (16,1,16), (17,1,17), (18,1,18), (19,1,19); + +SELECT rname, iname, iamount FROM recipes JOIN recipeingredients ON recipes.rid = recipeingredients.rid JOIN ingredients ON recipeingredients.iid = ingredients.iid; + + +CREATE DATABASE climbing; + +\c climbing + +CREATE TABLE categories (catID INT PRIMARY KEY NOT NULL, catName TEXT NOT NULL); + +INSERT INTO categories VALUES (1, 'easy'), (2, 'medium'), (3, 'hard'), (4, 'impossible'); + +CREATE TABLE climbs (cID INT PRIMARY KEY NOT NULL, cName TEXT NOT NULL, catID INT REFERENCES categories(catID)); + +INSERT INTO climbs VALUES (1, 'metal mark', 1), (2, 'tollhouse slab', 1), (3, 'pinnacles', 2), (4, 'Arroyo', 3), (5, 'El Cap', 3), (6, 'North Face', 4); + +SELECT cname, catname FROM climbs INNER JOIN categories ON climbs.catid = categories.catid; + + +SELECT c.cname, m.mname FROM customers AS c JOIN rentals AS r ON c.cid = r.cid JOIN movies AS m ON m.mid =r.mid; + + +CREATE TABLE actors(aid INT PRIMARY KEY NOT NULL, aname TEXT NOT NULL); + +INSERT INTO actors VALUES (1, 'James'), (2, 'Jenny'), (3, 'Rock'), (4, 'Rick'), (5, 'Steve'), (6, 'Andrew'); + +CREATE TABLE moviecast (caid INT PRIMARY KEY NOT NULL, mid INT REFERENCES movies(MID), aid INT REFERENCES actors(AID)); + +INSERT INTO moviecast VALUES (1, 2, 1), (2, 2, 2), (3, 2, 3), (4, 2, 4), (5, 2, 5), (6, 2, 6), (7, 1, 3), (8,1,2), (9,1,1), (10,1,5), (11,2,5), (12,2,6), (13,3,1), (14,3,4), (15,4,5), (16,4,2), (17,5,1), (18,6,3), (19,6,4); + + +SELECT cname, rid, mname, aname FROM customers AS c JOIN rentals as r ON c.cid = r.cid JOIN movies AS m ON r.mid = m.mid JOIN moviecast AS mc ON m.mid = mc.mid JOIN actors AS a ON mc.aid = a.aid; + +CREATE TABLE cphone (cpID INT PRIMARY KEY NOT NULL, cID INT REFERENCES customers(CID), cpPHONE TEXT NOT NULL); + +INSERT INTO cphone VALUES (1, 1, '007-0077'), (2, 2, '008-0088'), (3, 3, '009-0099'), (4, 4, '001,0011'), (5, 5, '002-0022'), (6, 6, '003-0033'), (7, 1, '777-8888'), (8, 2, '888-7777'), (9, 2, '999-3231'); + +CREATE TABLE aphone (apID INT PRIMARY KEY NOT NULL, aID INT REFERENCES actors(AID), apPHONE TEXT NOT NULL); + +INSERT INTO aphone VALUES (1, 1, '997-9977'), (2, 2, '998-9988'), (3, 3, '999-9999'), (4, 4, '991,9911'), (5, 5, '992-9922'), (6, 6, '993-9933'), (7, 1, '888-8888'), (8, 2, '888-8887'), (9, 2, '444-3231'); + +SELECT cname, cpphone FROM cphone AS cp JOIN customers AS c ON cp.cid = c.cid; + +SELECT aname, apphone FROM actors AS a JOIN aphone AS ap ON a.aid = ap.aid; + +SELECT cname, cpphone, aname, apphone FROM cphone AS cp JOIN customers AS c ON cp.cid = c.cid JOIN rentals AS r ON c.cid = r.cid JOIN movies AS m ON r.mid = m.mid JOIN moviecast AS mc ON m.mid = mc.mid JOIN actors AS a ON mc.aid = a.aid JOIN aphone AS ap ON a.aid = ap.aid; + +SELECT cname, cpphone, rid, mname, aname, apphone FROM cphone AS cp JOIN customers AS c ON cp.cid = c.cid JOIN rentals AS r ON c.cid = r.cid JOIN movies AS m ON r.mid = m.mid JOIN moviecast AS mc ON m.mid = mc.mid JOIN actors AS a ON mc.aid = a.aid JOIN aphone AS ap ON a.aid = ap.aid; \ No newline at end of file diff --git a/000_temp/62-kelowna/03/sql b/000_temp/62-kelowna/03/sql new file mode 100644 index 00000000..efde806d --- /dev/null +++ b/000_temp/62-kelowna/03/sql @@ -0,0 +1,80 @@ +INSERT INTO recipeclasses VALUES (1, 'italian'), (2,'french'), (3,'indian'), (4,'mexican'), (5,'thai'), (6,'chinese'), (7,'swiss'), (8,'german'), (9,'american'), (10,'japanese'), (11,'hungarian'), (12,'english'); + +INSERT INTO recipes VALUES (1,'hamburger',9,'bbq','include pickles'), (2,'bratwurst',8,'grill','drink beer while eating'), (3,'chow meain',6,'wok it','not so good as leftovers'), (4,'canoli',1,'lots of cream','wonderful dessert'), (5,'chai',3,'milk and spices baby','delicious to drink'), (6,'red wine',2,'enjoy za life','not so much for me'), (7,'burrito',4,'beans please','add hot sauce'), (8,'pad thai',5,'no fish sauce','put a fried egg on top'), (9,'swiss cheese',7,'put in the holes','melt it on crackers'), (10,'sushi',10,'do not cook the fish','add miso soup on the side'); + +SELECT recipeclassdescription, recipetitle FROM recipeclasses LEFT OUTER JOIN recipes ON recipeclasses.recipeclassid = recipes.recipeclassid; + +SELECT recipeclassdescription, recipetitle FROM recipeclasses LEFT JOIN recipes ON recipeclasses.recipeclassid = recipes.recipeclassid; + +SELECT rc.recipeclassdescription, r.recipetitle FROM recipeclasses AS rc LEFT JOIN recipes AS r ON rc.recipeclassid = r.recipeclassid; + +SELECT rc.recipeclassdescription FROM recipeclasses AS rc LEFT JOIN recipes AS r ON rc.recipeclassid = r.recipeclassid WHERE r.recipeid IS NULL; + +SELECT f.recipeclassdescription, r.recipetitle FROM (SELECT recipeclassid, recipeclassdescription FROM recipeclasses AS rc where rc.recipeclassdescription = 'german' OR rc.recipeclassdescription = 'italian' OR rc.recipeclassdescription = 'french') AS f LEFT JOIN recipes AS r ON f.recipeclassid = r.recipeclassid; + +SELECT f.recipetitle, rc.recipeclassdescription FROM (SELECT recipetitle, recipeclassid FROM recipes WHERE recipetitle LIKE '%a%') AS f RIGHT JOIN recipeclasses AS rc ON f.recipeclassid = rc.recipeclassid; + + + +CREATE DATABASE humanity; + +\c humanity + +CREATE TABLE humans (hid INT PRIMARY KEY NOT NULL, hname TEXT, hage INT); + +INSERT INTO humans VALUES (1,'',18), (2,'',22), (3,'jenny',27), (4,'james',32), (5,'ian',36), (6,'stephen',42), (7,'stephanie',47), (8,'agnes',49), (9,'portia',50), (10,'fidel',55), (11,'rich',62), (12,'al',78); + +SELECT f.hname FROM (SELECT hname FROM humans WHERE hname LIKE '%a%') AS f; + +SELECT f.hname, f.hage FROM (SELECT hname, hage FROM humans WHERE hage > 40) AS f; + + +SELECT f1.recipeclassdescription, f2.recipetitle FROM (SELECT recipeclasses.recipeclassid, recipeclassdescription FROM recipeclasses WHERE recipeclassdescription = 'german' OR recipeclassdescription = 'italian' OR recipeclassdescription = 'french' OR recipeclassdescription = 'hungarian') AS f1 LEFT JOIN (SELECT recipes.recipeclassid, recipetitle FROM recipes WHERE recipetitle LIKE '%o%') AS f2 on f1.recipeclassid = f2.recipeclassid; + +CREATE TABLE ingredients (ingredientid INT PRIMARY KEY NOT NULL, ingredientname TEXT); + +CREATE TABLE measurements (measurementsid INT PRIMARY KEY NOT NULL, measurementdescription TEXT); + +CREATE TABLE recipeingredients (riid INT PRIMARY KEY NOT NULL, recipeid INT NOT NULL REFERENCES recipes(recipeid), recipeseqno INT, ingredientid INT NOT NULL REFERENCES ingredients(ingredientid), measurementsid INT NOT NULL REFERENCES measurements(measurementsid)); + +INSERT INTO ingredients VALUES (1,'chocolate'), (2,'chipotle'), (3,'chicken'), (4,'beef'), (5,'flower'), (6,'butter'), (7,'milk'), (8,'broth'), (9,'salt'), (10,'baking soda'); + +INSERT INTO measurements VALUES (1,'some'), (2,'a little'), (3,'a lot'), (4,'a pinch'), (5,'until it tastes good'), (6,'not too much'), (7,'not too little'), (8,'just a pinch'), (9,'a smidge'), (10,'a generous scoop'), (11,'more than you think'), (12,'less than youd imagine'); + +INSERT INTO recipeingredients VALUES (1,6,1,7,10),(2,3,2,5,5),(3,8,3,9,7),(4,7,4,6,1),(5,4,5,3,2),(6,9,6,1,6),(7,3,7,6,9),(8,1,8,6,11),(9,1,9,8,6),(10,2,10,7,10),(11,5,11,7,6),(12,6,12,4,1),(13,7,13,8,2),(14,1,14,8,7),(15,2,15,1,3),(16,1,16,8,1),(17,2,17,4,5),(18,8,18,1,6),(19,5,19,8,3),(20,4,20,8,2),(21,9,21,7,6),(22,4,22,2,1),(23,9,23,9,1),(24,3,24,2,3),(25,9,25,6,2),(26,1,26,2,10),(27,8,27,9,6),(28,1,28,8,5),(29,3,29,8,5),(30,6,30,9,10),(31,3,31,5,10),(32,2,32,1,11),(33,4,33,1,9),(34,3,34,4,4),(35,4,35,9,8),(36,3,36,2,5),(37,4,37,2,10),(38,2,38,5,4),(39,1,39,5,6),(40,5,40,4,10),(41,3,41,2,8),(42,4,42,5,9),(43,6,43,5,2),(44,1,44,1,3),(45,8,45,9,11),(46,8,46,9,5),(47,1,47,7,3),(48,9,48,5,2),(49,9,49,6,5),(50,3,50,9,8),(51,5,51,8,5),(52,2,52,3,2),(53,4,53,4,8),(54,3,54,5,4),(55,5,55,7,1),(56,1,56,7,3),(57,7,57,8,3),(58,1,58,9,10),(59,9,59,5,11),(60,1,60,4,3),(61,2,61,1,7),(62,4,62,6,10),(63,4,63,4,9),(64,8,64,6,6),(65,6,65,7,6),(66,2,66,9,11),(67,6,67,3,11),(68,4,68,6,6),(69,6,69,3,6),(70,1,70,8,3),(71,2,71,7,4),(72,4,72,2,8),(73,8,73,3,5),(74,4,74,4,7),(75,6,75,8,9),(76,7,76,2,1),(77,8,77,3,2),(78,6,78,8,9),(79,3,79,5,10),(80,3,80,4,9),(81,7,81,7,4),(82,6,82,7,6),(83,3,83,2,10),(84,7,84,2,7),(85,9,85,2,10),(86,3,86,1,5),(87,8,87,4,2),(88,5,88,3,5),(89,6,89,4,9),(90,5,90,6,4),(91,9,91,2,9),(92,7,92,4,1),(93,7,93,4,2),(94,6,94,4,8),(95,4,95,2,5),(96,2,96,3,8),(97,5,97,3,9),(98,4,98,9,2),(99,7,99,5,2),(100,4,100,7,10); + + +SELECT recipetitle, recipeclassdescription, recipeseqno, ingredientname, measurementdescription FROM recipes AS r INNER JOIN recipeclasses AS rc ON r.recipeclassid = rc.recipeclassid INNER JOIN recipeingredients AS ri ON r.recipeid = ri.recipeid INNER JOIN ingredients AS i ON ri.ingredientid = i.ingredientid INNER JOIN measurements AS m ON ri.measurementsid = m.measurementsid ORDER BY r.recipeid, ri.recipeseqno; + +INSERT INTO recipeclasses VALUES (13, 'cambodian'), (14, 'nepalese'), (15, 'tazmanian'); + +SELECT recipeclassdescription, recipetitle FROM (recipeclasses AS rc LEFT JOIN recipes AS r ON r.recipeclassid = rc.recipeclassid); + + +--- does not work --- +SELECT recipeclassdescription, recipetitle, recipeseqno, ingredientname, measurementdescription FROM (((recipeclasses AS rc LEFT JOIN recipes AS r ON r.recipeclassid = rc.recipeclassid) INNER JOIN recipeingredients AS ri ON r.recipeid = ri.recipeid) INNER JOIN ingredients AS i ON ri.ingredientid = i.ingredientid) INNER JOIN measurements AS m ON ri.measurementsid = m.measurementsid; + +--- does work --- +SELECT recipeclassdescription, recipetitle, recipeseqno, ingredientname, measurementdescription FROM recipes AS r INNER JOIN recipeingredients AS ri ON r.recipeid = ri.recipeid INNER JOIN ingredients AS i ON ri.ingredientid = i.ingredientid INNER JOIN measurements AS m ON ri.measurementsid = m.measurementsid RIGHT JOIN recipeclasses AS rc ON r.recipeclassid = rc.recipeclassid; + +SELECT recipeclassdescription, recipetitle, recipeseqno, ingredientname, measurementdescription FROM recipes AS r INNER JOIN recipeingredients AS ri ON r.recipeid = ri.recipeid INNER JOIN ingredients AS i ON ri.ingredientid = i.ingredientid INNER JOIN measurements AS m ON ri.measurementsid = m.measurementsid RIGHT JOIN recipeclasses AS rc ON r.recipeclassid = rc.recipeclassid ORDER BY r.recipeid, ri.recipeseqno; + +SELECT recipeclassdescription, recipetitle, recipeseqno, ingredientname, measurementdescription FROM (((recipeclasses AS rc LEFT JOIN recipes AS r ON r.recipeclassid = rc.recipeclassid) LEFT JOIN recipeingredients AS ri ON r.recipeid = ri.recipeid) LEFT JOIN ingredients AS i ON ri.ingredientid = i.ingredientid) LEFT JOIN measurements AS m ON ri.measurementsid = m.measurementsid; + +SELECT recipeclassdescription, recipetitle, recipeseqno, ingredientname, measurementdescription FROM (((recipeclasses AS rc LEFT JOIN recipes AS r ON r.recipeclassid = rc.recipeclassid) LEFT JOIN recipeingredients AS ri ON r.recipeid = ri.recipeid) LEFT JOIN ingredients AS i ON ri.ingredientid = i.ingredientid) LEFT JOIN measurements AS m ON ri.measurementsid = m.measurementsid ORDER BY r.recipeid, ri.recipeseqno; + + +INSERT INTO recipes VALUES (11, 'reuben sandwich', NULL ,'put pastrami on rye and grill', 'butter your bread'), (12, 'thai chicken soup', NULL, 'use lemongrass', 'so delicious'); + + +SELECT rc.recipeclassdescription, r.recipetitle, ri.recipeseqno, i.ingredientname, m.measurementdescription FROM ((((recipeclasses AS rc JOIN recipes AS r ON rc.recipeclassid = r.recipeclassid) JOIN recipeingredients AS ri ON r.recipeid = ri.recipeid) JOIN ingredients AS i ON ri.ingredientid = i.ingredientid) JOIN measurements AS m ON ri.measurementsid = m.measurementsid); + +SELECT rc.recipeclassdescription, r.recipetitle, ri.recipeseqno, i.ingredientname, m.measurementdescription FROM ((((recipeclasses AS rc JOIN recipes AS r ON rc.recipeclassid = r.recipeclassid) JOIN recipeingredients AS ri ON r.recipeid = ri.recipeid) JOIN ingredients AS i ON ri.ingredientid = i.ingredientid) JOIN measurements AS m ON ri.measurementsid = m.measurementsid) ORDER BY r.recipetitle, ri.recipeseqno; + +SELECT rc.recipeclassdescription, r.recipetitle, ri.recipeseqno, i.ingredientname, m.measurementdescription FROM ((((recipeclasses AS rc LEFT JOIN recipes AS r ON rc.recipeclassid = r.recipeclassid) LEFT JOIN recipeingredients AS ri ON r.recipeid = ri.recipeid) LEFT JOIN ingredients AS i ON ri.ingredientid = i.ingredientid) LEFT JOIN measurements AS m ON ri.measurementsid = m.measurementsid); + +SELECT rc.recipeclassdescription, r.recipetitle, ri.recipeseqno, i.ingredientname, m.measurementdescription FROM ((((recipeclasses AS rc JOIN recipes AS r ON rc.recipeclassid = r.recipeclassid) JOIN recipeingredients AS ri ON r.recipeid = ri.recipeid) JOIN ingredients AS i ON ri.ingredientid = i.ingredientid) RIGHT JOIN measurements AS m ON ri.measurementsid = m.measurementsid); + +SELECT rc.recipeclassdescription, r.recipetitle, ri.recipeseqno, i.ingredientname, m.measurementdescription FROM ((((recipeclasses AS rc JOIN recipes AS r ON rc.recipeclassid = r.recipeclassid) JOIN recipeingredients AS ri ON r.recipeid = ri.recipeid) RIGHT JOIN ingredients AS i ON ri.ingredientid = i.ingredientid) LEFT JOIN measurements AS m ON ri.measurementsid = m.measurementsid); + +SELECT rc.recipeclassdescription, r.recipetitle, ri.recipeseqno, i.ingredientname, m.measurementdescription FROM ((((recipeclasses AS rc JOIN recipes AS r ON rc.recipeclassid = r.recipeclassid) JOIN recipeingredients AS ri ON r.recipeid = ri.recipeid) RIGHT JOIN ingredients AS i ON ri.ingredientid = i.ingredientid) FULL JOIN measurements AS m ON ri.measurementsid = m.measurementsid); \ No newline at end of file diff --git a/000_temp/62-kelowna/04-sql-union/union.sql b/000_temp/62-kelowna/04-sql-union/union.sql new file mode 100644 index 00000000..c58cf47e --- /dev/null +++ b/000_temp/62-kelowna/04-sql-union/union.sql @@ -0,0 +1,31 @@ +CREATE DATABASE exampleunion; + +\c exampleunion + +CREATE TABLE customers (cid INT PRIMARY KEY NOT NULL, first TEXT); + +INSERT INTO customers VALUES (1, 'James'), (2, 'Sergey'), (3, 'Vladimir'), (4, 'Putin'), (5, 'Coup'); + +CREATE TABLE vendors (vid INT PRIMARY KEY NOT NULL, first TEXT); + +INSERT INTO vendors VALUES (1, 'Stacey'), (2, 'Shelley'), (3, 'Sherry'), (4, 'Suzy'), (5, 'Sandy'); + +CREATE TABLE employees (eid INT PRIMARY KEY NOT NULL, first TEXT); + +INSERT INTO employees VALUES (1, 'Jeff'), (2, 'John'), (3, 'Jerry'), (4, 'Jose'), (5, 'Juan'); + +SELECT v.first FROM vendors AS v UNION SELECT e.first FROM employees AS e; + +SELECT 'customer' AS category, c.first FROM customers AS c UNION SELECT 'employee' AS category, e.first FROM employees AS e UNION SELECT 'vendor' AS category, v.first FROM vendors AS v UNION SELECT 'stuffedanimal' AS category, sa.saAnimalName FROM stuffedanimals AS sa; + +SELECT 'vendor' AS rowid, v.first FROM vendors AS v UNION SELECT 'employee' AS rowid, e.first FROM employees AS e; + +SELECT 'vendor' AS rowid, v.first FROM vendors AS v UNION SELECT 'employee' AS rowid, e.first FROM employees AS e UNION SELECT 'customer' AS rowid, c.first FROM customers AS c; + +SELECT 'vendor' AS rowid, v.first FROM vendors AS v UNION SELECT 'employee' AS rowid, e.first FROM employees AS e UNION SELECT 'customer' AS rowid, c.first FROM customers AS c ORDER BY rowid; + +SELECT 'vendor' AS rowid, v.first FROM vendors AS v UNION SELECT 'employee' AS rowid, e.first FROM employees AS e UNION SELECT 'customer' AS rowid, c.first FROM customers AS c ORDER BY first; + +SELECT 'vendor' AS rowid, v.first FROM vendors AS v UNION SELECT 'employee' AS rowid, e.first FROM employees AS e UNION SELECT 'customer' AS rowid, c.first FROM customers AS c ORDER BY rowid, first; + +SELECT 'vendor' AS rowid, v.first AS name FROM vendors AS v UNION SELECT 'employee' AS rowid, e.first AS name FROM employees AS e UNION SELECT 'customer' AS rowid, c.first AS name FROM customers AS c ORDER BY rowid, name; \ No newline at end of file diff --git a/000_temp/62-kelowna/05-subqueries/subqueries.sql b/000_temp/62-kelowna/05-subqueries/subqueries.sql new file mode 100644 index 00000000..fda030de --- /dev/null +++ b/000_temp/62-kelowna/05-subqueries/subqueries.sql @@ -0,0 +1,79 @@ +CREATE DATABASE acmesales; + +\c acmesales + +CREATE TABLE customers (cid INT PRIMARY KEY NOT NULL, cfirst TEXT); + +INSERT INTO customers VALUES (1,'Cory'), (2,'Casey'), (3,'Canard'), (4,'Cully'), (5,'Coffer'), (6,'Homey'); + +CREATE TABLE items (iid INT PRIMARY KEY NOT NULL, iitem TEXT); + +INSERT INTO items VALUES (1,'dog food'), (2,'tiramisu'), (3,'vino'), (4,'cheese'), (5,'chocolate'), (6,'gummy worms'), (7,'mango'); + +CREATE TABLE orders (oid INT PRIMARY KEY NOT NULL, cid INT REFERENCES customers(cid), odate TEXT); + +INSERT INTO orders VALUES (1,2,'tuesday'), (2,5,'wednesday'), (3,1,'thursday'), (4,3,'sunday'), (5,4,'saturday'), (6,5,'friday'); + +CREATE TABLE orderitems (oiid INT PRIMARY KEY NOT NULL, oid INT REFERENCES orders(oid), iid INT REFERENCES items(iid)); + +INSERT INTO orderitems VALUES (1,4,6), (2,4,5), (3,5,4), (4,6,3), (5,1,2), (6,2,1), (7,3,6), (8,4,5), (9,5,4), (10,6,3); + +SELECT c.cfirst, o.oid, o.odate, i.iitem FROM customers AS c JOIN orders AS o ON c.cid = o.cid JOIN orderitems AS oi ON o.oid = oi.oid JOIN items AS i ON oi.iid = i.iid; + +SELECT c.cfirst, o.oid, o.odate, i.iitem FROM customers AS c FULL JOIN orders AS o ON c.cid = o.cid FULL JOIN orderitems AS oi ON o.oid = oi.oid FULL JOIN items AS i ON oi.iid = i.iid; + +// SCALAR +// an expression that evaluates to one value +// aka, one column & one row = one field value + +SELECT o.oid, o.odate, (SELECT c.cfirst FROM customers AS c WHERE c.cid = o.cid) FROM orders AS o; + +// could have also done the above this way + +SELECT o.oid, o.odate, c.cfirst FROM customers AS c JOIN orders AS o ON c.cid = o.cid; + +// aggregate function COUNT + +SELECT COUNT(cfirst) AS ourcustomers FROM customers; + +SELECT c.cfirst, (SELECT COUNT(*) FROM orders AS o WHERE c.cid = o.cid) AS ordercount FROM customers AS c; + +// aggregate function MAX + +SELECT MAX(cid) AS highestcid FROM customers; + +SELECT MAX(odate) AS maxday FROM orders; + +SELECT c.cfirst, (SELECT MAX(o.oid) FROM orders AS o WHERE c.cid = o.cid) AS maxoid FROM customers AS c; + +// subquery as filters +// subquery with the WHERE clause +SELECT o.oid, o.odate, c.cfirst FROM customers AS c JOIN orders AS o ON c.cid = o.cid WHERE o.odate = (SELECT MAX(odate) FROM orders); + +// subquery as filters +// subquery with the IN keyword +SELECT o.oid, o.odate, c.cfirst FROM customers AS c JOIN orders AS o ON c.cid = o.cid WHERE o.odate IN (SELECT odate FROM orders WHERE odate LIKE '%u%'); + +// ALL, SOME, ANY keywords +SELECT o.oid, o.odate, c.cfirst FROM customers AS c JOIN orders AS o ON c.cid = o.cid WHERE o.odate = ANY (SELECT odate FROM orders WHERE odate LIKE '%u%'); + +SELECT o.oid, o.odate, c.cfirst FROM customers AS c JOIN orders AS o ON c.cid = o.cid WHERE o.odate = SOME (SELECT odate FROM orders WHERE odate LIKE '%u%'); + +SELECT o.oid, o.odate, c.cfirst FROM customers AS c JOIN orders AS o ON c.cid = o.cid WHERE o.odate = ALL (SELECT odate FROM orders WHERE odate LIKE '%u%'); + +SELECT o.oid, o.odate, c.cfirst FROM customers AS c JOIN orders AS o ON c.cid = o.cid WHERE o.odate = ALL (SELECT odate FROM orders WHERE odate LIKE 'thur%'); + +SELECT o.oid, o.odate, c.cfirst FROM customers AS c JOIN orders AS o ON c.cid = o.cid WHERE o.odate > ALL (SELECT odate FROM orders WHERE odate LIKE 'thur%'); + +// EXISTS +// I DO NOT KNOW HOW THIS WORKS +// MORE RESEARCH AND EXPERIMENTATION ARE NEEDED +SELECT c.cfirst, o.oid, o.odate, i.iitem FROM customers AS c JOIN orders AS o ON c.cid = o.cid JOIN orderitems AS oi ON o.oid = oi.oid JOIN items AS i ON oi.iid = i.iid; + +SELECT c.cfirst, o.oid, o.odate, i.iitem FROM customers AS c JOIN orders AS o ON c.cid = o.cid JOIN orderitems AS oi ON o.oid = oi.oid JOIN items AS i ON oi.iid = i.iid WHERE i.iitem = 'chocolate'; + +SELECT c.cfirst, o.oid, o.odate, i.iitem FROM customers AS c JOIN orders AS o ON c.cid = o.cid JOIN orderitems AS oi ON o.oid = oi.oid JOIN items AS i ON oi.iid = i.iid WHERE EXISTS (SELECT c.cfirst, o.oid, o.odate, i.iitem FROM customers AS c JOIN orders AS o ON c.cid = o.cid JOIN orderitems AS oi ON o.oid = oi.oid JOIN items AS i ON oi.iid = i.iid WHERE i.iitem = 'chocolate'); + + + + diff --git a/000_temp/62-kelowna/06-hollywood/subqueries.sql b/000_temp/62-kelowna/06-hollywood/subqueries.sql new file mode 100644 index 00000000..cd457ca0 --- /dev/null +++ b/000_temp/62-kelowna/06-hollywood/subqueries.sql @@ -0,0 +1,39 @@ +CREATE DATABASE hollywood; + +\l + +\c hollywood + +CREATE TABLE customers (cid INT PRIMARY KEY NOT NULL, cname TEXT); + +INSERT INTO customers VALUES (1,'Charlie'), (2,'Chris'), (3,'Christina'), (4,'Cassandra'), (5,'Cull'), (6,'Cabron'), (7,'Carla'), (8,'Caitland'), (9,'Colleen'), (10,'Christian'); + +CREATE TABLE movies (mid INT PRIMARY KEY NOT NULL, mname TEXT); + +INSERT INTO movies VALUES (1,'Jaws'), (2,'First Blood'), (3,'Rambo'), (4,'Terms of Endearment'), (5,'Heat'), (6,'The Lives of Others'), (7,'A Man Called Ove'), (8,'Disconnect'), (9,'Ex Machina'), (10,'Castle'); + +CREATE TABLE actors (aid INT PRIMARY KEY NOT NULL, aname TEXT); + +INSERT INTO actors VALUES (1,'Alfred'), (2,'Albert'), (3,'Angel'), (4,'Angelina'), (5,'Angie'), (6,'Agnes'), (7,'Amnia'), (8,'Allison'), (9,'Alejandro'), (10,'Aerial'); + +CREATE TABLE rentals (rid INT PRIMARY KEY NOT NULL, cid INT NOT NULL REFERENCES customers(cid), mid INT NOT NULL REFERENCES movies(mid)); + +INSERT INTO rentals VALUES (1,2,8),(2,8,10),(3,2,9),(4,6,1),(5,7,1),(6,5,2),(7,3,10),(8,9,5),(9,2,6),(10,8,7),(11,6,7),(12,9,9),(13,8,8),(14,8,9),(15,1,6),(16,2,9),(17,8,2),(18,10,7),(19,8,2),(20,6,7),(21,4,1),(22,5,4),(23,4,8),(24,9,5),(25,10,4),(26,8,2),(27,10,10),(28,1,6),(29,9,9),(30,4,6),(31,2,1),(32,6,7),(33,7,9),(34,2,3),(35,4,7),(36,4,7),(37,3,9),(38,8,5),(39,8,4),(40,7,1),(41,4,4),(42,8,4),(43,2,10),(44,4,4),(45,2,3),(46,9,7),(47,7,8),(48,1,4),(49,3,4),(50,6,9),(51,6,2),(52,6,8),(53,8,1),(54,1,6),(55,1,3),(56,9,4),(57,2,3),(58,5,8),(59,8,8),(60,2,5),(61,7,3),(62,2,10),(63,7,1),(64,4,7),(65,10,2),(66,3,6),(67,6,1),(68,8,10),(69,9,9),(70,5,4),(71,5,8),(72,3,3),(73,7,10),(74,1,7),(75,2,7),(76,1,2),(77,5,5),(78,6,4),(79,2,1),(80,3,9),(81,8,2),(82,9,5),(83,3,4),(84,3,9),(85,4,9),(86,7,1),(87,6,1),(88,5,3),(89,8,6),(90,2,10),(91,1,4),(92,1,10),(93,1,4),(94,1,5),(95,8,1),(96,6,3),(97,10,1),(98,9,7),(99,6,7),(100,1,7); + +CREATE TABLE castmembers (caid INT PRIMARY KEY NOT NULL, mid INT NOT NULL REFERENCES movies(mid), aid INT NOT NULL REFERENCES actors(aid)); + +INSERT INTO castmembers VALUES (1,2,8),(2,8,10),(3,2,9),(4,6,1),(5,7,1),(6,5,2),(7,3,10),(8,9,5),(9,2,6),(10,8,7),(11,6,7),(12,9,9),(13,8,8),(14,8,9),(15,1,6),(16,2,9),(17,8,2),(18,10,7),(19,8,2),(20,6,7),(21,4,1),(22,5,4),(23,4,8),(24,9,5),(25,10,4),(26,8,2),(27,10,10),(28,1,6),(29,9,9),(30,4,6),(31,2,1),(32,6,7),(33,7,9),(34,2,3),(35,4,7),(36,4,7),(37,3,9),(38,8,5),(39,8,4),(40,7,1),(41,4,4),(42,8,4),(43,2,10),(44,4,4),(45,2,3),(46,9,7),(47,7,8),(48,1,4),(49,3,4),(50,6,9),(51,6,2),(52,6,8),(53,8,1),(54,1,6),(55,1,3),(56,9,4),(57,2,3),(58,5,8),(59,8,8),(60,2,5),(61,7,3),(62,2,10),(63,7,1),(64,4,7),(65,10,2),(66,3,6),(67,6,1),(68,8,10),(69,9,9),(70,5,4),(71,5,8),(72,3,3),(73,7,10),(74,1,7),(75,2,7),(76,1,2),(77,5,5),(78,6,4),(79,2,1),(80,3,9),(81,8,2),(82,9,5),(83,3,4),(84,3,9),(85,4,9),(86,7,1),(87,6,1),(88,5,3),(89,8,6),(90,2,10),(91,1,4),(92,1,10),(93,1,4),(94,1,5),(95,8,1),(96,6,3),(97,10,1),(98,9,7),(99,6,7),(100,1,7); + + +SELECT c.cname, m.mname FROM customers AS c JOIN rentals AS r ON c.cid = r.cid JOIN movies AS m ON r.mid = m.mid; + +SELECT a.aname, m.mname FROM actors AS a JOIN castmembers AS cm ON a.aid = cm.aid JOIN movies AS m ON cm.mid = m.mid; + +INSERT INTO customers VALUES (101, 'Mike'), (102, 'Max'); + +INSERT INTO movies VALUES (101, 'Aliens'), (102, 'Ragoon'); + +INSERT INTO actors VALUES (101, 'Milfred'), (102, 'Martine'); + +SELECT c.cname, m.mname, a.aname FROM customers AS c FULL JOIN rentals AS r ON c.cid = r.cid FULL JOIN movies AS m ON r.mid = m.mid FULL JOIN castmembers AS cm ON m.mid = cm.mid FULL JOIN actors AS a ON cm.aid = a.aid; + diff --git a/000_temp/62-kelowna/07-sql-aggregate-funcs/agg.sql b/000_temp/62-kelowna/07-sql-aggregate-funcs/agg.sql new file mode 100644 index 00000000..95ac5eb3 --- /dev/null +++ b/000_temp/62-kelowna/07-sql-aggregate-funcs/agg.sql @@ -0,0 +1,85 @@ +/* COUNT */ + +/* from cooking database */ + +SELECT COUNT(recipetitle) AS totalRecipes FROM recipes; + +SELECT COUNT(DISTINCT recipetitle) AS totalDistRecipes FROM recipes; + +/* from hollywood database*/ +SELECT c.cname, m.mname, a.aname FROM ((((customers AS c JOIN rentals AS r ON c.cid = r.cid) JOIN movies AS m ON r.mid = m.mid) JOIN castmembers AS cm ON m.mid = cm.mid) JOIN actors AS a ON cm.aid = a.aid); + +SELECT c.cname, m.mname, a.aname FROM ((((customers AS c LEFT JOIN rentals AS r ON c.cid = r.cid) LEFT JOIN movies AS m ON r.mid = m.mid) LEFT JOIN castmembers AS cm ON m.mid = cm.mid) LEFT JOIN actors AS a ON cm.aid = a.aid); + +SELECT r.rid, m.mname FROM rentals AS r JOIN movies AS m ON r.mid = m.mid; + +SELECT r.rid, m.mname FROM rentals AS r RIGHT JOIN movies AS m ON r.mid = m.mid; + +SELECT cm.caid, a.aname FROM castmembers AS cm RIGHT JOIN actors AS a ON cm.aid = a.aid; + +SELECT cm.caid, a.aname FROM actors AS a LEFT JOIN castmembers AS cm ON a.aid = cm.aid; + +SELECT c.cname, m.mname, a.aname FROM ((((customers AS c FULL JOIN rentals AS r ON c.cid = r.cid) FULL JOIN movies AS m ON r.mid = m.mid) FULL JOIN castmembers AS cm ON m.mid = cm.mid) FULL JOIN actors AS a ON cm.aid = a.aid); + +SELECT COUNT(*) AS totalRentals FROM rentals; + +SELECT COUNT(cname) AS custs FROM customers; + +SELECT COUNT(DISTINCT cname) AS distinctCusts FROM customers; + + + +/* SUM */ + +DROP TABLE rentals; + +CREATE TABLE rentals (rid INT PRIMARY KEY NOT NULL, cid INT NOT NULL REFERENCES customers(cid), mid INT NOT NULL REFERENCES movies(mid), rAmount INT NOT NULL); + +https://play.golang.org/p/H7afRPh5bUd + +INSERT INTO rentals VALUES (1,2,8,5),(2,4,2,6),(3,8,1,4),(4,7,5,4),(5,11,10,6),(6,7,2,3),(7,9,7,3),(8,1,9,6),(9,10,8,5),(10,7,1,3),(11,1,9,5),(12,2,10,4),(13,2,2,3),(14,5,4,3),(15,6,4,6),(16,5,9,7),(17,6,4,5),(18,8,10,7),(19,8,6,6),(20,11,4,3),(21,5,1,3),(22,3,7,6),(23,2,3,6),(24,4,4,4),(25,6,9,5),(26,1,8,6),(27,7,1,6),(28,11,8,6),(29,11,10,6),(30,9,2,5),(31,4,7,4),(32,11,1,6),(33,4,4,3),(34,10,6,4),(35,1,8,5),(36,4,1,3),(37,7,3,6),(38,4,2,5),(39,11,8,5),(40,8,2,7),(41,4,3,4),(42,10,7,3),(43,1,7,7),(44,6,3,3),(45,10,1,5),(46,7,9,6),(47,1,4,7),(48,4,3,5),(49,7,10,3),(50,5,2,4),(51,7,2,7),(52,10,6,6),(53,4,1,5),(54,3,8,4),(55,7,5,5),(56,7,3,6),(57,8,9,4),(58,5,6,3),(59,8,3,5),(60,5,2,7),(61,3,4,3),(62,7,1,6),(63,1,5,5),(64,3,6,5),(65,10,1,6),(66,7,6,4),(67,11,7,7),(68,8,2,5),(69,5,1,7),(70,5,8,5),(71,8,2,6),(72,1,9,4),(73,5,8,5),(74,8,6,4),(75,11,6,5),(76,9,1,4),(77,11,3,3),(78,10,3,7),(79,5,1,3),(80,7,5,7),(81,10,2,4),(82,2,7,3),(83,3,8,6),(84,6,5,3),(85,1,6,7),(86,5,1,5),(87,9,1,7),(88,6,6,6),(89,7,1,7),(90,3,1,4),(91,11,3,7),(92,2,5,5),(93,4,4,4),(94,5,3,3),(95,8,2,5),(96,9,6,5),(97,10,3,7),(98,10,2,3),(99,9,4,6),(100,8,10,5); + +SELECT c.cname, m.mname, a.aname, r.rAmount FROM ((((customers AS c FULL JOIN rentals AS r ON c.cid = r.cid) FULL JOIN movies AS m ON r.mid = m.mid) FULL JOIN castmembers AS cm ON m.mid = cm.mid) FULL JOIN actors AS a ON cm.aid = a.aid); + +SELECT SUM(rAmount) AS totalRev FROM rentals; + +select c.cname, r.ramount from customers AS c JOIN rentals AS r ON c.cid = r.cid; + +select c.cname, r.rid, r.ramount from customers AS c JOIN rentals AS r ON c.cid = r.cid; + +select c.cname, r.rid, r.ramount from customers AS c FULL JOIN rentals AS r ON c.cid = r.cid; + +SELECT c.cid, c.cname, r.rid, r.ramount FROM customers AS c FULL JOIN rentals AS r ON c.cid = r.cid; + +SELECT c.cid, c.cname, r.rid, r.ramount, m.mname, a.aname FROM ((((customers AS c FULL JOIN rentals AS r ON c.cid = r.cid) FULL JOIN movies AS m ON r.mid = m.mid) FULL JOIN castmembers AS cm ON m.mid = cm.mid) FULL JOIN actors AS a ON cm.aid = a.aid); + +SELECT c.cid, c.cname, SUM(r.ramount) AS bigSpend FROM customers AS c FULL JOIN rentals AS r ON c.cid = r.cid GROUP BY c.cid; + +SELECT c.cid, c.cname, SUM(r.ramount) AS bigSpend FROM customers AS c FULL JOIN rentals AS r ON c.cid = r.cid GROUP BY c.cid ORDER BY bigSpend DESC; + + + +/* AVG */ + +SELECT AVG(ramount) FROM rentals; + +SELECT c.cid, c.cname, AVG(r.ramount) AS bigSpend FROM customers AS c FULL JOIN rentals AS r ON c.cid = r.cid GROUP BY c.cid; + + + +/* MAX */ +SELECT MAX(ramount) FROM rentals; + +SELECT c.cid, c.cname, MAX(r.ramount) AS bigSpend FROM customers AS c FULL JOIN rentals AS r ON c.cid = r.cid GROUP BY c.cid; + + + +/* MIN */ +SELECT MIN(ramount) FROM rentals; + +SELECT c.cid, c.cname, MIN(r.ramount) AS bigSpend FROM customers AS c FULL JOIN rentals AS r ON c.cid = r.cid GROUP BY c.cid; + + +/* AFTER CLASS */ + +SELECT cu.cname AS firstName FROM customers AS cu; \ No newline at end of file diff --git a/000_temp/62-kelowna/08-sql-group/agg.sql b/000_temp/62-kelowna/08-sql-group/agg.sql new file mode 100644 index 00000000..86c51d35 --- /dev/null +++ b/000_temp/62-kelowna/08-sql-group/agg.sql @@ -0,0 +1,86 @@ +CREATE DATABASE entertainers; + +\l + +\c entertainers + +CREATE TABLE entertainers (eid INT PRIMARY KEY NOT NULL, ename TEXT NOT NULL); + +\d + +\d entertainers + +CREATE TABLE engagements (enID INT PRIMARY KEY NOT NULL, eid INT NOT NULL REFERENCES entertainers(eid), encontractprice INT NOT NULL); + +INSERT INTO entertainers VALUES (1, 'WOOKIE-WOOKIE'), (2, 'HodgePodge'), (3, 'ScarlettLetters'), (4, 'TomorrowsSun'), (5, 'WhereWithAll'), (6, 'CowboyFutures'), (7, 'DesperateTortillas'), (8, 'CaldenarFlippers'), (9, 'LaughingJackals'), (10, 'CareeningForthright'); + + +generate values +https://play.golang.org/p/iET0hmFrcgf + +INSERT INTO engagements VALUES (1,2,9888),(2,8,6060),(3,2,3319),(4,6,4541),(5,7,5301),(6,5,10512),(7,3,7090),(8,9,5275),(9,2,3446),(10,8,11107),(11,6,7467),(12,9,8259),(13,8,11948),(14,8,4889),(15,1,5016),(16,2,2409),(17,8,8832),(18,10,7357),(19,8,2632),(20,6,7027),(21,4,5091),(22,5,2564),(23,4,6148),(24,9,6325),(25,10,3354),(26,8,5722),(27,10,4200),(28,1,10706),(29,9,6539),(30,4,11356),(31,2,10511),(32,6,2157),(33,7,11829),(34,2,9203),(35,4,7747),(36,4,6377),(37,3,11719),(38,8,7095),(39,8,9464),(40,7,8421),(41,4,2954),(42,8,5134),(43,2,2060),(44,4,10644),(45,2,4003),(46,9,11337),(47,7,11108),(48,1,8504),(49,3,11844),(50,6,3599),(51,6,3352),(52,6,11758),(53,8,10011),(54,1,7286),(55,1,5633),(56,9,10554),(57,2,10583),(58,5,3298),(59,8,8138),(60,2,7895),(61,7,7803),(62,2,4080),(63,7,3271),(64,4,5087),(65,10,10982),(66,3,9176),(67,6,7711),(68,8,5750),(69,9,4819),(70,5,9904),(71,5,6548),(72,3,3533),(73,7,9840),(74,1,7787),(75,2,10077),(76,1,9352),(77,5,2365),(78,6,11184),(79,2,2091),(80,3,4259),(81,8,5232),(82,9,10155),(83,3,3224),(84,3,6209),(85,4,3969),(86,7,5711),(87,6,2441),(88,5,5163),(89,8,6416),(90,2,5040),(91,1,11514),(92,1,3360),(93,1,2784),(94,1,4985),(95,8,10011),(96,6,6163),(97,10,9921),(98,9,8757),(99,6,10667),(100,1,11457); + +SELECT e.ename, en.encontractprice FROM entertainers AS e INNER JOIN engagements AS en ON e.eid = en.eid; + +SELECT e.ename, en.encontractprice FROM entertainers AS e JOIN engagements AS en ON e.eid = en.eid; + +INSERT INTO entertainers VALUES (11, 'SomebodyNobodyLoves'); + +SELECT e.ename, en.encontractprice FROM entertainers AS e LEFT JOIN engagements AS en ON e.eid = en.eid; + +SELECT e.ename, en.encontractprice FROM entertainers AS e FULL JOIN engagements AS en ON e.eid = en.eid; + +SELECT COUNT(encontractprice) as TotalEngagements FROM engagements; +SELECT SUM(encontractprice) as TotalRevenue FROM engagements; +SELECT MAX(encontractprice) as MaxContractPrice FROM engagements; +SELECT MIN(encontractprice) as MinContractPrice FROM engagements; +SELECT AVG(encontractprice) as AvgContractPrice FROM engagements; + +SELECT e.ename, MAX(en.encontractprice) as MaxContractPrice FROM entertainers AS e JOIN engagements AS en ON e.eid = en.eid GROUP BY e.ename; + +SELECT e.ename, MIN(en.encontractprice) as MinContractPrice FROM entertainers AS e JOIN engagements AS en ON e.eid = en.eid GROUP BY e.ename ORDER BY MinContractPrice; + +SELECT e.ename, AVG(en.encontractprice) as AvgContractPrice FROM entertainers AS e JOIN engagements AS en ON e.eid = en.eid GROUP BY e.ename ORDER BY AvgContractPrice; + +SELECT e.ename, SUM(en.encontractprice) as SumContractPrice FROM entertainers AS e JOIN engagements AS en ON e.eid = en.eid GROUP BY e.ename ORDER BY SumContractPrice; + +SELECT e.ename, COUNT(en.encontractprice) as CountContractPrice FROM entertainers AS e JOIN engagements AS en ON e.eid = en.eid GROUP BY e.ename ORDER BY CountContractPrice; + +SELECT e.ename, COUNT(en.encontractprice) AS NumContracts, SUM(en.encontractprice) AS TotalRevenue, MIN(en.encontractprice) AS MinRev, MAX(en.encontractprice) AS MaxRev, AVG(en.encontractprice) AS AvgRev FROM entertainers AS e JOIN engagements AS en ON e.eid = en.eid GROUP BY e.ename ORDER BY NumContracts; + +SELECT e.ename, COUNT(en.encontractprice) AS NumContracts, SUM(en.encontractprice) AS TotalRevenue, MIN(en.encontractprice) AS MinRev, MAX(en.encontractprice) AS MaxRev, AVG(en.encontractprice) AS AvgRev FROM entertainers AS e LEFT JOIN engagements AS en ON e.eid = en.eid GROUP BY e.ename ORDER BY NumContracts; + +SELECT e.ename, COUNT(*) AS NumContracts, SUM(en.encontractprice) AS TotalRevenue, MIN(en.encontractprice) AS MinRev, MAX(en.encontractprice) AS MaxRev, AVG(en.encontractprice) AS AvgRev FROM entertainers AS e LEFT JOIN engagements AS en ON e.eid = en.eid GROUP BY e.ename ORDER BY NumContracts; + + + +/* multiple fields in group by*/ + +DROP TABLE engagements; + +DROP TABLE entertainers; + +CREATE TABLE entertainers (eid INT PRIMARY KEY NOT NULL, ename TEXT NOT NULL, ecity TEXT NOT NULL); + +INSERT INTO entertainers VALUES (1, 'WOOKIE-WOOKIE', 'Paris'), (2, 'HodgePodge', 'Paris'), (3, 'ScarlettLetters', 'Paris'), (4, 'TomorrowsSun', 'Hollywood'), (5, 'WhereWithAll', 'Hollywood'), (6, 'CowboyFutures', 'Hollywood'), (7, 'DesperateTortillas', 'New York'), (8, 'CaldenarFlippers', 'New York'), (9, 'LaughingJackals', 'San Francisco'), (10, 'CareeningForthright', 'San Francisco'); + +CREATE TABLE engagements (enID INT PRIMARY KEY NOT NULL, eid INT NOT NULL REFERENCES entertainers(eid), encontractprice INT NOT NULL); + +generate values +https://play.golang.org/p/iET0hmFrcgf + +INSERT INTO engagements VALUES (1,2,9888),(2,8,6060),(3,2,3319),(4,6,4541),(5,7,5301),(6,5,10512),(7,3,7090),(8,9,5275),(9,2,3446),(10,8,11107),(11,6,7467),(12,9,8259),(13,8,11948),(14,8,4889),(15,1,5016),(16,2,2409),(17,8,8832),(18,10,7357),(19,8,2632),(20,6,7027),(21,4,5091),(22,5,2564),(23,4,6148),(24,9,6325),(25,10,3354),(26,8,5722),(27,10,4200),(28,1,10706),(29,9,6539),(30,4,11356),(31,2,10511),(32,6,2157),(33,7,11829),(34,2,9203),(35,4,7747),(36,4,6377),(37,3,11719),(38,8,7095),(39,8,9464),(40,7,8421),(41,4,2954),(42,8,5134),(43,2,2060),(44,4,10644),(45,2,4003),(46,9,11337),(47,7,11108),(48,1,8504),(49,3,11844),(50,6,3599),(51,6,3352),(52,6,11758),(53,8,10011),(54,1,7286),(55,1,5633),(56,9,10554),(57,2,10583),(58,5,3298),(59,8,8138),(60,2,7895),(61,7,7803),(62,2,4080),(63,7,3271),(64,4,5087),(65,10,10982),(66,3,9176),(67,6,7711),(68,8,5750),(69,9,4819),(70,5,9904),(71,5,6548),(72,3,3533),(73,7,9840),(74,1,7787),(75,2,10077),(76,1,9352),(77,5,2365),(78,6,11184),(79,2,2091),(80,3,4259),(81,8,5232),(82,9,10155),(83,3,3224),(84,3,6209),(85,4,3969),(86,7,5711),(87,6,2441),(88,5,5163),(89,8,6416),(90,2,5040),(91,1,11514),(92,1,3360),(93,1,2784),(94,1,4985),(95,8,10011),(96,6,6163),(97,10,9921),(98,9,8757),(99,6,10667),(100,1,11457); + +SELECT e.ecity, e.ename, COUNT(*) AS NumContracts, SUM(en.encontractprice) AS TotalRevenue, MIN(en.encontractprice) AS MinRev, MAX(en.encontractprice) AS MaxRev, AVG(en.encontractprice) AS AvgRev FROM entertainers AS e LEFT JOIN engagements AS en ON e.eid = en.eid GROUP BY e.ecity, e.ename ORDER BY e.ecity, NumContracts; + +/* simulating DISTINCT */ + +SELECT ecity FROM entertainers; + +SELECT DISTINCT(ecity) FROM entertainers; + +SELECT ecity FROM entertainers GROUP BY ecity; + +SELECT ecity, COUNT(*) AS bandspercity FROM entertainers GROUP BY ecity; + + diff --git a/000_temp/63-fall-2018/001-hello-world/hello.go b/000_temp/63-fall-2018/001-hello-world/hello.go new file mode 100644 index 00000000..d2c4e91e --- /dev/null +++ b/000_temp/63-fall-2018/001-hello-world/hello.go @@ -0,0 +1,7 @@ +package main + +import "fmt" + +func main() { + fmt.Println("Hello world") +} diff --git a/000_temp/63-fall-2018/002-hello-world/hello.go b/000_temp/63-fall-2018/002-hello-world/hello.go new file mode 100644 index 00000000..24dcac87 --- /dev/null +++ b/000_temp/63-fall-2018/002-hello-world/hello.go @@ -0,0 +1,9 @@ +package main + +import ( + "fmt" +) + +func main() { + fmt.Println("Hello world") +} diff --git a/000_temp/63-fall-2018/003-func/func.go b/000_temp/63-fall-2018/003-func/func.go new file mode 100644 index 00000000..d267b4f3 --- /dev/null +++ b/000_temp/63-fall-2018/003-func/func.go @@ -0,0 +1,21 @@ +package main + +import ( + "fmt" +) + +func main() { + fmt.Println("Hello world") + secondStatement() + finalStat() +} + +// func receiver identifier(parameters) returns {code} + +func secondStatement() { + fmt.Println("Here is my second statement") +} + +func finalStat() { + fmt.Println("about to exit") +} diff --git a/000_temp/63-fall-2018/004-variables/main.go b/000_temp/63-fall-2018/004-variables/main.go new file mode 100644 index 00000000..12ea85a5 --- /dev/null +++ b/000_temp/63-fall-2018/004-variables/main.go @@ -0,0 +1,20 @@ +package main + +import ( + "fmt" +) + +func main() { + fname := "James" + lname := "Bond" + fmt.Println(fname, lname) + girlfriend := "Miss Moneypenny" + fmt.Println(girlfriend) +} + +/* +declare, assign, initialize +DECLARE a VARIABLE stores a VALUE of a certain TYPE +we ASSIGN a VALUE of a certain TYPE to a VARIABLE +declare & assign ==> initialize +*/ diff --git a/000_temp/63-fall-2018/005-variables/main.go b/000_temp/63-fall-2018/005-variables/main.go new file mode 100644 index 00000000..d25fc1d4 --- /dev/null +++ b/000_temp/63-fall-2018/005-variables/main.go @@ -0,0 +1,18 @@ +package main + +import ( + "fmt" +) + +var x int +var y string + +func main() { + fmt.Println(x) + fmt.Println("|", y, "|") +} + +// zero value +// if you declare a VAR of a certain TYPE +// and do not ASSIGN a VALUE to that VAR +// a ZERO VALUE will be ASSIGNED to that VAR diff --git a/000_temp/63-fall-2018/006-variables/main.go b/000_temp/63-fall-2018/006-variables/main.go new file mode 100644 index 00000000..20e52d13 --- /dev/null +++ b/000_temp/63-fall-2018/006-variables/main.go @@ -0,0 +1,13 @@ +package main + +import ( + "fmt" +) + +var x = 42 +var y = "M" + +func main() { + fmt.Println(x) + fmt.Println(y) +} diff --git a/000_temp/63-fall-2018/007-params/main.go b/000_temp/63-fall-2018/007-params/main.go new file mode 100644 index 00000000..257099fe --- /dev/null +++ b/000_temp/63-fall-2018/007-params/main.go @@ -0,0 +1,28 @@ +package main + +import ( + "fmt" +) + +func main() { + fmt.Println("Begin") + + // pass in an ARGUMENT to the func when you CALL IT + foo("James Bond") + + x := "Miss Moneypenny" + // pass in an ARGUMENT to the func when you CALL IT + foo(x) + + n, _ := fmt.Println("1") + + fmt.Println("number of bytes written", n) +} + +// foo takes a VALUE of TYPE string +// we could also say: +// foo has a parameter which is a VALUE of TYPE string +// funcs may be defined with PARAMETER(S) +func foo(name string) { + fmt.Println("Hello", name) +} diff --git a/000_temp/63-fall-2018/008-review-var-type-value/main.go b/000_temp/63-fall-2018/008-review-var-type-value/main.go new file mode 100644 index 00000000..d8b22e01 --- /dev/null +++ b/000_temp/63-fall-2018/008-review-var-type-value/main.go @@ -0,0 +1,19 @@ +package main + +import ( + "fmt" +) + +// DECLARE a variable to be of a certain TYPE +// we can only ever store VALUES of that TYPE in that VARIABLE +// the VARIABLE has an IDENTIFIER +// STATIC programming language +// IDIOMATIC - idioms are patterns of speech +// COMPILER will check to make sure our code is IDIOMATIC + +var x int + +func main() { + x = 7 + fmt.Println(x) +} diff --git a/000_temp/63-fall-2018/009-review-func/main.go b/000_temp/63-fall-2018/009-review-func/main.go new file mode 100644 index 00000000..8d9f014a --- /dev/null +++ b/000_temp/63-fall-2018/009-review-func/main.go @@ -0,0 +1,31 @@ +package main + +import "fmt" + +func main() { + fmt.Println("This is the entry point beginning of the programming") + + foo() + + // call bar with ARGUMENTS + a, b := bar("James Bond", 32) + fmt.Println(a) + fmt.Println("In 10 years Bond will be", b, "years old.") + + fmt.Println(`Program "about" to exit`) +} + +func foo() { + fmt.Println("Foo is here") +} + +// define bar with PARAMETERS +// EXPRESSIONS evaluate to some VALUE, eg, y + 10 +// STATEMENT is a line of code to be executed +func bar(x string, y int) (string, int) { + return fmt.Sprint(x, " is here and he is ", y, " years old"), y + 10 +} + +// we will pass ARGUMENTS in to a function that has been defined with PARAMETERS + +// func receiver identifier(parameters) return(s) {code} diff --git a/000_temp/63-fall-2018/010-hands-on/01/main.go b/000_temp/63-fall-2018/010-hands-on/01/main.go new file mode 100644 index 00000000..1e763f95 --- /dev/null +++ b/000_temp/63-fall-2018/010-hands-on/01/main.go @@ -0,0 +1,10 @@ +package main + +import "fmt" + +func main() { + x := 40 + y := 2 + z := x * y + fmt.Println(z) +} 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 new file mode 100644 index 00000000..0e8d297b --- /dev/null +++ b/000_temp/63-fall-2018/010-hands-on/02/main.go @@ -0,0 +1,16 @@ +package main + +import "fmt" + +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 new file mode 100644 index 00000000..e15ff88c --- /dev/null +++ b/000_temp/63-fall-2018/010-hands-on/03/main.go @@ -0,0 +1,21 @@ +package main + +import "fmt" + +func main() { + + // when we call a func, we pass in ARGUMENTS + foo("Todd", 42) + foo("James", 32) + foo("Jenny", 27) + foo("Cole", 19) +} + + +// func +// modularize our code and DRY (don't repeat yourself) +// funcs are defined with parameters +// parameters specify a VALUE of a certain TYPE to be passed in when the func is called +func foo(x string, y int) { + fmt.Println(x, y) +} 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 new file mode 100644 index 00000000..9d790777 --- /dev/null +++ b/000_temp/63-fall-2018/010-hands-on/04/main.go @@ -0,0 +1,14 @@ +package main + +import "fmt" + +func main() { + z := foo(40, 45) + 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 new file mode 100644 index 00000000..53cb74c7 --- /dev/null +++ b/000_temp/63-fall-2018/010-hands-on/05/main.go @@ -0,0 +1,15 @@ +package main + +import "fmt" + +func main() { + x := foo("Todd") + 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 new file mode 100644 index 00000000..330652c9 --- /dev/null +++ b/000_temp/63-fall-2018/011-loops/main.go @@ -0,0 +1,15 @@ +package main + +import "fmt" + +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 new file mode 100644 index 00000000..c7f06600 --- /dev/null +++ b/000_temp/63-fall-2018/012-conditional/main.go @@ -0,0 +1,17 @@ +package main + +import "fmt" + +func main() { + foo() +} + + +// func receiver identifier(parameters) returns {code} +func foo() { + for i := 0; i <= 100; i++ { + 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 new file mode 100644 index 00000000..bc594823 --- /dev/null +++ b/000_temp/63-fall-2018/013-slice/main.go @@ -0,0 +1,20 @@ +package main + +import "fmt" + +func main() { + xi := []int{2,3,4,5,6,7,8} + fmt.Println(xi) + + for i, v := range xi { + fmt.Println(i, v) + } + + xs := []string{"James", "Jenny", "M", "Q"} + + fmt.Println(xs) + + for i, v := range xs { + fmt.Println(i, v) + } +} diff --git a/000_temp/63-fall-2018/014-maps/main.go b/000_temp/63-fall-2018/014-maps/main.go new file mode 100644 index 00000000..5d2ab53f --- /dev/null +++ b/000_temp/63-fall-2018/014-maps/main.go @@ -0,0 +1,12 @@ +package main + +import "fmt" + +func main() { + m := map[string]int{"James":32, "Jenny":27,} + fmt.Println(m) + + for k, v := range m { + fmt.Println(k, v) + } +} diff --git a/000_temp/63-fall-2018/015-struct/main.go b/000_temp/63-fall-2018/015-struct/main.go new file mode 100644 index 00000000..8fcf44d6 --- /dev/null +++ b/000_temp/63-fall-2018/015-struct/main.go @@ -0,0 +1,45 @@ +package main + +import "fmt" + +type person struct { + 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",}, + } + fmt.Println(p1) + + p2 := person{ + first: "Jenny", + last: "Moneypenny", + age: 27, + sayings: []string{"Danger knows no gender", "A woman's place is in control",}, + } + fmt.Println(p2) + + xp := []person{p1, p2} + + fmt.Println("---------") + for i, v := range xp { + fmt.Println(i, v, v.first) + for j, w := range v.sayings { + fmt.Println(j, w) + } + } + fmt.Println("---------") + + 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 new file mode 100644 index 00000000..bc445452 --- /dev/null +++ b/000_temp/63-fall-2018/016-fun-with-text/main.go @@ -0,0 +1,18 @@ +package main + +import "fmt" + +func main() { + x := "ABCDE" + fmt.Println(x) + + fmt.Printf("%T\n", x) + + xr := []rune(x) + fmt.Println(xr) + + for _, v := range xr { + 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 new file mode 100644 index 00000000..ea59942c --- /dev/null +++ b/000_temp/63-fall-2018/017-slice/main.go @@ -0,0 +1,25 @@ +package main + +import "fmt" + +func main() { + x := 42 + y := 43 + xi := []int{x, y} + + fmt.Printf("%T\n", x) + fmt.Printf("%T\n", y) + fmt.Printf("%T\n", xi) + + fmt.Println(xi) + + a := "James" + b := "Jenny" + xs := []string{a, b} + + fmt.Printf("%T\n", a) + fmt.Printf("%T\n", b) + 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 new file mode 100644 index 00000000..c84807fd --- /dev/null +++ b/000_temp/63-fall-2018/018-map/main.go @@ -0,0 +1,11 @@ +package main + +import "fmt" + +func main() { + 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 new file mode 100644 index 00000000..d32ae55d --- /dev/null +++ b/000_temp/63-fall-2018/019-struct/01/main.go @@ -0,0 +1,16 @@ +package _1 + +import "fmt" + +type hotdog int +var x hotdog + +func main() { + x = 7 + fmt.Printf("%T\n", x) + fmt.Println(x) + y := int(x) + 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 new file mode 100644 index 00000000..7cb3b388 --- /dev/null +++ b/000_temp/63-fall-2018/019-struct/02/main.go @@ -0,0 +1,18 @@ +package main + +import "fmt" + +type person struct { + first string + license int + sayings []string +} + +func main() { + p1 := person{ + first: "James", + license: 007, + 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 new file mode 100644 index 00000000..61de635f --- /dev/null +++ b/000_temp/63-fall-2018/019-struct/03/main.go @@ -0,0 +1,31 @@ +package main + +import "fmt" + +type person struct { + first string + license int + sayings []string +} + +func main() { + p1 := person{ + first: "James", + license: 7, + sayings: []string{"Shaken, not stirred", "Bond, James Bond",}, + } + fmt.Println(p1) + + p2 := person{ + first: "Jenny", + license: 8, + 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,} + fmt.Println(mp) +} \ No newline at end of file diff --git a/000_temp/63-fall-2018/020-looping/01/main.go b/000_temp/63-fall-2018/020-looping/01/main.go new file mode 100644 index 00000000..b73205fc --- /dev/null +++ b/000_temp/63-fall-2018/020-looping/01/main.go @@ -0,0 +1,25 @@ +package main + +import "fmt" + +func main() { + + /* + for { + fmt.Println("hello") + } + */ + + // for init; cond; incrementor {code} + for i := 0; i <= 10; i++ { + fmt.Println(i) + } + + for i := 0; ; i++ { + fmt.Println(i, i) + if i == 11 { + break + } + } + +} diff --git a/000_temp/63-fall-2018/020-looping/02/main.go b/000_temp/63-fall-2018/020-looping/02/main.go new file mode 100644 index 00000000..1fb0b8ee --- /dev/null +++ b/000_temp/63-fall-2018/020-looping/02/main.go @@ -0,0 +1,33 @@ +package main + +import "fmt" + +func main() { + xi := []int{2, 3, 4, 5, 7, 9, 42} + + for i, v := range xi { + fmt.Println(i, v) + } + + for idx := range xi { + fmt.Println(idx) + } + + for _, val := range xi { + fmt.Println("-",val) + } + + m := map[string]int{"James":32, "Jenny":27,} + + for k, v := range m { + fmt.Println(k, v) + } + + for key := range m { + fmt.Println(key) + } + + 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 new file mode 100644 index 00000000..608fdde7 --- /dev/null +++ b/000_temp/63-fall-2018/021-receiver/01/main.go @@ -0,0 +1,42 @@ +package main + +import "fmt" + +type person struct { + first string + last string + saying string +} + +// func (receiver) identifier(parameters) (returns) {code} + +func (p person) speak() { + fmt.Println(p.first, "says", p.saying) +} + +func main() { + p1 := person{ + first: "James", + last: "Bond", + saying: "Shaken, not stirred.", + } + + p2 := person{ + first: "Jenny", + last: "Moneypenny", + saying: "Helllllllo, James.", + } + + fmt.Println(p1) + fmt.Println(p2) + + fmt.Println("---------") + + fmt.Println(p1.first, "says", p1.saying) + fmt.Println(p2.first, "says", p2.saying) + + fmt.Println("---------") + + p1.speak() + p2.speak() +} diff --git a/000_temp/63-fall-2018/022-interfaces/01/main.go b/000_temp/63-fall-2018/022-interfaces/01/main.go new file mode 100644 index 00000000..e194fb3a --- /dev/null +++ b/000_temp/63-fall-2018/022-interfaces/01/main.go @@ -0,0 +1,71 @@ +package main + +import "fmt" + +type person struct { + first string + last string + saying string +} + +func (p person) speak() { + fmt.Println(p.first, "says", p.saying) +} + +func (sa secretAgent) speak() { + fmt.Println(sa.first, "says wackabacka haha", sa.saying) +} + +type secretAgent struct { + person + ltk bool +} +type human interface { + speak() +} + +func foo(h human) { + h.speak() +} + +func main() { + p1 := person{ + first: "James", + last: "Bond", + saying: "Shaken, not stirred.", + } + + p2 := person{ + first: "Jenny", + last: "Moneypenny", + saying: "Helllllllo, James.", + } + + sa1 := secretAgent{ + person: person{ + first: "Ian", + last: "Fleming", + saying: "The books were based on real stories", + }, + ltk: true, + } + + fmt.Println(p1) + fmt.Println(p2) + + fmt.Println("---------") + + fmt.Println(p1.first, "says", p1.saying) + fmt.Println(p2.first, "says", p2.saying) + + fmt.Println("---------") + + p1.speak() + p2.speak() + + fmt.Println("--------- interface") + + foo(p1) + foo(p2) + foo(sa1) +} diff --git a/000_temp/63-fall-2018/022-interfaces/02/main.go b/000_temp/63-fall-2018/022-interfaces/02/main.go new file mode 100644 index 00000000..e194fb3a --- /dev/null +++ b/000_temp/63-fall-2018/022-interfaces/02/main.go @@ -0,0 +1,71 @@ +package main + +import "fmt" + +type person struct { + first string + last string + saying string +} + +func (p person) speak() { + fmt.Println(p.first, "says", p.saying) +} + +func (sa secretAgent) speak() { + fmt.Println(sa.first, "says wackabacka haha", sa.saying) +} + +type secretAgent struct { + person + ltk bool +} +type human interface { + speak() +} + +func foo(h human) { + h.speak() +} + +func main() { + p1 := person{ + first: "James", + last: "Bond", + saying: "Shaken, not stirred.", + } + + p2 := person{ + first: "Jenny", + last: "Moneypenny", + saying: "Helllllllo, James.", + } + + sa1 := secretAgent{ + person: person{ + first: "Ian", + last: "Fleming", + saying: "The books were based on real stories", + }, + ltk: true, + } + + fmt.Println(p1) + fmt.Println(p2) + + fmt.Println("---------") + + fmt.Println(p1.first, "says", p1.saying) + fmt.Println(p2.first, "says", p2.saying) + + fmt.Println("---------") + + p1.speak() + p2.speak() + + fmt.Println("--------- interface") + + foo(p1) + foo(p2) + foo(sa1) +} diff --git a/000_temp/63-fall-2018/023-review/main.go b/000_temp/63-fall-2018/023-review/main.go new file mode 100644 index 00000000..9f33dc01 --- /dev/null +++ b/000_temp/63-fall-2018/023-review/main.go @@ -0,0 +1,124 @@ +package main + +import "fmt" + +var c int +var d = 43 + +const z = 142 + +type person struct { + first string + age int + saying string +} + +// func receiver identifier(params) returns {code} + +func (p person) speak() { + fmt.Println(p.first, "says", p.saying) +} + +type secretagent struct { + person + ltk bool +} + +func (sa secretagent) speak() { + fmt.Println(sa.first, "says even more", sa.saying) +} + +type human interface { + speak() +} + +func foo(h human) { + h.speak() +} + +func main() { + a := "James" + fmt.Println(a) + fmt.Printf("%T\n", a) + + b := fmt.Sprint("Hello", a) + fmt.Println(b) + + c = 42 + fmt.Println("c:", c) + + d = 43 + fmt.Println("d:", d) + fmt.Printf("d type: %T\n", d) + + // slice AGGREGATE or COMPOSITE data structure + + ee := []int{2,3,4,7,9,} + fmt.Println(ee) + + // it was going SEQUENCE + // now it's going ITERATIVE + + for i, v := range ee { + fmt.Println(i, v) + // conditional logic + if i == 3 { + fmt.Println("HEY, i is EQUAL TO", i) + } + } + + + // map AGGREGATE or COMPOSITE data structure + f := map[string]int{"James":32, "Jenny":27,} + fmt.Println(f) + + for k, v := range f { + fmt.Println(k, v) + } + + // struct AGGREGATE or COMPOSITE data structure + p1 := person{ + first: "James", + age: 32, + saying: "shaken, not stirred", + } + + p2 := person { + first: "Jenny", + age: 27, + saying: "nobody does it better", + } + + xp := []person{p1, p2} + fmt.Println(xp) + for i2, v2 := range xp { + fmt.Println(i2, v2) + } + + // loop init, cond, post + for j := 0; j < 10; j++ { + fmt.Println(j) + } + + sa1 := secretagent{ + person: person{ + first: "Jack", + age: 29, + saying: "Blahb blahblabhalbhablhab", + }, + ltk: true, + } + fmt.Println(sa1) + fmt.Println(sa1.first) + fmt.Println(sa1.person.first) + + p1.speak() + p2.speak() + sa1.speak() + fmt.Println("--") + foo(p1) + foo(p2) + foo(sa1) + + fmt.Println(z) +} diff --git a/000_temp/63-fall-2018/024-unfurling-slice/main.go b/000_temp/63-fall-2018/024-unfurling-slice/main.go new file mode 100644 index 00000000..5cbce4f9 --- /dev/null +++ b/000_temp/63-fall-2018/024-unfurling-slice/main.go @@ -0,0 +1,13 @@ +package main + +import "fmt" + +func main() { + 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 new file mode 100644 index 00000000..26cd71ca --- /dev/null +++ b/000_temp/63-fall-2018/025-defer/main.go @@ -0,0 +1,26 @@ +package main + +import "fmt" + +func main() { + defer foo() + bar() +} + +func foo() { + fmt.Println("foooooooo") +} + +func bar() { + fmt.Println("barrrrrrrrr") + defer one() + two() +} + +func one() { + fmt.Println("oneeeeeee") +} + +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 new file mode 100644 index 00000000..6f1cd19d --- /dev/null +++ b/000_temp/63-fall-2018/026-anonymous-func/main.go @@ -0,0 +1,15 @@ +package main + +import "fmt" + +func main() { + foo() + + // anonymous func + + func(x int){ + fmt.Println(x) + }(4) +} + +func foo() {fmt.Println("this is foo")} \ No newline at end of file diff --git a/000_temp/63-fall-2018/027-func-expression/main.go b/000_temp/63-fall-2018/027-func-expression/main.go new file mode 100644 index 00000000..f536aab4 --- /dev/null +++ b/000_temp/63-fall-2018/027-func-expression/main.go @@ -0,0 +1,27 @@ +package main + +import "fmt" + +func main() { + a := foo("something") + fmt.Println(a) + + d := 14 + b := func(z string){ + fmt.Println(z) + } + + fmt.Printf("%T\n", d) + fmt.Printf("%T\n", b) + + b("james") + b("jenny") + x := foo("cat") + y := foo("bird") + 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 new file mode 100644 index 00000000..9fc52167 --- /dev/null +++ b/000_temp/63-fall-2018/028-returning-a-func/main.go @@ -0,0 +1,23 @@ +package main + +import "fmt" + +func main() { + x := foo() + y := foo() + fmt.Printf("%T\t %T\n", x,y) + + func(z string){ + fmt.Println(z) + }("james") + + x("jenny") + y("dogggggggggy") + +} + +func foo() func(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 new file mode 100644 index 00000000..e53221a7 --- /dev/null +++ b/000_temp/63-fall-2018/029-pointers/main.go @@ -0,0 +1,19 @@ +package main + +import "fmt" + +func main() { + x := 42 + fmt.Println(x) + fmt.Printf("%T\n", x) + fmt.Println(&x) + + y := &x + fmt.Println(y) + fmt.Printf("%T\n", y) + fmt.Println(*y) + + *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 new file mode 100644 index 00000000..2babcccd --- /dev/null +++ b/000_temp/63-fall-2018/030-os-args/main.go @@ -0,0 +1,13 @@ +package main + +import ( + "os" + "fmt" +) + +func main() { + fmt.Println(os.Args[0]) + fmt.Println(os.Args[1]) + fmt.Println(os.Args[2]) + fmt.Println(os.Args[3]) +} diff --git a/000_temp/63-fall-2018/031-string-template/main.go b/000_temp/63-fall-2018/031-string-template/main.go new file mode 100644 index 00000000..7d1d6803 --- /dev/null +++ b/000_temp/63-fall-2018/031-string-template/main.go @@ -0,0 +1,45 @@ +package main + +import ( + "fmt" + "os" + "log" + "strings" + "io" +) + +func main() { + // CREATE STRING + // assign to variable + name := "James" + str := `html here` + name + `more html` + fmt.Println(str) + + // CREATE STRING + // string print + s := fmt.Sprint(`mas ` + name + `menos`) + fmt.Println(s) + + // CREATE FILE + // io.Copy to the file + nf, err := os.Create("newfile.txt") + if err != nil { + log.Fatal("whoops", err) + } + + io.Copy(nf, strings.NewReader(s)) + + // CREATE FILE + // writestring to file + nf2, err := os.Create("newfile2.txt") + if err != nil { + log.Fatal("whoops", err) + } + + n, err := nf2.WriteString(str) + if err != nil { + log.Fatal("whoops2", err) + } + + fmt.Println("bytes written", n) +} diff --git a/000_temp/63-fall-2018/032-text-template/main.go b/000_temp/63-fall-2018/032-text-template/main.go new file mode 100644 index 00000000..6285de4a --- /dev/null +++ b/000_temp/63-fall-2018/032-text-template/main.go @@ -0,0 +1,22 @@ +package main + +import ( + "text/template" + "log" + "os" + "fmt" +) + +func main() { + tpl, err := template.ParseFiles("one.txt", "two.txt") + if err != nil { + log.Fatal("whoops", err) + } + + fmt.Println("\n-----") + tpl.ExecuteTemplate(os.Stdout, "one.txt", nil) + + fmt.Println("\n\n-----") + tpl.ExecuteTemplate(os.Stdout, "two.txt", "James") + fmt.Println("\n\n") +} diff --git a/000_temp/63-fall-2018/032-text-template/one.txt b/000_temp/63-fall-2018/032-text-template/one.txt new file mode 100644 index 00000000..045061a0 --- /dev/null +++ b/000_temp/63-fall-2018/032-text-template/one.txt @@ -0,0 +1 @@ +Hello, this is file one. \ No newline at end of file diff --git a/000_temp/63-fall-2018/032-text-template/two.txt b/000_temp/63-fall-2018/032-text-template/two.txt new file mode 100644 index 00000000..c93bf5b8 --- /dev/null +++ b/000_temp/63-fall-2018/032-text-template/two.txt @@ -0,0 +1 @@ +Hello, this is file two and my name is {{.}} \ No newline at end of file diff --git a/000_temp/63-fall-2018/033-parseglob/main.go b/000_temp/63-fall-2018/033-parseglob/main.go new file mode 100644 index 00000000..0d024b14 --- /dev/null +++ b/000_temp/63-fall-2018/033-parseglob/main.go @@ -0,0 +1,39 @@ +package main + +import ( + "text/template" + "os" +) + +var tpl *template.Template + +func init() { + tpl = template.Must(template.ParseGlob("templates/*")) +} + +func main() { + err := tpl.ExecuteTemplate(os.Stdout, "one.gohtml", nil) + if err != nil { + panic(err) + } + + err = tpl.ExecuteTemplate(os.Stdout, "two.gohtml", nil) + if err != nil { + panic(err) + } + + err = tpl.ExecuteTemplate(os.Stdout, "lasagne.php", nil) + if err != nil { + panic(err) + } + + err = tpl.ExecuteTemplate(os.Stdout, "anaheim.dland", nil) + if err != nil { + panic(err) + } + + err = tpl.ExecuteTemplate(os.Stdout, "lets.blowitup", nil) + if err != nil { + panic(err) + } +} diff --git a/000_temp/63-fall-2018/033-parseglob/templates/anaheim.dland b/000_temp/63-fall-2018/033-parseglob/templates/anaheim.dland new file mode 100644 index 00000000..c8540638 --- /dev/null +++ b/000_temp/63-fall-2018/033-parseglob/templates/anaheim.dland @@ -0,0 +1,2 @@ +this is file anaheim +****** \ No newline at end of file diff --git a/000_temp/63-fall-2018/033-parseglob/templates/lasagne.php b/000_temp/63-fall-2018/033-parseglob/templates/lasagne.php new file mode 100644 index 00000000..0596157e --- /dev/null +++ b/000_temp/63-fall-2018/033-parseglob/templates/lasagne.php @@ -0,0 +1,2 @@ +this is file lasagne +****** \ No newline at end of file diff --git a/000_temp/63-fall-2018/033-parseglob/templates/one.gohtml b/000_temp/63-fall-2018/033-parseglob/templates/one.gohtml new file mode 100644 index 00000000..d7432a4c --- /dev/null +++ b/000_temp/63-fall-2018/033-parseglob/templates/one.gohtml @@ -0,0 +1,2 @@ +this is file one +****** \ No newline at end of file diff --git a/000_temp/63-fall-2018/033-parseglob/templates/two.gohtml b/000_temp/63-fall-2018/033-parseglob/templates/two.gohtml new file mode 100644 index 00000000..d2648063 --- /dev/null +++ b/000_temp/63-fall-2018/033-parseglob/templates/two.gohtml @@ -0,0 +1,2 @@ +this is file two +****** \ No newline at end of file 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 new file mode 100644 index 00000000..4138fcfa --- /dev/null +++ b/000_temp/63-fall-2018/034-pass-in-data-aggregate/main.go @@ -0,0 +1,50 @@ +package main + +import ( + "text/template" + "os" +) + +var tpl *template.Template + +func init() { + tpl = template.Must(template.ParseGlob("templates/*")) +} + +type person struct { + First string + Last string + Age int +} + +func main() { + + p1 := person{ + First: "James", + Last: "Bond", + Age: 32, + } + + err := tpl.ExecuteTemplate(os.Stdout, "one.gohtml", p1) + if err != nil { + panic(err) + } + + p2 := person{ + First: "Jenny", + Last: "Moneypenny", + Age: 27, + } + + xp := []person{p1, p2} + err = tpl.ExecuteTemplate(os.Stdout, "two.gohtml", xp) + if err != nil { + panic(err) + } + + err = tpl.ExecuteTemplate(os.Stdout, "lasagne.php", p1) + if err != nil { + panic(err) + } + +} diff --git a/000_temp/63-fall-2018/034-pass-in-data-aggregate/templates/lasagne.php b/000_temp/63-fall-2018/034-pass-in-data-aggregate/templates/lasagne.php new file mode 100644 index 00000000..175b0a27 --- /dev/null +++ b/000_temp/63-fall-2018/034-pass-in-data-aggregate/templates/lasagne.php @@ -0,0 +1,2 @@ +this is file lasagne {{.First}} is {{.Last}} +****** \ No newline at end of file diff --git a/000_temp/63-fall-2018/034-pass-in-data-aggregate/templates/one.gohtml b/000_temp/63-fall-2018/034-pass-in-data-aggregate/templates/one.gohtml new file mode 100644 index 00000000..a7bd2240 --- /dev/null +++ b/000_temp/63-fall-2018/034-pass-in-data-aggregate/templates/one.gohtml @@ -0,0 +1,2 @@ +this is file one {{.}} +****** \ No newline at end of file diff --git a/000_temp/63-fall-2018/034-pass-in-data-aggregate/templates/two.gohtml b/000_temp/63-fall-2018/034-pass-in-data-aggregate/templates/two.gohtml new file mode 100644 index 00000000..0a8c762e --- /dev/null +++ b/000_temp/63-fall-2018/034-pass-in-data-aggregate/templates/two.gohtml @@ -0,0 +1,2 @@ +this is file two {{.}} +****** \ No newline at end of file diff --git a/000_temp/63-fall-2018/035-hash-bucket/main.go b/000_temp/63-fall-2018/035-hash-bucket/main.go new file mode 100644 index 00000000..abc30424 --- /dev/null +++ b/000_temp/63-fall-2018/035-hash-bucket/main.go @@ -0,0 +1,41 @@ +package main + +import ( + "bufio" + "fmt" + "log" + "net/http" +) + +func main() { + // get the book moby dick + res, err := http.Get("http://www.gutenberg.org/files/2701/2701-0.txt") + if err != nil { + log.Fatal(err) + } + + defer res.Body.Close() + scanner := bufio.NewScanner(res.Body) + // scan the page + // Set the split function for the scanning operation. + scanner.Split(bufio.ScanWords) + // Create slice to hold counts + buckets := make([]int, 2000) + // Loop over the words + for scanner.Scan() { + fmt.Print(scanner.Text(), " - ") + n := hashBucket(scanner.Text()) + buckets[n]++ + } + fmt.Println(buckets[65:123]) + // fmt.Println("***************") + // for i := 28; i <= 126; i++ { + // fmt.Printf("%v - %c - %v \n", i, i, buckets[i]) + // } +} + +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 new file mode 100644 index 00000000..ebbb00ac --- /dev/null +++ b/000_temp/63-fall-2018/036-templates-review/main.go @@ -0,0 +1,49 @@ +package main + +import ( + "html/template" + "os" + "log" +) + +var tpl *template.Template + +func init() { + tpl = template.Must(template.ParseGlob("templates/*.gohtml")) +} + +func main() { + err := tpl.ExecuteTemplate(os.Stdout, "main.gohtml", nil) + if err != nil { + log.Fatal("there was an error", err) + } + + err = tpl.ExecuteTemplate(os.Stdout, "about.gohtml", nil) + if err != nil { + log.Fatal("there was an error", err) + } + + f, err := os.Create("main.html") + if err != nil { + log.Fatal("another error", err) + } + defer f.Close() + + f2, err := os.Create("about.html") + if err != nil { + log.Fatal("another error", err) + } + defer f2.Close() + + err = tpl.ExecuteTemplate(f, "main.gohtml", nil) + if err != nil { + log.Fatal("there was an error", err) + } + + err = tpl.ExecuteTemplate(f2, "about.gohtml", nil) + if err != nil { + log.Fatal("there was an error", err) + } + + +} \ No newline at end of file diff --git a/000_temp/63-fall-2018/036-templates-review/templates/about.gohtml b/000_temp/63-fall-2018/036-templates-review/templates/about.gohtml new file mode 100644 index 00000000..89312acc --- /dev/null +++ b/000_temp/63-fall-2018/036-templates-review/templates/about.gohtml @@ -0,0 +1,12 @@ + + + + + About + + + +

Here is my about template

+ + + \ No newline at end of file diff --git a/000_temp/63-fall-2018/036-templates-review/templates/main.gohtml b/000_temp/63-fall-2018/036-templates-review/templates/main.gohtml new file mode 100644 index 00000000..00f69d92 --- /dev/null +++ b/000_temp/63-fall-2018/036-templates-review/templates/main.gohtml @@ -0,0 +1,12 @@ + + + + + Main + + + +

Here is my main template

+ + + \ No newline at end of file diff --git a/000_temp/63-fall-2018/037-data/01/about.html b/000_temp/63-fall-2018/037-data/01/about.html new file mode 100644 index 00000000..1ddf1801 --- /dev/null +++ b/000_temp/63-fall-2018/037-data/01/about.html @@ -0,0 +1,15 @@ + + + + + + + Document + + + + +

here's the data from about [7 8 9 125 345]

+ + \ No newline at end of file diff --git a/000_temp/63-fall-2018/037-data/01/default.html b/000_temp/63-fall-2018/037-data/01/default.html new file mode 100644 index 00000000..554b3e3a --- /dev/null +++ b/000_temp/63-fall-2018/037-data/01/default.html @@ -0,0 +1,15 @@ + + + + + + + Document + + + +

Here's the data 42

+ + + \ 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 new file mode 100644 index 00000000..fd858f6f --- /dev/null +++ b/000_temp/63-fall-2018/037-data/01/main.go @@ -0,0 +1,39 @@ +package main + +import ( + "html/template" + "os" + "log" +) + +var tpl *template.Template + +func init() { + tpl = template.Must(template.ParseGlob("templates/*.gohtml")) +} + +func main() { + f1, err := os.Create("default.html") + if err != nil { + log.Fatal("f1 errored", err) + } + defer f1.Close() + + f2, err := os.Create("about.html") + if err != nil { + log.Fatal("f1 errored", err) + } + defer f2.Close() + + err = tpl.ExecuteTemplate(f1, "default.gohtml", 42) + if err != nil { + panic(err) + } + + + 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/01/templates/about.gohtml b/000_temp/63-fall-2018/037-data/01/templates/about.gohtml new file mode 100644 index 00000000..4a098421 --- /dev/null +++ b/000_temp/63-fall-2018/037-data/01/templates/about.gohtml @@ -0,0 +1,15 @@ + + + + + + + Document + + + + +

here's the data from about {{.}}

+ + \ No newline at end of file diff --git a/000_temp/63-fall-2018/037-data/01/templates/default.gohtml b/000_temp/63-fall-2018/037-data/01/templates/default.gohtml new file mode 100644 index 00000000..86c227e5 --- /dev/null +++ b/000_temp/63-fall-2018/037-data/01/templates/default.gohtml @@ -0,0 +1,15 @@ + + + + + + + Document + + + +

Here's the data {{.}}

+ + + \ No newline at end of file diff --git a/000_temp/63-fall-2018/037-data/02/about.html b/000_temp/63-fall-2018/037-data/02/about.html new file mode 100644 index 00000000..348f6c97 --- /dev/null +++ b/000_temp/63-fall-2018/037-data/02/about.html @@ -0,0 +1,23 @@ + + + + + + + Document + + + + +

ABOUT here is a raw + +string + + +literal + + +

+ + \ No newline at end of file diff --git a/000_temp/63-fall-2018/037-data/02/default.html b/000_temp/63-fall-2018/037-data/02/default.html new file mode 100644 index 00000000..1afc6b1b --- /dev/null +++ b/000_temp/63-fall-2018/037-data/02/default.html @@ -0,0 +1,16 @@ + + + + + + + Document + + + +

DEFAULT here is text with space

+ + + + \ No newline at end of file diff --git a/000_temp/63-fall-2018/037-data/02/main.go b/000_temp/63-fall-2018/037-data/02/main.go new file mode 100644 index 00000000..957a710c --- /dev/null +++ b/000_temp/63-fall-2018/037-data/02/main.go @@ -0,0 +1,48 @@ +package main + +import ( + "html/template" + "os" + "log" +) + +var tpl *template.Template + +func init() { + tpl = template.Must(template.ParseGlob("templates/*.gohtml")) +} + +func main() { + f1, err := os.Create("default.html") + if err != nil { + log.Fatal("f1 errored", err) + } + defer f1.Close() + + f2, err := os.Create("about.html") + if err != nil { + log.Fatal("f1 errored", err) + } + defer f2.Close() + + s := " here is text with space " + err = tpl.ExecuteTemplate(f1, "default.gohtml", s) + if err != nil { + panic(err) + } + + + t := ` here is a raw + +string + + +literal + + +` + err = tpl.ExecuteTemplate(f2, "about.gohtml", t) + if err != nil { + panic(err) + } +} diff --git a/000_temp/63-fall-2018/037-data/02/templates/about.gohtml b/000_temp/63-fall-2018/037-data/02/templates/about.gohtml new file mode 100644 index 00000000..ec3546a3 --- /dev/null +++ b/000_temp/63-fall-2018/037-data/02/templates/about.gohtml @@ -0,0 +1,15 @@ + + + + + + + Document + + + + +

ABOUT {{- .}}

+ + \ No newline at end of file diff --git a/000_temp/63-fall-2018/037-data/02/templates/default.gohtml b/000_temp/63-fall-2018/037-data/02/templates/default.gohtml new file mode 100644 index 00000000..fb5302a8 --- /dev/null +++ b/000_temp/63-fall-2018/037-data/02/templates/default.gohtml @@ -0,0 +1,16 @@ + + + + + + + Document + + + +

DEFAULT {{- . -}}

+ + + + \ No newline at end of file diff --git a/000_temp/63-fall-2018/037-data/03/about.html b/000_temp/63-fall-2018/037-data/03/about.html new file mode 100644 index 00000000..6d39692f --- /dev/null +++ b/000_temp/63-fall-2018/037-data/03/about.html @@ -0,0 +1,15 @@ + + + + + + + Document + + + + +

ABOUT this is the else

+ + \ No newline at end of file diff --git a/000_temp/63-fall-2018/037-data/03/default.html b/000_temp/63-fall-2018/037-data/03/default.html new file mode 100644 index 00000000..83db2f29 --- /dev/null +++ b/000_temp/63-fall-2018/037-data/03/default.html @@ -0,0 +1,16 @@ + + + + + + + Document + + + +

DEFAULT evaluated

+ + + + \ No newline at end of file diff --git a/000_temp/63-fall-2018/037-data/03/main.go b/000_temp/63-fall-2018/037-data/03/main.go new file mode 100644 index 00000000..ca00444a --- /dev/null +++ b/000_temp/63-fall-2018/037-data/03/main.go @@ -0,0 +1,40 @@ +package main + +import ( + "html/template" + "os" + "log" +) + +var tpl *template.Template + +func init() { + tpl = template.Must(template.ParseGlob("templates/*.gohtml")) +} + +func main() { + f1, err := os.Create("default.html") + if err != nil { + log.Fatal("f1 errored", err) + } + defer f1.Close() + + f2, err := os.Create("about.html") + if err != nil { + log.Fatal("f1 errored", err) + } + defer f2.Close() + + t := true + err = tpl.ExecuteTemplate(f1, "default.gohtml", t) + if err != nil { + panic(err) + } + + + f := false + err = tpl.ExecuteTemplate(f2, "about.gohtml", f) + if err != nil { + panic(err) + } +} diff --git a/000_temp/63-fall-2018/037-data/03/templates/about.gohtml b/000_temp/63-fall-2018/037-data/03/templates/about.gohtml new file mode 100644 index 00000000..79bc1eb1 --- /dev/null +++ b/000_temp/63-fall-2018/037-data/03/templates/about.gohtml @@ -0,0 +1,15 @@ + + + + + + + Document + + + + +

ABOUT {{if .}}evaluated{{else}} this is the else{{end}}

+ + \ No newline at end of file diff --git a/000_temp/63-fall-2018/037-data/03/templates/default.gohtml b/000_temp/63-fall-2018/037-data/03/templates/default.gohtml new file mode 100644 index 00000000..053254bb --- /dev/null +++ b/000_temp/63-fall-2018/037-data/03/templates/default.gohtml @@ -0,0 +1,16 @@ + + + + + + + Document + + + +

DEFAULT {{if .}}evaluated{{else}} this is the else{{end}}

+ + + + \ No newline at end of file diff --git a/000_temp/64-HANDLER/main.go b/000_temp/64-HANDLER/main.go new file mode 100644 index 00000000..b5b6687c --- /dev/null +++ b/000_temp/64-HANDLER/main.go @@ -0,0 +1,28 @@ +package main + +import ( + "net/http" + "io" +) + + +type hotdog int + +// func receiver identifier(params) return(s) {} +func (h hotdog) ServeHTTP(w http.ResponseWriter, r *http.Request) { + io.WriteString(w, "WASSSSSUP!!!!!") +} + +func main() { + var d hotdog + http.ListenAndServe(":8080", d) +} + +/* + +type Handler +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 new file mode 100644 index 00000000..cce7dabe --- /dev/null +++ b/000_temp/65-DEFAULT-SERVE-MUX/main.go @@ -0,0 +1,47 @@ +package main + +import ( + "net/http" + "log" + "fmt" + "html" + "io" +) + + +type hotdog int + +func (h hotdog) ServeHTTP(w http.ResponseWriter, r *http.Request) { + io.WriteString(w, "hello from baz") +} + +func main() { + + var bazhandler hotdog + + http.Handle("/baz", bazhandler) + + http.HandleFunc("/foo", fooHandler) + + http.HandleFunc("/bar/", func(w http.ResponseWriter, r *http.Request) { + fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path)) + }) + + + log.Fatal(http.ListenAndServe(":8080", nil)) + +} + +func fooHandler(w http.ResponseWriter, r *http.Request) { + io.WriteString(w, "hello from foo") +} + + +/* + +type Handler +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 new file mode 100644 index 00000000..6eb300f5 --- /dev/null +++ b/000_temp/66-HANDLEFUNC/main.go @@ -0,0 +1,27 @@ +package main + +import ( + "net/http" + "io" + "fmt" +) + +func main() { + http.HandleFunc("/", home) + http.HandleFunc("/about", about) + http.HandleFunc("/contact", contact) + + http.ListenAndServe(":8080", nil) +} + +func home(w http.ResponseWriter, r *http.Request) { + io.WriteString(w, "hello from home") +} + +func about (w http.ResponseWriter, r *http.Request) { + io.WriteString(w, "wassup from about") +} + +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 new file mode 100644 index 00000000..efbb3464 --- /dev/null +++ b/000_temp/67-KABOOM-BOOOYAH/main.go @@ -0,0 +1,32 @@ +package main + +import ( + "net/http" + "html/template" +) + +var tpl *template.Template + +func init() { + tpl = template.Must(template.ParseGlob("templates/*.gohtml")) +} + +func main() { + http.HandleFunc("/", home) + http.HandleFunc("/about", about) + http.HandleFunc("/contact", contact) + + http.ListenAndServe(":8080", nil) +} + +func home(w http.ResponseWriter, r *http.Request) { + tpl.ExecuteTemplate(w, "default.gohtml", nil) +} + +func about (w http.ResponseWriter, r *http.Request) { + tpl.ExecuteTemplate(w, "about.gohtml", nil) +} + +func contact (w http.ResponseWriter, r * http.Request) { + tpl.ExecuteTemplate(w, "contact.gohtml", nil) +} diff --git a/000_temp/67-KABOOM-BOOOYAH/templates/about.gohtml b/000_temp/67-KABOOM-BOOOYAH/templates/about.gohtml new file mode 100644 index 00000000..1fdab8c9 --- /dev/null +++ b/000_temp/67-KABOOM-BOOOYAH/templates/about.gohtml @@ -0,0 +1 @@ +hello from about \ No newline at end of file diff --git a/000_temp/67-KABOOM-BOOOYAH/templates/contact.gohtml b/000_temp/67-KABOOM-BOOOYAH/templates/contact.gohtml new file mode 100644 index 00000000..758bb847 --- /dev/null +++ b/000_temp/67-KABOOM-BOOOYAH/templates/contact.gohtml @@ -0,0 +1 @@ +hello from contact \ No newline at end of file diff --git a/000_temp/67-KABOOM-BOOOYAH/templates/default.gohtml b/000_temp/67-KABOOM-BOOOYAH/templates/default.gohtml new file mode 100644 index 00000000..59000559 --- /dev/null +++ b/000_temp/67-KABOOM-BOOOYAH/templates/default.gohtml @@ -0,0 +1 @@ +hello from default \ No newline at end of file diff --git a/000_temp/68-sql-group-having/having.sql b/000_temp/68-sql-group-having/having.sql new file mode 100644 index 00000000..e6c9f983 --- /dev/null +++ b/000_temp/68-sql-group-having/having.sql @@ -0,0 +1,62 @@ +CREATE DATABASE music; + +\c music + +CREATE TABLE musicians (mid INT PRIMARY KEY NOT NULL, mname TEXT NOT NULL); + +INSERT INTO musicians VALUES (1, 'Dave Mustang'), (2, 'Steven Tyler'), (3, 'Sam Smith'), (4, 'Sarah McLachlan'), (5, 'Bono'), (6, 'Steve Miller'), (7, 'Bruce Springsteen'); + +CREATE TABLE bands (bid INT PRIMARY KEY NOT NULL, bname TEXT NOT NULL); + +INSERT INTO bands VALUES (1, 'Aerosmith'), (2, 'U2'), (3, 'Ghost'), (4, 'E Street'), (5, 'Metallica'), (6, 'Megadeath'); + +CREATE TABLE bandmembers (bmid INT PRIMARY KEY NOT NULL, bid INT REFERENCES bands(bid) NOT NULL, mid INT REFERENCES musicians(mid) NOT NULL); + +INSERT INTO bandmembers VALUES (1,1,1), (2,2,2), (3,3,3), (4,4,4), (5,5,5), (6,5,7), (7,4,4), (8,3,3); + +CREATE TABLE styles (sid INT PRIMARY KEY NOT NULL, sname TEXT NOT NULL); + +INSERT INTO styles VALUES (1,'jazz'), (2,'metal'), (3,'rock'), (4,'country'), (5,'pop'), (6,'folk'); + +CREATE TABLE bandstyles (bsid INT PRIMARY KEY NOT NULL, bid INT REFERENCES bands(bid) NOT NULL, sid INT REFERENCES styles(sid) NOT NULL); + +INSERT INTO bandstyles VALUES (1,1,1), (2,2,2), (3,3,3), (4,4,4), (5,5,5), (6,4,4), (7,3,3); + +------------ queries --------------- + + +SELECT m.mname, b.bname, s.sname FROM (((musicians AS m FULL JOIN bandmembers AS bm ON m.mid = bm.mid) FULL JOIN bands AS b ON bm.bid = b.bid) FULL JOIN bandstyles AS bs ON b.bid = bs.bid) FULL JOIN styles AS s ON bs.sid = s.sid; + + +/* generate more band members */ +https://play.golang.org/p/uv2g4Ap21Sl + +INSERT INTO bandmembers VALUES (9,2,3),(10,3,5),(11,2,4),(12,1,1),(13,2,1),(14,5,2),(15,3,5),(16,4,5),(17,2,1),(18,3,2),(19,1,2),(20,4,4),(21,3,3),(22,3,4),(23,1,1),(24,2,4),(25,3,2),(26,5,2),(27,3,2),(28,1,2),(29,4,1),(30,5,4),(31,4,3),(32,4,5),(33,5,4),(34,3,2),(35,5,5),(36,1,1),(37,4,4),(38,4,1),(39,2,1),(40,1,2); + + +SELECT bname, sname FROM (bands AS b LEFT JOIN bandstyles AS bs ON b.bid = bs.bid) FULL JOIN styles AS s ON bs.sid = s.sid; + +SELECT m.mname, b.bname FROM (musicians AS m LEFT JOIN bandmembers AS bm ON m.mid = bm.mid) LEFT JOIN bands AS b ON bm.bid = b.bid; + +SELECT m.mname, b.bname FROM (musicians AS m LEFT JOIN bandmembers AS bm ON m.mid = bm.mid) LEFT JOIN bands AS b ON bm.bid = b.bid GROUP BY m.mname, b.bname; + +SELECT m.mid, m.mname, b.bname FROM (musicians AS m LEFT JOIN bandmembers AS bm ON m.mid = bm.mid) LEFT JOIN bands AS b ON bm.bid = b.bid GROUP BY m.mid, m.mname, b.bname; + +/* does not work*/ +SELECT b.bname, s.sname, COUNT(*) AS members FROM (((musicians AS m JOIN bandmembers AS bm ON m.mid = bm.mid) JOIN bands AS b ON bm.bid = b.bid) JOIN bandstyles AS bs ON b.bid = bs.bid) JOIN styles AS s ON bs.sid = s.sid; + +SELECT COUNT(*) AS total FROM musicians; + +/* does not work*/ +SELECT COUNT(*) AS total FROM musicians GROUP BY mname; + +SELECT mname, COUNT(*) AS total FROM musicians GROUP BY mname; + +SELECT b.bname, s.sname, COUNT(*) AS members FROM (((musicians AS m JOIN bandmembers AS bm ON m.mid = bm.mid) JOIN bands AS b ON bm.bid = b.bid) JOIN bandstyles AS bs ON b.bid = bs.bid) JOIN styles AS s ON bs.sid = s.sid GROUP BY b.bname, s.sname; + +SELECT b.bname, s.sname, COUNT(*) AS members FROM (((musicians AS m JOIN bandmembers AS bm ON m.mid = bm.mid) JOIN bands AS b ON bm.bid = b.bid) JOIN bandstyles AS bs ON b.bid = bs.bid) JOIN styles AS s ON bs.sid = s.sid GROUP BY b.bname, s.sname HAVING COUNT(*) > 10; + +SELECT b.bname, s.sname, COUNT(bm.bmid) AS bandmembers FROM (((musicians AS m JOIN bandmembers AS bm ON m.mid = bm.mid) JOIN bands AS b ON bm.bid = b.bid) JOIN bandstyles AS bs ON b.bid = bs.bid) JOIN styles AS s ON bs.sid = s.sid GROUP BY b.bname, s.sname; + +SELECT b.bname, s.sname, COUNT(bm.bmid) AS bandmembers FROM (((musicians AS m JOIN bandmembers AS bm ON m.mid = bm.mid) JOIN bands AS b ON bm.bid = b.bid) JOIN bandstyles AS bs ON b.bid = bs.bid) JOIN styles AS s ON bs.sid = s.sid GROUP BY b.bname, s.sname HAVING COUNT(*) > 10; + diff --git a/000_temp/69-review-golang/main.go b/000_temp/69-review-golang/main.go new file mode 100644 index 00000000..79853a7c --- /dev/null +++ b/000_temp/69-review-golang/main.go @@ -0,0 +1,28 @@ +package main + +import ( + "html/template" + "net/http" +) + +var tpl *template.Template + +func init() { + tpl = template.Must(template.ParseGlob("templates/*.gohtml")) +} + +func main() { + + http.HandleFunc("/", def) + http.HandleFunc("/about", abo) + http.Handle("/foobar", http.NotFoundHandler()) + http.ListenAndServe(":8080", nil) +} + +func def(w http.ResponseWriter, r *http.Request) { + tpl.ExecuteTemplate(w, "default.gohtml", nil) +} + +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/69-review-golang/templates/about.gohtml b/000_temp/69-review-golang/templates/about.gohtml new file mode 100644 index 00000000..6b37e571 --- /dev/null +++ b/000_temp/69-review-golang/templates/about.gohtml @@ -0,0 +1,17 @@ + + + + + + + ABOUT + + + + +

HOME

+

ABOUT

+ + + \ No newline at end of file diff --git a/000_temp/69-review-golang/templates/default.gohtml b/000_temp/69-review-golang/templates/default.gohtml new file mode 100644 index 00000000..844d9eee --- /dev/null +++ b/000_temp/69-review-golang/templates/default.gohtml @@ -0,0 +1,16 @@ + + + + + + + HOME + + + +

HOME

+

ABOUT

+ + + \ No newline at end of file diff --git a/000_temp/70-sql/having.sql b/000_temp/70-sql/having.sql new file mode 100644 index 00000000..65fe4536 --- /dev/null +++ b/000_temp/70-sql/having.sql @@ -0,0 +1,96 @@ +SELECT m.mname, b.bname, s.sname FROM (((musicians AS m FULL JOIN bandmembers AS bm ON m.mid = bm.mid) FULL JOIN bands AS b ON bm.bid = b.bid) FULL JOIN bandstyles AS bs ON b.bid = bs.bid) FULL JOIN styles AS s ON bs.sid = s.sid; + +SELECT s.sname, COUNT(b.bname) AS bands FROM (((musicians AS m FULL JOIN bandmembers AS bm ON m.mid = bm.mid) FULL JOIN bands AS b ON bm.bid = b.bid) FULL JOIN bandstyles AS bs ON b.bid = bs.bid) FULL JOIN styles AS s ON bs.sid = s.sid GROUP BY s.sname; + +SELECT s.sname, COUNT(DISTINCT(b.bname)) AS bands FROM (((musicians AS m FULL JOIN bandmembers AS bm ON m.mid = bm.mid) FULL JOIN bands AS b ON bm.bid = b.bid) FULL JOIN bandstyles AS bs ON b.bid = bs.bid) FULL JOIN styles AS s ON bs.sid = s.sid GROUP BY s.sname; + +SELECT DISTINCT(b.bname) AS name FROM bands AS b; + +SELECT DISTINCT(bname) FROM bands; + +SELECT * FROM bands; + +INSERT INTO bands VALUES (7, 'Madonna'); +INSERT INTO bands VALUES (8, 'Madonna'); + +SELECT b.bname, s.sname FROM (bands AS b FULL JOIN bandstyles AS bs ON b.bid = bs.bid) FULL JOIN styles AS s ON bs.sid = s.sid; + +INSERT INTO bandstyles VALUES (8,5,2), (9,4,1), (10,3,2), (11,2,3), (12,1,4), (13,1,5), (14,1,6); + +SELECT m.mname, b.bname, s.sname FROM (((musicians AS m FULL JOIN bandmembers AS bm ON m.mid = bm.mid) FULL JOIN bands AS b ON bm.bid = b.bid) FULL JOIN bandstyles AS bs ON b.bid = bs.bid) FULL JOIN styles AS s ON bs.sid = s.sid; + +SELECT s.sname, COUNT(b.bname) AS bands FROM (bands AS b FULL JOIN bandstyles AS bs ON b.bid = bs.bid) FULL JOIN styles AS s ON bs.sid = s.sid GROUP BY s.sname; + +SELECT s.sname, COUNT(m.mname) AS musicians FROM (((musicians AS m FULL JOIN bandmembers AS bm ON m.mid = bm.mid) FULL JOIN bands AS b ON bm.bid = b.bid) FULL JOIN bandstyles AS bs ON b.bid = bs.bid) FULL JOIN styles AS s ON bs.sid = s.sid GROUP BY s.sname; + +SELECT s.sname, COUNT(DISTINCT(b.bname)) AS bands FROM (bands AS b FULL JOIN bandstyles AS bs ON b.bid = bs.bid) FULL JOIN styles AS s ON bs.sid = s.sid GROUP BY s.sname; + +SELECT bname, sname FROM (bands AS b FULL JOIN bandstyles AS bs ON b.bid = bs.bid) FULL JOIN styles AS s ON bs.sid = s.sid; + +SELECT sname, bname FROM (bands AS b FULL JOIN bandstyles AS bs ON b.bid = bs.bid) FULL JOIN styles AS s ON bs.sid = s.sid ORDER BY bname; + +SELECT DISTINCT bname, sname AS bands FROM (bands AS b FULL JOIN bandstyles AS bs ON b.bid = bs.bid) FULL JOIN styles AS s ON bs.sid = s.sid ORDER BY bname; + +SELECT DISTINCT sname, bname AS bands FROM (bands AS b FULL JOIN bandstyles AS bs ON b.bid = bs.bid) FULL JOIN styles AS s ON bs.sid = s.sid ORDER BY bname; + +SELECT DISTINCT sname, COUNT(bname) AS bands FROM (bands AS b FULL JOIN bandstyles AS bs ON b.bid = bs.bid) FULL JOIN styles AS s ON bs.sid = s.sid GROUP BY sname; + +SELECT sname, COUNT(bname) AS bands FROM (bands AS b FULL JOIN bandstyles AS bs ON b.bid = bs.bid) FULL JOIN styles AS s ON bs.sid = s.sid GROUP BY sname; + +SELECT DISTINCT sname, bname FROM (bands AS b FULL JOIN bandstyles AS bs ON b.bid = bs.bid) FULL JOIN styles AS s ON bs.sid = s.sid; + +SELECT sname, bname FROM (bands AS b FULL JOIN bandstyles AS bs ON b.bid = bs.bid) FULL JOIN styles AS s ON bs.sid = s.sid; + +SELECT sname, COUNT(bname) as bands FROM (bands AS b FULL JOIN bandstyles AS bs ON b.bid = bs.bid) FULL JOIN styles AS s ON bs.sid = s.sid GROUP BY sname; + +SELECT DISTINCT sname, COUNT(bname) as bands FROM (bands AS b FULL JOIN bandstyles AS bs ON b.bid = bs.bid) FULL JOIN styles AS s ON bs.sid = s.sid GROUP BY sname; + + + + + + + + + + + + + + + + + + +/* generate more band members */ +https://play.golang.org/p/uv2g4Ap21Sl + +INSERT INTO bandmembers VALUES (9,2,3),(10,3,5),(11,2,4),(12,1,1),(13,2,1),(14,5,2),(15,3,5),(16,4,5),(17,2,1),(18,3,2),(19,1,2),(20,4,4),(21,3,3),(22,3,4),(23,1,1),(24,2,4),(25,3,2),(26,5,2),(27,3,2),(28,1,2),(29,4,1),(30,5,4),(31,4,3),(32,4,5),(33,5,4),(34,3,2),(35,5,5),(36,1,1),(37,4,4),(38,4,1),(39,2,1),(40,1,2); + + +SELECT bname, sname FROM (bands AS b LEFT JOIN bandstyles AS bs ON b.bid = bs.bid) FULL JOIN styles AS s ON bs.sid = s.sid; + +SELECT m.mname, b.bname FROM (musicians AS m LEFT JOIN bandmembers AS bm ON m.mid = bm.mid) LEFT JOIN bands AS b ON bm.bid = b.bid; + +SELECT m.mname, b.bname FROM (musicians AS m LEFT JOIN bandmembers AS bm ON m.mid = bm.mid) LEFT JOIN bands AS b ON bm.bid = b.bid GROUP BY m.mname, b.bname; + +SELECT m.mid, m.mname, b.bname FROM (musicians AS m LEFT JOIN bandmembers AS bm ON m.mid = bm.mid) LEFT JOIN bands AS b ON bm.bid = b.bid GROUP BY m.mid, m.mname, b.bname; + +/* does not work*/ +SELECT b.bname, s.sname, COUNT(*) AS members FROM (((musicians AS m JOIN bandmembers AS bm ON m.mid = bm.mid) JOIN bands AS b ON bm.bid = b.bid) JOIN bandstyles AS bs ON b.bid = bs.bid) JOIN styles AS s ON bs.sid = s.sid; + +SELECT COUNT(*) AS total FROM musicians; + +/* does not work*/ +SELECT COUNT(*) AS total FROM musicians GROUP BY mname; + +SELECT mname, COUNT(*) AS total FROM musicians GROUP BY mname; + +SELECT b.bname, s.sname, COUNT(*) AS members FROM (((musicians AS m JOIN bandmembers AS bm ON m.mid = bm.mid) JOIN bands AS b ON bm.bid = b.bid) JOIN bandstyles AS bs ON b.bid = bs.bid) JOIN styles AS s ON bs.sid = s.sid GROUP BY b.bname, s.sname; + +SELECT b.bname, s.sname, COUNT(*) AS members FROM (((musicians AS m JOIN bandmembers AS bm ON m.mid = bm.mid) JOIN bands AS b ON bm.bid = b.bid) JOIN bandstyles AS bs ON b.bid = bs.bid) JOIN styles AS s ON bs.sid = s.sid GROUP BY b.bname, s.sname HAVING COUNT(*) > 10; + +SELECT b.bname, s.sname, COUNT(bm.bmid) AS bandmembers FROM (((musicians AS m JOIN bandmembers AS bm ON m.mid = bm.mid) JOIN bands AS b ON bm.bid = b.bid) JOIN bandstyles AS bs ON b.bid = bs.bid) JOIN styles AS s ON bs.sid = s.sid GROUP BY b.bname, s.sname; + +SELECT b.bname, s.sname, COUNT(bm.bmid) AS bandmembers FROM (((musicians AS m JOIN bandmembers AS bm ON m.mid = bm.mid) JOIN bands AS b ON bm.bid = b.bid) JOIN bandstyles AS bs ON b.bid = bs.bid) JOIN styles AS s ON bs.sid = s.sid GROUP BY b.bname, s.sname HAVING COUNT(*) > 10; + diff --git a/000_temp/71/assets/city.jpeg b/000_temp/71/assets/city.jpeg new file mode 100644 index 00000000..88934e1c Binary files /dev/null and b/000_temp/71/assets/city.jpeg differ diff --git a/000_temp/71/assets/main.css b/000_temp/71/assets/main.css new file mode 100644 index 00000000..fb78b132 --- /dev/null +++ b/000_temp/71/assets/main.css @@ -0,0 +1,38 @@ +html, body { + padding: 0; + border: 0; + margin: 0; + box-sizing: border-box; +} + +#atf { + height: 100vh; + background-color: cornflowerblue; + background-image: url("http://github.com/stuff/city.jpeg"); + background-repeat: no-repeat; + background-size: cover; + background-position: center; + display: flex; + justify-content: center; + align-items: center; + font-size: 13vw; + color: white; +} + +header { + position: fixed; + top: 0; + left: 0; + background-color: rgba(97, 115, 255, 0.44); + height: 5vh; + width: 100vw; + display: flex; + justify-content: space-between; + align-items: center; +} + +header > div { + font-size: 1.5rem; + color: white; + margin: 0 2rem; +} diff --git a/000_temp/71/assets/toby.jpg b/000_temp/71/assets/toby.jpg new file mode 100644 index 00000000..16fe5330 Binary files /dev/null and b/000_temp/71/assets/toby.jpg differ diff --git a/000_temp/71/main.go b/000_temp/71/main.go new file mode 100644 index 00000000..dc7e34b2 --- /dev/null +++ b/000_temp/71/main.go @@ -0,0 +1,27 @@ +package main + +import ( + "html/template" + "net/http" +) + +var tpl *template.Template + +func init() { + tpl = template.Must(template.ParseGlob("templates/*.gohtml")) +} + +func main() { + http.HandleFunc("/", hom) + http.HandleFunc("/about", abo) + http.Handle("/stuff/", http.StripPrefix("/stuff", http.FileServer(http.Dir("./assets/")))) + http.ListenAndServe(":8080", nil) +} + +func hom(w http.ResponseWriter, r *http.Request) { + tpl.ExecuteTemplate(w, "default.gohtml", nil) +} + +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/templates/about.gohtml b/000_temp/71/templates/about.gohtml new file mode 100644 index 00000000..510b8af5 --- /dev/null +++ b/000_temp/71/templates/about.gohtml @@ -0,0 +1,19 @@ + + + + + + + Legal Firm ABOUT Page + + + +

ABOUT

+ +HOME +ABOUT + + + + \ No newline at end of file diff --git a/000_temp/71/templates/default.gohtml b/000_temp/71/templates/default.gohtml new file mode 100644 index 00000000..9298761c --- /dev/null +++ b/000_temp/71/templates/default.gohtml @@ -0,0 +1,33 @@ + + + + + + + Legal Firm Home Page + + + + +
+
left
+
right
+
+ +
+ +CHICAGO + +
+ +

HOME

+ +HOME +ABOUT + + + + + + \ No newline at end of file diff --git a/000_temp/72/assets/toby.jpg b/000_temp/72/assets/toby.jpg new file mode 100644 index 00000000..16fe5330 Binary files /dev/null and b/000_temp/72/assets/toby.jpg differ diff --git a/000_temp/72/main.go b/000_temp/72/main.go new file mode 100644 index 00000000..b3795881 --- /dev/null +++ b/000_temp/72/main.go @@ -0,0 +1,27 @@ +package main + +import ( + "html/template" + "net/http" +) + +var tpl *template.Template + +func init() { + tpl = template.Must(template.ParseGlob("templates/*.gohtml")) +} + +func main() { + http.HandleFunc("/", foo) + http.HandleFunc("/about", bar) + http.Handle("/resources/", http.StripPrefix("/resources", http.FileServer(http.Dir("./assets")))) + http.ListenAndServe(":8080", nil) +} + +func foo(w http.ResponseWriter, r *http.Request) { + tpl.ExecuteTemplate(w, "index.gohtml", nil) +} + +func bar(w http.ResponseWriter, r *http.Request) { + tpl.ExecuteTemplate(w, "about.gohtml", nil) +} diff --git a/000_temp/72/templates/about.gohtml b/000_temp/72/templates/about.gohtml new file mode 100644 index 00000000..58bd92b0 --- /dev/null +++ b/000_temp/72/templates/about.gohtml @@ -0,0 +1,18 @@ + + + + + + + ABOUT + + + +

ABOUT

+ +HOME +ABOUT + + + \ No newline at end of file diff --git a/000_temp/72/templates/index.gohtml b/000_temp/72/templates/index.gohtml new file mode 100644 index 00000000..d0c02408 --- /dev/null +++ b/000_temp/72/templates/index.gohtml @@ -0,0 +1,20 @@ + + + + + + + HOME + + + +

HOME

+ +HOME +ABOUT + + + + + \ No newline at end of file diff --git a/000_temp/73/01/main.go b/000_temp/73/01/main.go new file mode 100644 index 00000000..8fd45780 --- /dev/null +++ b/000_temp/73/01/main.go @@ -0,0 +1,27 @@ +package main + +import "fmt" + + +// a VALUE of TYPE + +var x int +var s string +var b bool +var i float64 + +func main() { + x = 42 + s = "James" + b = true + i = 42.123 + fmt.Println(x, s, b, i) + + f := 43 + g := "Bond" + h := false + j := 43.342 + 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 new file mode 100644 index 00000000..f6b872e7 --- /dev/null +++ b/000_temp/73/02/main.go @@ -0,0 +1,28 @@ +package main + +import "fmt" + +// AGGREGATE or COMPOSITE +func main() { + xi := []int{4,5,6,7,42} + + fmt.Println(xi) + + for p, v := range xi { + fmt.Println(p, v) + } + + // FOR declaration; condition; post {} + n := 42 + for { + fmt.Println(n) + n++ + if n == 1000 { + break + } + } + + for i := 1000; i <= 2000; i++ { + fmt.Println(i) + } +} diff --git a/000_temp/73/03/main.go b/000_temp/73/03/main.go new file mode 100644 index 00000000..8b568604 --- /dev/null +++ b/000_temp/73/03/main.go @@ -0,0 +1,13 @@ +package main + +import "fmt" + +type hotdog int + +var hd hotdog + +func main() { + hd = 42 + fmt.Println(hd) + fmt.Printf("%T\n", hd) +} diff --git a/000_temp/73/04/main.go b/000_temp/73/04/main.go new file mode 100644 index 00000000..6b2ef70d --- /dev/null +++ b/000_temp/73/04/main.go @@ -0,0 +1,31 @@ +package main + +import "fmt" + +type person struct { + first string + age int +} + +type secretAgent struct { + person + ltk bool +} + +func main() { + p := person{ + first: "Miss Moneypenny", + age: 27, + } + + sa := secretAgent { + person: person { + first: "James", + age: 32, + }, + ltk: true, + } + + fmt.Println(p) + fmt.Println(sa) +} \ No newline at end of file diff --git a/000_temp/73/05/main.go b/000_temp/73/05/main.go new file mode 100644 index 00000000..fba21fb7 --- /dev/null +++ b/000_temp/73/05/main.go @@ -0,0 +1,19 @@ +package main + +import "fmt" + +func main() { + x := 42 + fmt.Println("x", x) + fmt.Println(&x) + + y := &x + + fmt.Printf("%v\t %T\n", x, x) + fmt.Printf("%v\t %T\n", y, y) + fmt.Println(*y) + + *y = 100 + fmt.Println("y", *y) + fmt.Println("x", x) +} diff --git a/000_temp/73/06/main.go b/000_temp/73/06/main.go new file mode 100644 index 00000000..fb9b32f5 --- /dev/null +++ b/000_temp/73/06/main.go @@ -0,0 +1,26 @@ +package main + +import "fmt" + +func main() { + a := 42 + fmt.Println("a", a) + + foo(a) + fmt.Println("a after foo", a) + + bar(&a) + fmt.Println("a after bar", a) +} + + +func foo(b int) { + b = 43 + fmt.Println("b", b) +} + +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/74/index.html b/000_temp/74/index.html new file mode 100644 index 00000000..bae7e1cd --- /dev/null +++ b/000_temp/74/index.html @@ -0,0 +1,43 @@ + + + + + + Document + + + + +

HELLO WORLD

+ +

Lorem ipsum dolor sit amet, consectetur adipisicing elit. A atque autem, consectetur deleniti dolor expedita fugiat + fugit harum illum iste itaque maiores maxime non obcaecati omnis quam ratione recusandae suscipit.

+ + + + + + + + + \ No newline at end of file diff --git a/000_temp/75/main.go b/000_temp/75/main.go new file mode 100644 index 00000000..d2803717 --- /dev/null +++ b/000_temp/75/main.go @@ -0,0 +1,76 @@ +package main + +import ( + "html/template" + "net/http" +) + +const s = ` + + + + + + CONTACT 4 + + + +

YOU ARE AT CONTACT 4

+ +HOME +ABOUT +CONTACT 2 +CONTACT 3 +CONTACT 4 + + + + +` + +var tpl *template.Template +var tpl2 *template.Template +var tpl3 *template.Template +var tpl4 *template.Template + +func init() { + tpl = template.Must(template.ParseGlob("templates/*.gohtml")) + + tpl2 = template.Must(template.New("pale").ParseFiles("templates/contact.gohtml")) + + tpl3 = template.Must(template.ParseFiles("templates/contact.gohtml")) + + tpl4 = template.Must(template.New("pale").Parse(s)) +} + +func main() { + http.HandleFunc("/", foo) + http.HandleFunc("/about", bar) + http.HandleFunc("/contact/2", con2) + http.HandleFunc("/contact/3", con3) + http.HandleFunc("/contact/4", con4) + http.ListenAndServe(":8080", nil) +} + +func foo(w http.ResponseWriter, r *http.Request) { + tpl.ExecuteTemplate(w, "index.gohtml", nil) +} + +func bar(w http.ResponseWriter, r *http.Request) { + tpl.ExecuteTemplate(w, "about.gohtml", nil) +} + +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/75/templates/about.gohtml b/000_temp/75/templates/about.gohtml new file mode 100644 index 00000000..859035fb --- /dev/null +++ b/000_temp/75/templates/about.gohtml @@ -0,0 +1,23 @@ + + + + + + + ABOUT + + + +

YOU ARE AT ABOUT

+ +HOME +ABOUT +CONTACT 2 +CONTACT 3 +CONTACT 4 + + + + + \ No newline at end of file diff --git a/000_temp/75/templates/contact.gohtml b/000_temp/75/templates/contact.gohtml new file mode 100644 index 00000000..36b38bf8 --- /dev/null +++ b/000_temp/75/templates/contact.gohtml @@ -0,0 +1,21 @@ + + + + + + + HOME + + + +

YOU ARE AT CONTACT

+ +HOME +ABOUT +CONTACT 2 +CONTACT 3 +CONTACT 4 + + + \ No newline at end of file diff --git a/000_temp/75/templates/index.gohtml b/000_temp/75/templates/index.gohtml new file mode 100644 index 00000000..b916743e --- /dev/null +++ b/000_temp/75/templates/index.gohtml @@ -0,0 +1,22 @@ + + + + + + + HOME + + + +

YOU ARE AT HOME

+ +HOME +ABOUT +CONTACT 2 +CONTACT 3 +CONTACT 4 + + + + \ No newline at end of file diff --git a/000_temp/76 sql/sales.sql b/000_temp/76 sql/sales.sql new file mode 100644 index 00000000..18ada146 --- /dev/null +++ b/000_temp/76 sql/sales.sql @@ -0,0 +1,54 @@ +CREATE DATABASE sales; + +\l + +\c sales + +CREATE TABLE employees (eid INT PRIMARY KEY NOT NULL, ename TEXT NOT NULL); + +CREATE TABLE states (stid INT PRIMARY KEY NOT NULL, stname TEXT NOT NULL); + +CREATE TABLE cities (cid INT PRIMARY KEY NOT NULL, cname TEXT NOT NULL); + +CREATE TABLE sales (sid INT PRIMARY KEY NOT NULL, eid INT REFERENCES employees(eid) NOT NULL, cid INT REFERENCES cities(cid) NOT NULL, stid INT REFERENCES states(stid) NOT NULL, samount INT NOT NULL); + +INSERT INTO employees VALUES (1,'James'), (2,'Moneypenny'), (3,'Ian'), (4,'Fleming'), (5,'Bond'); + +INSERT INTO states VALUES (1,'AL'), (2,'AK'), (3,'AZ'), (4,'AR'), (5,'CA'); + +INSERT INTO cities VALUES (1,'Montgomery'), (2,'Anchorage'), (3,'Phoenix'), (4,'Little Rock'), (5,'Sacramento'), (6,'Fresno'), (7,'Benicia'), (8,'San Fran'), (9,'LA'), (10,'San Diego'); + +https://play.golang.org/p/knG5I2AAi79 + +INSERT INTO sales VALUES (1,3,2,2,1948), (2,2,10,5,1419), (3,1,6,5,557), (4,5,1,1,3612), (5,5,3,3,4829), (6,2,5,5,1546), (7,2,8,5,596), (8,4,7,5,1359), (9,3,8,5,3388), (10,1,9,5,3116), (11,4,2,2,2488), (12,5,2,2,457), (13,2,8,5,1586), (14,4,7,5,3191), (15,4,5,5,2534), (16,4,8,5,4425), (17,4,10,5,2058), (18,5,2,2,2300), (19,1,1,1,2989), (20,4,9,5,4456), (21,1,2,2,2706), (22,2,7,5,4929), (23,3,2,2,4884), (24,4,7,5,4477), (25,4,3,3,548), (26,3,5,5,2564), (27,1,7,5,3724), (28,3,4,4,3234), (29,5,2,2,3134), (30,2,4,4,2103), (31,2,9,5,2647), (32,1,8,5,1604), (33,4,3,3,2306), (34,1,9,5,1452), (35,3,6,5,3788), (36,1,1,1,386), (37,3,1,1,3199), (38,2,4,4,3683), (39,3,5,5,4368), (40,2,8,5,995), (41,3,7,5,4082), (42,2,10,5,1371), (43,2,4,4,4920), (44,3,2,2,2276), (45,1,6,5,1488), (46,4,10,5,2919), (47,4,5,5,1325), (48,3,8,5,1633), (49,5,7,5,641), (50,2,7,5,3177), (51,2,1,1,3945), (52,1,5,5,4284), (53,1,2,2,1703), (54,3,9,5,3332), (55,5,9,5,2923), (56,3,4,4,4309), (57,4,4,4,1267), (58,1,1,1,541), (59,3,5,5,4758), (60,2,6,5,3140), (61,4,1,1,4801), (62,1,10,5,884), (63,5,1,1,3348), (64,1,1,1,4263), (65,1,10,5,2149), (66,1,7,5,3767), (67,2,1,1,1730), (68,2,3,3,2678), (69,1,7,5,500), (70,3,3,3,393), (71,2,9,5,4204), (72,4,9,5,3857), (73,3,10,5,1258), (74,1,3,3,2282), (75,1,4,4,518), (76,1,4,4,4097), (77,3,7,5,2621), (78,3,1,1,2130), (79,1,2,2,2521), (80,5,10,5,1565), (81,2,1,1,1282), (82,2,8,5,701), (83,3,10,5,1634), (84,5,2,2,1786), (85,1,10,5,1820), (86,1,5,5,3763), (87,1,1,1,4385), (88,1,4,4,1544), (89,1,2,2,675), (90,1,10,5,1437), (91,3,9,5,1645), (92,5,6,5,1338), (93,4,5,5,1707), (94,3,9,5,2521), (95,2,3,3,1218), (96,1,2,2,3783), (97,3,10,5,230), (98,2,2,2,956), (99,4,2,2,1154), (100,5,7,5,4968); + +SELECT e.ename, c.cname, st.stname, s.samount FROM ((employees AS e JOIN sales AS s ON e.eid = s.eid) JOIN cities AS c ON s.cid = c.cid) JOIN states AS st ON s.stid = st.stid; + + + +SELECT e.ename, c.cname, st.stname, s.samount FROM ((employees AS e JOIN sales AS s ON e.eid = s.eid) JOIN cities AS c ON s.cid = c.cid) JOIN states AS st ON s.stid = st.stid WHERE e.eid = 1; + + +// does not work +SELECT e.ename, c.cname, st.stname, s.samount FROM (((employees AS e WHERE e.eid = 1) JOIN sales AS s ON e.eid = s.eid) JOIN cities AS c ON s.cid = c.cid) JOIN states AS st ON s.stid = st.stid; + +// works +SELECT e.ename FROM employees AS e WHERE e.eid = 1; + +SELECT e.ename, c.cname, st.stname, SUM(s.samount) FROM ((employees AS e JOIN sales AS s ON e.eid = s.eid) JOIN cities AS c ON s.cid = c.cid) JOIN states AS st ON s.stid = st.stid GROUP BY e.ename, c.cname, st.stname; + +SELECT e.ename, c.cname, st.stname, SUM(s.samount) FROM ((employees AS e JOIN sales AS s ON e.eid = s.eid) JOIN cities AS c ON s.cid = c.cid) JOIN states AS st ON s.stid = st.stid WHERE e.eid = 1 GROUP BY e.ename, c.cname, st.stname; + +SELECT e.ename, c.cname, st.stname, SUM(s.samount) FROM ((employees AS e JOIN sales AS s ON e.eid = s.eid) JOIN cities AS c ON s.cid = c.cid) JOIN states AS st ON s.stid = st.stid GROUP BY e.ename, c.cname, st.stname ORDER BY e.ename; + +SELECT e.ename, c.cname, st.stname, SUM(s.samount) FROM ((employees AS e JOIN sales AS s ON e.eid = s.eid) JOIN cities AS c ON s.cid = c.cid) JOIN states AS st ON s.stid = st.stid WHERE e.eid = 1 GROUP BY e.ename, c.cname, st.stname HAVING st.stname = 'CA'; + +SELECT e.ename, c.cname, st.stname, SUM(s.samount) FROM ((employees AS e JOIN sales AS s ON e.eid = s.eid) JOIN cities AS c ON s.cid = c.cid) JOIN states AS st ON s.stid = st.stid WHERE e.eid = 1 AND st.stname = 'CA' GROUP BY e.ename, c.cname, st.stname; + + +SELECT e.ename, c.cname, st.stname, SUM(s.samount) FROM ((employees AS e JOIN sales AS s ON e.eid = s.eid) JOIN cities AS c ON s.cid = c.cid) JOIN states AS st ON s.stid = st.stid GROUP BY e.ename, c.cname, st.stname HAVING e.ename = 'James' AND st.stname = 'CA'; + +// does not work +SELECT e.ename, c.cname, st.stname, SUM(s.samount) FROM ((employees AS e JOIN sales AS s ON e.eid = s.eid) JOIN cities AS c ON s.cid = c.cid) JOIN states AS st ON s.stid = st.stid GROUP BY e.ename, c.cname, st.stname HAVING e.eid = 1 AND st.stname = 'CA'; + + diff --git a/000_temp/77-web-server/assets/toby.jpg b/000_temp/77-web-server/assets/toby.jpg new file mode 100644 index 00000000..16fe5330 Binary files /dev/null and b/000_temp/77-web-server/assets/toby.jpg differ diff --git a/000_temp/77-web-server/main.go b/000_temp/77-web-server/main.go new file mode 100644 index 00000000..be55885b --- /dev/null +++ b/000_temp/77-web-server/main.go @@ -0,0 +1,50 @@ +package main + +import ( + "html/template" + "net/http" +) + +var tpl *template.Template +func init() { + tpl = template.Must(template.ParseGlob("templates/*.gohtml")) +} + +func main() { + http.HandleFunc("/", foo) + http.HandleFunc("/about", bar ) + http.HandleFunc("/contact", con) + http.Handle("/resources/", http.StripPrefix("/resources", http.FileServer(http.Dir("./assets")))) + http.ListenAndServe(":8080", nil) +} + +func foo(w http.ResponseWriter, r *http.Request) { + tpl.ExecuteTemplate(w, "index.gohtml", 42) +} + +func bar(w http.ResponseWriter, r *http.Request) { + 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 + } + + p1 := person{ + First: "James", + Age: 32, + } + + p2 := person { + First: "Jenny", + Age: 27, + } + + xp := []person{p1, p2} + + + tpl.ExecuteTemplate(w, "contact.gohtml", xp) +} diff --git a/000_temp/77-web-server/templates/about.gohtml b/000_temp/77-web-server/templates/about.gohtml new file mode 100644 index 00000000..57cc8dc7 --- /dev/null +++ b/000_temp/77-web-server/templates/about.gohtml @@ -0,0 +1,25 @@ + + + + + + + ABOUT + + + +

ABOUT

+ +HOME +ABOUT +CONTACT + +
+ +{{if .}} +{{.}} +{{end}} + + + \ No newline at end of file diff --git a/000_temp/77-web-server/templates/contact.gohtml b/000_temp/77-web-server/templates/contact.gohtml new file mode 100644 index 00000000..e83f1890 --- /dev/null +++ b/000_temp/77-web-server/templates/contact.gohtml @@ -0,0 +1,33 @@ + + + + + + + CONTACT + + + +

CONTACT

+ +HOME +ABOUT +CONTACT + +
+ +{{if .}} +{{.}} + +{{range .}} +

First: {{.First}}

+

Age: {{.Age}}

+{{end}} +{{end}} + + + + + + \ No newline at end of file diff --git a/000_temp/77-web-server/templates/index.gohtml b/000_temp/77-web-server/templates/index.gohtml new file mode 100644 index 00000000..1aa33933 --- /dev/null +++ b/000_temp/77-web-server/templates/index.gohtml @@ -0,0 +1,25 @@ + + + + + + + HOME + + + +

HOME

+ +HOME +ABOUT +CONTACT + +
+ +{{if .}} +{{.}} +{{end}} + + + \ No newline at end of file diff --git a/000_temp/78/assets/toby.jpg b/000_temp/78/assets/toby.jpg new file mode 100644 index 00000000..16fe5330 Binary files /dev/null and b/000_temp/78/assets/toby.jpg differ diff --git a/000_temp/78/main.go b/000_temp/78/main.go new file mode 100644 index 00000000..70a2a72d --- /dev/null +++ b/000_temp/78/main.go @@ -0,0 +1,57 @@ +package main + +import ( + "net/http" + "fmt" + "html/template" + "os" + "io" +) + +var tpl *template.Template + +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")))) + http.ListenAndServe(":8080", nil) +} + +func foo(w http.ResponseWriter, r *http.Request) { + fmt.Println("Method", r.Method) + + fn := "" + + if r.Method == http.MethodPost { + // open + f, h, err := r.FormFile("q") + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + defer f.Close() + + fn = h.Filename + + df, err := os.Create("./assets/"+fn) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + defer df.Close() + + _, err = io.Copy(df, f) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + } + + // w.Header().Set("Content-Type", "text/html; charset=utf-8") + + tpl.ExecuteTemplate(w, "index.gohtml", fn) +} diff --git a/000_temp/78/templates/index.gohtml b/000_temp/78/templates/index.gohtml new file mode 100644 index 00000000..28ab32a4 --- /dev/null +++ b/000_temp/78/templates/index.gohtml @@ -0,0 +1,24 @@ + + + + + + + Document + + + +
+ + +
+
+ +{{if .}} +file name: {{.}} + +{{end}} + + + \ No newline at end of file diff --git a/000_temp/79/main.go b/000_temp/79/main.go new file mode 100644 index 00000000..c35ff2ea --- /dev/null +++ b/000_temp/79/main.go @@ -0,0 +1,44 @@ +package main + +import ( + "fmt" + "html/template" + "net/http" +) + +var tpl *template.Template + +func init() { + tpl = template.Must(template.ParseGlob("templates/*")) +} + +func main() { + http.HandleFunc("/", foo) + http.HandleFunc("/bar", bar) + http.HandleFunc("/barred", barred) + http.Handle("/favicon.ico", http.NotFoundHandler()) + http.ListenAndServe(":8080", nil) +} + +func foo(w http.ResponseWriter, req *http.Request) { + + var s string + + if req.Method == http.MethodPost { + s = req.FormValue("fname") + } + + fmt.Print("Your request method at foo: ", req.Method, "\n") + fmt.Print("Your FORM VALUE at foo: ", s, "\n\n") +} + +func bar(w http.ResponseWriter, req *http.Request) { + fmt.Println("Your request method at bar:", req.Method) + // process form submission here + http.Redirect(w, req, "/", http.StatusSeeOther) +} + +func barred(w http.ResponseWriter, req *http.Request) { + fmt.Println("Your request method at barred:", req.Method) + tpl.ExecuteTemplate(w, "index.gohtml", nil) +} diff --git a/000_temp/79/templates/index.gohtml b/000_temp/79/templates/index.gohtml new file mode 100644 index 00000000..54c0bbc5 --- /dev/null +++ b/000_temp/79/templates/index.gohtml @@ -0,0 +1,15 @@ + + + + + Title + + + +
+ + +
+ + + \ No newline at end of file diff --git a/000_temp/80-renamer/80-renamer.txt b/000_temp/80-renamer/80-renamer.txt new file mode 100755 index 00000000..9ed28e74 Binary files /dev/null and b/000_temp/80-renamer/80-renamer.txt differ diff --git a/000_temp/80-renamer/main.go b/000_temp/80-renamer/main.go new file mode 100644 index 00000000..513f5ff5 --- /dev/null +++ b/000_temp/80-renamer/main.go @@ -0,0 +1,27 @@ +package main + +import ( + "fmt" + "io/ioutil" + "os" + "strconv" +) + +func main() { + + dir := "results/" + files, _ := ioutil.ReadDir(dir) + + for i, f := range files { + fmt.Println(f.Name()) + oldfile := (dir + f.Name()) + newfile := (dir + strconv.Itoa(i) + ".txt") + fmt.Println(oldfile, "named it to", newfile, "\n") + + err := os.Rename(oldfile, newfile) + if err != nil { + fmt.Println(err) + return + } + } +} \ No newline at end of file diff --git a/000_temp/80-renamer/results/0.txt b/000_temp/80-renamer/results/0.txt new file mode 100644 index 00000000..e69de29b diff --git a/000_temp/80-renamer/results/1.txt b/000_temp/80-renamer/results/1.txt new file mode 100644 index 00000000..e69de29b diff --git a/000_temp/81-cookie-counter/main.go b/000_temp/81-cookie-counter/main.go new file mode 100644 index 00000000..e223f3cf --- /dev/null +++ b/000_temp/81-cookie-counter/main.go @@ -0,0 +1,66 @@ +package main + +import ( + "net/http" + "html/template" + "strconv" + "fmt" +) + +var tpl *template.Template + +func init() { + tpl = template.Must(template.ParseGlob("templates/*.gohtml")) +} + +func main() { + http.HandleFunc("/", foo) + http.HandleFunc("/bar", bar) + http.ListenAndServe(":8080", nil) +} + +func foo(w http.ResponseWriter, r *http.Request) { + c, err := cookieCounter(r) + if err != nil { + http.Error(w, "bad request", http.StatusBadRequest) + return + } + http.SetCookie(w, c) + + tpl.ExecuteTemplate(w, "index.gohtml", c.Value) +} + + +func bar(w http.ResponseWriter, r *http.Request) { + c, err := cookieCounter(r) + if err != nil { + fmt.Println(err) + http.Error(w, "bad request", http.StatusBadRequest) + return + } + http.SetCookie(w, c) + + tpl.ExecuteTemplate(w, "about.gohtml", c.Value) +} + +func cookieCounter(r *http.Request) (*http.Cookie, error) { + var c *http.Cookie + + c, err := r.Cookie("wackadoodle") + if err != nil { + c = &http.Cookie{ + Name: "wackadoodle", + Value: "0", + Path: "/", + } + } + + i, err := strconv.Atoi(c.Value) + if err != nil { + return c, err + } + i++ + c.Value = strconv.Itoa(i) + + return c, nil +} diff --git a/000_temp/81-cookie-counter/templates/about.gohtml b/000_temp/81-cookie-counter/templates/about.gohtml new file mode 100644 index 00000000..d1fc872f --- /dev/null +++ b/000_temp/81-cookie-counter/templates/about.gohtml @@ -0,0 +1,16 @@ + + + + + + + ABOUT + + + +

about

+VALUE OF COOKIE {{.}} + + + \ No newline at end of file diff --git a/000_temp/81-cookie-counter/templates/index.gohtml b/000_temp/81-cookie-counter/templates/index.gohtml new file mode 100644 index 00000000..9302b5ee --- /dev/null +++ b/000_temp/81-cookie-counter/templates/index.gohtml @@ -0,0 +1,16 @@ + + + + + + + Document + + + +

home

+VALUE OF COOKIE {{.}} + + + \ No newline at end of file diff --git a/000_temp/82/main.go b/000_temp/82/main.go new file mode 100644 index 00000000..180f8828 --- /dev/null +++ b/000_temp/82/main.go @@ -0,0 +1,10 @@ +package main + +import "fmt" + +func main() { + var x uint + x = 18446744073709551615 + fmt.Println(x) + fmt.Printf("%T\n\n",x) +} diff --git a/000_temp/83_select/README.md b/000_temp/83_select/README.md new file mode 100644 index 00000000..5af6f981 --- /dev/null +++ b/000_temp/83_select/README.md @@ -0,0 +1,130 @@ +# our pathway + +We will be following the example created in [this article by Alex Edwards](http://www.alexedwards.net/blog/practical-persistence-sql) and licensed under a [MIT license](https://opensource.org/licenses/MIT) + +In order to successfully pull records from a table in a database as our user ```bond```, we will need to [ALTER](https://www.postgresql.org/docs/9.6/static/sql-alteruser.html) bond to have a different role. + +# alter bond's role +``` +alter user bond with superuser; +``` + +# switch to your bookstore database +You should already have a ```bookstore``` database: + +list databases +``` +\l +``` + +switch into that database +``` +\c bookstore +``` + +directory of tables, if any +``` +\d +``` + +# create table +``` +CREATE TABLE books ( + isbn char(14) PRIMARY KEY NOT NULL, + title varchar(255) NOT NULL, + author varchar(255) NOT NULL, + price decimal(5,2) NOT NULL +); +``` + +directory of tables +``` +\d +``` + +details of table ```books``` +``` +\d books +``` + +# insert records +``` +INSERT INTO books (isbn, title, author, price) VALUES +('978-1503261969', 'Emma', 'Jayne Austen', 9.44), +('978-1505255607', 'The Time Machine', 'H. G. Wells', 5.99), +('978-1503379640', 'The Prince', 'Niccolò Machiavelli', 6.99); +``` + +view records +``` +SELECT * FROM books; +``` + +# main.go + +## importing the driver +make sure you import the driver +``` +_ "github.com/lib/pq" +``` +"We don't use anything in the pq package directly, which means that the Go compiler will raise an error if we try to import it normally. But **we need the pq package's init() function to run so that our driver can register itself with database/sql.** We get around this by aliasing the package name to the blank identifier. This means pq.init() still gets executed, but the alias is harmlessly discarded (and our code runs error-free). This approach is standard for most of Go's SQL drivers." - Alex Edwards + +## define a book type struct +``` +type Book struct { + isbn string + title string + author string + price float32 +} +``` +"Next we define a Book type. **The struct fields and their types must align to our books table.** For completeness I should point out that we've only been able to use the string and float32 types safely because we set NOT NULL constraints on the columns in our table. If the table contained nullable fields we would need to use the sql.NullString and sql.NullFloat64 types instead – see [this Gist](https://gist.github.com/alexedwards/dc3145c8e2e6d2fd6cd9) for a working example. Generally it's easiest to avoid nullable fields altogether if you can, which is what we've done here." - Alex Edwards + +## initialize a new sql.DB +``` +db, err := sql.Open("postgres", "postgres://bond:password@localhost/bookstore?sslmode=disable") + if err != nil { + panic(err) + } + defer db.Close() +``` +"In the main() function we initialise a new sql.DB by calling sql.Open(). We pass in the name of our driver (in this case "postgres") and the connection string (you'll need to check your driver documentation for the correct format). It's worth emphasizing that **sql.DB is not a database connection – it's an abstraction representing a pool of underlying connections.** You can change the maximum number of open and idle connections in the pool with the db.SetMaxOpenConns() and db.SetMaxIdleConns() methods respectively. A final thing to note is that **sql.DB is safe for concurrent access,** which is very convenient if you're using it in a web application (like we will shortly)." - Alex Edwards + +## ping the db +``` + if err = db.Ping(); err != nil { + panic(err) + } +``` +"Because sql.Open() doesn't actually check a connection, we also call DB.Ping() to make sure that everything works OK on startup." - Alex Edwards + +## query the db +``` +rows, err := db.Query("SELECT * FROM books") + if err != nil { + panic(err) + } + defer rows.Close() +``` +"We will fetch a resultset from the books table using the DB.Query() method and assign it to a rows variable. Then we defer rows.Close() to ensure the resultset is properly closed before the parent function returns. **Closing a resultset properly is really important. As long as a resultset is open it will keep the underlying database connection open – which in turn means the connection is not available to the pool.** So if something goes wrong and the resultset isn't closed it can rapidly lead to all the connections in your pool being used up. Another gotcha (which caught me out when I first began) is that the defer statement should come after you check for an error from DB.Query. Otherwise, if DB.Query() returns an error, you'll get a panic trying to close a nil resultset." - Alex Edwards + +## iterate through results +``` + for rows.Next() { + bk := Book{} + err := rows.Scan(&bk.isbn, &bk.title, &bk.author, &bk.price) // order matters + if err != nil { + panic(err) + } + bks = append(bks, bk) + } +``` +"We then use rows.Next() to iterate through the rows in the resultset. This preps the first (and then each subsequent) row to be acted on by the rows.Scan() method. Note that if iteration over all of the rows completes then the resultset automatically closes itself and frees-up the connection. We use the rows.Scan() method to copy the values from each field in the row to a new Book object that we created. We then check for any errors that occurred during Scan, and add the new Book to a slice of books." - Alex Edwards + +## make sure everything ran well +``` + if err = rows.Err(); err != nil { + panic(err) + } +``` +"When our rows.Next() loop has finished we call rows.Err(). This returns any error that was encountered during the interation. It's important to call this – don't just assume that we completed a successful iteration over the whole resultset." - Alex Edwards Err returns the error, if any, that was encountered during iteration. Err may be called after an explicit or implicit Close. \ No newline at end of file diff --git a/000_temp/83_select/main.go b/000_temp/83_select/main.go new file mode 100644 index 00000000..0d5a29fb --- /dev/null +++ b/000_temp/83_select/main.go @@ -0,0 +1,26 @@ +package main + +import ( + "database/sql" + "fmt" + _ "github.com/lib/pq" +) + +func main() { + + connstr := "host=localhost port=5432 dbname=bookstore user=bond password=password sslmode=disable" + + // "postgres://bond:password@localhost/bookstore?sslmode=disable" + + db, err := sql.Open("postgres", connstr) + if err != nil { + panic(err) + } + defer db.Close() + + err = db.Ping() + if err != nil { + panic(err) + } + fmt.Println("You connected to your database.") +} diff --git a/000_temp/84-pg-query/main.go b/000_temp/84-pg-query/main.go new file mode 100644 index 00000000..ca56408b --- /dev/null +++ b/000_temp/84-pg-query/main.go @@ -0,0 +1,48 @@ +package main + +import ( + "database/sql" + "fmt" + _ "github.com/lib/pq" +) + +type Book struct { + ISBN string + Title string + Author string + Price float32 +} + +func main() { + connStrn := "host=localhost port=5432 dbname=bookstore user=bond password=password sslmode=disable" + + db, err := sql.Open("postgres", connStrn) + if err != nil { + panic(err) + } + defer db.Close() + + err = db.Ping() + if err != nil { + panic(err) + } + + fmt.Println("Connected to database") + + rs, err := db.Query("select * from books;") + + xb := make([]Book, 0) + for rs.Next() { + b := Book{} + rs.Scan(&b.ISBN, &b.Title, &b.Author, &b.Price) + xb = append(xb, b) + + if err = rs.Err(); err != nil { + 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 new file mode 100644 index 00000000..4711d308 --- /dev/null +++ b/000_temp/85-pg-query-web/main.go @@ -0,0 +1,87 @@ +package main + +import ( + "database/sql" + "fmt" + _ "github.com/lib/pq" + "html/template" + "net/http" +) + +var tpl *template.Template +var db *sql.DB + +func init() { + tpl = template.Must(template.ParseGlob("templates/*.gohtml")) + + connStrn := "host=localhost port=5432 dbname=bookstore user=bond password=password sslmode=disable" + + db, err := sql.Open("postgres", connStrn) + if err != nil { + panic(err) + } + defer db.Close() + + err = db.Ping() + if err != nil { + panic(err) + } + + fmt.Println("Connected to database") +} + +type Book struct { + Isbn string + Title string + Author string + Price float32 +} + +func main() { + http.HandleFunc("/", foo) + http.ListenAndServe(":8080", nil) +} + +func foo(w http.ResponseWriter, r *http.Request) { + rs, err := db.Query("select * from books;") + + xb := make([]Book, 0) + for rs.Next() { + b := Book{} + rs.Scan(&b.Isbn, &b.Title, &b.Author, &b.Price) + xb = append(xb, b) + + if err = rs.Err(); err != nil { + panic(err) + } + } + tpl.ExecuteTemplate(w, "books.gohtml", xb) +} + + + +//rs, err := db.Query("select * from books;") +//if err != nil { +// http.Error(w, http.StatusText(500), 500) +// return +//} +//defer rs.Close() +// +//xb := make([]Book, 0) +//for rs.Next() { +// b := Book{} +// err := rs.Scan(&b.ISBN, &b.Title, &b.Author, &b.Price) +// if err != nil { +// http.Error(w, http.StatusText(500), 500) +// return +// +// } +// xb = append(xb, b) +// +// if err = rs.Err(); err != nil { +// http.Error(w, http.StatusText(500), 500) +// return +// } +//} +// +//tpl.ExecuteTemplate(w, "index.gohtml", xb) diff --git a/000_temp/85-pg-query-web/templates/index.gohtml b/000_temp/85-pg-query-web/templates/index.gohtml new file mode 100644 index 00000000..d6173f62 --- /dev/null +++ b/000_temp/85-pg-query-web/templates/index.gohtml @@ -0,0 +1,17 @@ + + + + + + + Document + + + +{{range .}} +{{.Isbn}} - {{.Title}} - {{.Author}} - {{.Price}} +{{end}} + + + \ No newline at end of file diff --git a/000_temp/86-website/main.go b/000_temp/86-website/main.go new file mode 100644 index 00000000..94b9f53e --- /dev/null +++ b/000_temp/86-website/main.go @@ -0,0 +1,22 @@ +package main + +import ( + "html/template" + "net/http" +) + +var tpl *template.Template + +func init() { + tpl = template.Must(template.ParseGlob("templates/*.gohtml")) +} + +func main() { + http.HandleFunc("/", index) + http.Handle("/assets/", http.StripPrefix("/assets", http.FileServer(http.Dir("./resources")))) + http.ListenAndServe(":8080", nil) +} + +func index(w http.ResponseWriter, r *http.Request) { + tpl.ExecuteTemplate(w, "index.gohtml", nil) +} diff --git a/000_temp/86-website/resources/css/main.css b/000_temp/86-website/resources/css/main.css new file mode 100644 index 00000000..9960c767 --- /dev/null +++ b/000_temp/86-website/resources/css/main.css @@ -0,0 +1,23 @@ +html, body, h1 { + padding: 0; + border: 0; + margin: 0; +} + +h1 { + color: red; + font-size: 10rem; +} + +#atf { + height: 100vh; + background-image: url("http://github.com/assets/img/skyf.jpeg"); + background-position: center; + background-size: cover; + background-repeat: no-repeat; + display: flex; + flex-direction: row; + flex-wrap: nowrap; + justify-content: flex-end; + align-items: center; +} \ No newline at end of file diff --git a/000_temp/86-website/resources/img/skyf.jpeg b/000_temp/86-website/resources/img/skyf.jpeg new file mode 100644 index 00000000..620c76d5 Binary files /dev/null and b/000_temp/86-website/resources/img/skyf.jpeg differ diff --git a/000_temp/86-website/resources/img/toby.jpg b/000_temp/86-website/resources/img/toby.jpg new file mode 100644 index 00000000..16fe5330 Binary files /dev/null and b/000_temp/86-website/resources/img/toby.jpg differ diff --git a/000_temp/86-website/templates/index.gohtml b/000_temp/86-website/templates/index.gohtml new file mode 100644 index 00000000..975e7247 --- /dev/null +++ b/000_temp/86-website/templates/index.gohtml @@ -0,0 +1,21 @@ + + + + + + + Document + + + + + +
+

SKYFALL

+
+ + + + + \ No newline at end of file diff --git a/000_temp/87-website/main.go b/000_temp/87-website/main.go new file mode 100644 index 00000000..337f4776 --- /dev/null +++ b/000_temp/87-website/main.go @@ -0,0 +1,22 @@ +package main + +import ( + "html/template" + "net/http" +) + +var tpl *template.Template + +func init() { + tpl = template.Must(template.ParseGlob("templates/*.gohtml")) +} + +func main() { + http.HandleFunc("/", idx) + http.Handle("/assets/", http.StripPrefix("/assets", http.FileServer(http.Dir("./served")))) + http.ListenAndServe(":80", nil) +} + +func idx(w http.ResponseWriter, r *http.Request) { + tpl.ExecuteTemplate(w, "index.gohtml", nil) +} diff --git a/000_temp/87-website/served/css/main.css b/000_temp/87-website/served/css/main.css new file mode 100644 index 00000000..68ba7fe4 --- /dev/null +++ b/000_temp/87-website/served/css/main.css @@ -0,0 +1,24 @@ +html, body, div, h1 { + padding: 0; + border: 0; + margin: 0; + box-sizing: border-box; +} + +#atf { + height: 100vh; + background-image: url("http://github.com/assets/img/skyf.jpeg"); + background-repeat: no-repeat; + background-size: cover; + background-position: center; + display: flex; + flex-direction: row; + flex-wrap: nowrap; + justify-content: flex-end; + align-items: center; +} + +h1 { + color: red; + font-size: 10rem; +} \ No newline at end of file diff --git a/000_temp/87-website/served/img/skyf.jpeg b/000_temp/87-website/served/img/skyf.jpeg new file mode 100644 index 00000000..620c76d5 Binary files /dev/null and b/000_temp/87-website/served/img/skyf.jpeg differ diff --git a/000_temp/87-website/skyfall b/000_temp/87-website/skyfall new file mode 100755 index 00000000..2aad6f6c Binary files /dev/null and b/000_temp/87-website/skyfall differ diff --git a/000_temp/87-website/templates/index.gohtml b/000_temp/87-website/templates/index.gohtml new file mode 100644 index 00000000..b57b18ac --- /dev/null +++ b/000_temp/87-website/templates/index.gohtml @@ -0,0 +1,15 @@ + + + + + Title + + + + +
+

SKYFALL

+
+ + + \ No newline at end of file diff --git a/000_temp/88-whole-enchilada/main.go b/000_temp/88-whole-enchilada/main.go new file mode 100644 index 00000000..6c587e24 --- /dev/null +++ b/000_temp/88-whole-enchilada/main.go @@ -0,0 +1,63 @@ +package main + +import ( + "html/template" + "net/http" + _ "github.com/lib/pq" + "database/sql" + "log" + "fmt" +) + +type customer struct { + ID int + First string +} + +var tpl *template.Template +var db *sql.DB + +func init() { + var err error + + tpl = template.Must(template.ParseGlob("templates/*.gohtml")) + + connStr := "dbname=thanksgiving user=turkey password=gravy host=localhost port=5432 sslmode=disable" + db, err = sql.Open("postgres", connStr) + if err != nil { + log.Fatal("**** NO OPEN ****", err) + } + + err = db.Ping() + if err != nil { + log.Fatal("**** NO PING WORKING ****", err) + } else { + fmt.Println("Ping successful") + } +} + +func main() { + defer db.Close() + + http.HandleFunc("/", index) + http.Handle("/assets/", http.StripPrefix("/assets", http.FileServer(http.Dir("./resources")))) + http.ListenAndServe(":80", nil) +} + +func index(w http.ResponseWriter, r *http.Request) { + rows, err := db.Query("SELECT * FROM customers;") + if err != nil { + log.Fatal(err) + } + defer rows.Close() + + xc := []customer{} + + for rows.Next() { + c := customer{} + rows.Scan(&c.ID, &c.First) + xc = append(xc, c) + } + + tpl.ExecuteTemplate(w, "index.gohtml", xc) +} diff --git a/000_temp/88-whole-enchilada/resources/img/skyf.jpeg b/000_temp/88-whole-enchilada/resources/img/skyf.jpeg new file mode 100644 index 00000000..620c76d5 Binary files /dev/null and b/000_temp/88-whole-enchilada/resources/img/skyf.jpeg differ diff --git a/000_temp/88-whole-enchilada/templates/index.gohtml b/000_temp/88-whole-enchilada/templates/index.gohtml new file mode 100644 index 00000000..5b7b6df3 --- /dev/null +++ b/000_temp/88-whole-enchilada/templates/index.gohtml @@ -0,0 +1,21 @@ + + + + + + + HOME + + + +

HOME

+ +{{range .}} +

ID - {{.ID}} .......... FIRST - {{.First}}

+{{end}} + + + + + \ No newline at end of file diff --git a/000_temp/88-whole-enchilada/texan b/000_temp/88-whole-enchilada/texan new file mode 100755 index 00000000..c22cc5a1 Binary files /dev/null and b/000_temp/88-whole-enchilada/texan differ diff --git a/000_temp/90-div/index.html b/000_temp/90-div/index.html new file mode 100644 index 00000000..f45958a8 --- /dev/null +++ b/000_temp/90-div/index.html @@ -0,0 +1,417 @@ + + + + + Title + + + + + +
+ + + + + \ No newline at end of file diff --git a/000_temp/90-div/main.css b/000_temp/90-div/main.css new file mode 100644 index 00000000..6826090f --- /dev/null +++ b/000_temp/90-div/main.css @@ -0,0 +1,10 @@ +#dog { + /*formatting*/ + width: 150px; + height: 150px; + background-color: red; + /*layout*/ + position: fixed; + right: 0; + bottom: 0; +} \ No newline at end of file diff --git a/000_temp/91-flex/index.html b/000_temp/91-flex/index.html new file mode 100644 index 00000000..87342e3f --- /dev/null +++ b/000_temp/91-flex/index.html @@ -0,0 +1,14 @@ + + + + + Title + + + + + +
+ + + \ No newline at end of file diff --git a/000_temp/91-flex/main.css b/000_temp/91-flex/main.css new file mode 100644 index 00000000..67c623d9 --- /dev/null +++ b/000_temp/91-flex/main.css @@ -0,0 +1,23 @@ +html, body, div { + padding: 0; + border: 0; + margin: 0; + box-sizing: border-box; +} + +body { + height: 100vh; + border: 4px solid red; + display: flex; + flex-direction: row; + flex-wrap: nowrap; + justify-content: center; + align-items: center; +} + +#dog { + /*formatting*/ + width: 150px; + height: 150px; + background-color: red; +} \ No newline at end of file diff --git a/000_temp/92-whole-enchilada-2/main.go b/000_temp/92-whole-enchilada-2/main.go new file mode 100644 index 00000000..9bdcdea6 --- /dev/null +++ b/000_temp/92-whole-enchilada-2/main.go @@ -0,0 +1,175 @@ +package main + +import ( + "html/template" + "net/http" + _ "github.com/lib/pq" + "database/sql" + "log" + "fmt" +) + +type customer struct { + ID int + First string +} + +var tpl *template.Template +var db *sql.DB + +func init() { + var err error + + tpl = template.Must(template.ParseGlob("templates/*.gohtml")) + + connStr := "dbname=thanksgiving user=turkey password=gravy host=localhost port=5432 sslmode=disable" + db, err = sql.Open("postgres", connStr) + if err != nil { + log.Fatal("**** NO OPEN ****", err) + } + + err = db.Ping() + if err != nil { + log.Fatal("**** NO PING WORKING ****", err) + } else { + fmt.Println("Ping successful") + } +} + +func main() { + defer db.Close() + + http.HandleFunc("/", read) + http.HandleFunc("/process", create) + http.HandleFunc("/processtoo", ptoo) + http.HandleFunc("/update", up) + http.HandleFunc("/delete", del) + http.Handle("/assets/", http.StripPrefix("/assets", http.FileServer(http.Dir("./resources")))) + http.ListenAndServe(":8080", nil) +} + +//READ +func read(w http.ResponseWriter, r *http.Request) { + rows, err := db.Query("SELECT * FROM customers;") + if err != nil { + log.Fatal(err) + } + defer rows.Close() + + xc := []customer{} + + for rows.Next() { + c := customer{} + rows.Scan(&c.ID, &c.First) + xc = append(xc, c) + } + + tpl.ExecuteTemplate(w, "index.gohtml", xc) +} + +// CREATE +func create(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodPost { + http.Redirect(w, r, "/", http.StatusFound) + return + } + + fn := r.FormValue("fffnnnaaammmeee") + fmt.Println(fn) + + result, err := db.Exec("INSERT INTO customers (cfirst) VALUES ($1);", fn) + if err != nil { + log.Fatal(err) + } + + n, err := result.RowsAffected() + if err != nil { + log.Println(err) + http.Redirect(w, r, "/", http.StatusFound) + return + } + fmt.Println("rows affected", n) + + http.Redirect(w, r, "/", http.StatusFound) +} + + +// UPDATE +func up(w http.ResponseWriter, r *http.Request) { + + if r.Method != http.MethodGet { + http.Redirect(w, r, "/", http.StatusFound) + return + } + + customerid := r.FormValue("recordid") + + row, err := db.Query("SELECT * FROM customers WHERE cid = $1", customerid) + if err != nil { + http.Error(w, "coudn't retrieve record in up", http.StatusInternalServerError) + return + } + + c := customer{} + for row.Next() { + row.Scan(&c.ID, &c.First) + } + + 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) + return + } + + id := r.FormValue("customerid") + fn := r.FormValue("fffnnnaaammmeee") + fmt.Println(id) + fmt.Println(fn) + + result, err := db.Exec("UPDATE customers SET cfirst = $2 WHERE cid = $1;", id, fn) + if err != nil { + log.Fatal(err) + } + + n, err := result.RowsAffected() + if err != nil { + log.Println(err) + http.Redirect(w, r, "/", http.StatusFound) + return + } + fmt.Println("rows affected", n) + + http.Redirect(w, r, "/", http.StatusFound) +} + + +// DELETE +func del(w http.ResponseWriter, r *http.Request) { + + if r.Method != http.MethodGet { + http.Redirect(w, r, "/", http.StatusFound) + return + } + + 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) + return + } + + n, err := result.RowsAffected() + if err != nil { + http.Error(w, "didn't show rows affected", http.StatusInternalServerError) + return + } + fmt.Println("rows affected", n) + + http.Redirect(w, r, "/", http.StatusFound) +} diff --git a/000_temp/92-whole-enchilada-2/resources/img/skyf.jpeg b/000_temp/92-whole-enchilada-2/resources/img/skyf.jpeg new file mode 100644 index 00000000..620c76d5 Binary files /dev/null and b/000_temp/92-whole-enchilada-2/resources/img/skyf.jpeg differ diff --git a/000_temp/92-whole-enchilada-2/templates/index.gohtml b/000_temp/92-whole-enchilada-2/templates/index.gohtml new file mode 100644 index 00000000..4a292971 --- /dev/null +++ b/000_temp/92-whole-enchilada-2/templates/index.gohtml @@ -0,0 +1,29 @@ + + + + + + + HOME + + + +

HOME

+ +{{range .}} +

ID - {{.ID}} .......... FIRST - {{.First}} .. UPDATE .. DELETE

+{{end}} + + + + + +
+ + + +
+ + + \ No newline at end of file diff --git a/000_temp/92-whole-enchilada-2/templates/update.gohtml b/000_temp/92-whole-enchilada-2/templates/update.gohtml new file mode 100644 index 00000000..3e0261fd --- /dev/null +++ b/000_temp/92-whole-enchilada-2/templates/update.gohtml @@ -0,0 +1,24 @@ + + + + + + + HOME + + + +

UPDATING

+ +

ID - {{.ID}} .......... FIRST - {{.First}}

+ +
+ + + + +
+ + + \ 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 new file mode 100644 index 00000000..624e6494 --- /dev/null +++ b/003_string-to-html/03_os-Args/index.html @@ -0,0 +1,12 @@ + + + + + + Hello World! + + +

JAMES

+ + + \ No newline at end of file diff --git a/003_string-to-html/README.md b/003_string-to-html/README.md index 8587b360..d7ae7b61 100644 --- a/003_string-to-html/README.md +++ b/003_string-to-html/README.md @@ -30,8 +30,8 @@ NewReader returns a new Reader reading from s. func NewReader(s string) *Reader ``` -##[os.Args](https://godoc.org/os#pkg-variables) +## [os.Args](https://godoc.org/os#pkg-variables) Args is a variable from package os. Args hold the command-line arguments, starting with the program name. ``` Go var Args []string -``` \ No newline at end of file +``` diff --git a/010_nested-templates/01_nested-templates/templates/index.gohtml b/010_nested-templates/01_nested-templates/templates/index.gohtml index 29296d88..8a809ac1 100644 --- a/010_nested-templates/01_nested-templates/templates/index.gohtml +++ b/010_nested-templates/01_nested-templates/templates/index.gohtml @@ -5,7 +5,10 @@ Hello World! +

The meaning of life: {{.}}

+

{{template "polarbear"}}

+ \ No newline at end of file diff --git a/011_composition-and-methods/04_method/tpl.gohtml b/011_composition-and-methods/04_method/tpl.gohtml index 8cbaaab8..341d3db9 100644 --- a/011_composition-and-methods/04_method/tpl.gohtml +++ b/011_composition-and-methods/04_method/tpl.gohtml @@ -10,6 +10,7 @@

{{.Age}}

{{.SomeProcessing}}

{{.AgeDbl}}

+

{{.Age | .TakesArg}}

{{.AgeDbl | .TakesArg}}

\ No newline at end of file diff --git a/015_understanding-TCP-servers/06_dial-write/README.md b/015_understanding-TCP-servers/06_dial-write/README.md index 3d836a76..5e9cdb6f 100644 --- a/015_understanding-TCP-servers/06_dial-write/README.md +++ b/015_understanding-TCP-servers/06_dial-write/README.md @@ -1,3 +1,3 @@ run "02_read-scanner" -run "07_dial-write" \ No newline at end of file +run "06_dial-write" \ No newline at end of file diff --git a/015_understanding-TCP-servers/07_tcp-apps/02_memory-database/main.go b/015_understanding-TCP-servers/07_tcp-apps/02_memory-database/main.go index fa81dae9..0bae2ca2 100644 --- a/015_understanding-TCP-servers/07_tcp-apps/02_memory-database/main.go +++ b/015_understanding-TCP-servers/07_tcp-apps/02_memory-database/main.go @@ -46,7 +46,9 @@ func handle(conn net.Conn) { ln := scanner.Text() fs := strings.Fields(ln) // logic - if (len(fs) < 1) { continue } + if len(fs) < 1 { + continue + } switch fs[0] { case "GET": k := fs[1] diff --git a/016_building-a-tcp-server-for-http/01/main.go b/016_building-a-tcp-server-for-http/01/main.go index fef0ddad..84875e68 100644 --- a/016_building-a-tcp-server-for-http/01/main.go +++ b/016_building-a-tcp-server-for-http/01/main.go @@ -56,7 +56,7 @@ func request(conn net.Conn) { func respond(conn net.Conn) { - body := `Hello World` + body := `Hello World` fmt.Fprint(conn, "HTTP/1.1 200 OK\r\n") fmt.Fprintf(conn, "Content-Length: %d\r\n", len(body)) diff --git a/016_building-a-tcp-server-for-http/03_solution/main.go b/016_building-a-tcp-server-for-http/03_solution/main.go index 33321d54..5417c964 100644 --- a/016_building-a-tcp-server-for-http/03_solution/main.go +++ b/016_building-a-tcp-server-for-http/03_solution/main.go @@ -58,7 +58,7 @@ func request(conn net.Conn) { func respond(conn net.Conn) { - body := `Hello World` + body := `Hello World` fmt.Fprint(conn, "HTTP/1.1 200 OK\r\n") fmt.Fprintf(conn, "Content-Length: %d\r\n", len(body)) diff --git a/016_building-a-tcp-server-for-http/05_solution/main.go b/016_building-a-tcp-server-for-http/05_solution/main.go index 10921185..d88b77cd 100644 --- a/016_building-a-tcp-server-for-http/05_solution/main.go +++ b/016_building-a-tcp-server-for-http/05_solution/main.go @@ -122,7 +122,7 @@ func contact(conn net.Conn) { func apply(conn net.Conn) { - body := ` + body := ` APPLY
index
about
diff --git a/022_hands-on/02/04_solution/main.go b/022_hands-on/02/04_solution/main.go index 7740f10b..2fdb743c 100644 --- a/022_hands-on/02/04_solution/main.go +++ b/022_hands-on/02/04_solution/main.go @@ -34,7 +34,5 @@ func main() { // how does the above reader know when it's done? fmt.Println("Code got here.") io.WriteString(conn, "I see you connected.") - - conn.Close() } } diff --git a/022_hands-on/02/05_hands-on/README.md b/022_hands-on/02/05_hands-on/README.md index 38d0de3a..9040f3ad 100644 --- a/022_hands-on/02/05_hands-on/README.md +++ b/022_hands-on/02/05_hands-on/README.md @@ -8,11 +8,13 @@ We will now break out of the reading. Package bufio has the Scanner type. The Scanner type "provides a convenient interface for reading data". When you have a Scanner type, you can call the SCAN method on it. Successive calls to the Scan method will step through the tokens (piece of data). The default token is a line. The Scanner type also has a TEXT method. When you call this method, you will be given the text from the current token. Here is how you will use it: +``` scanner := bufio.NewScanner(conn) for scanner.Scan() { ln := scanner.Text() fmt.Println(ln) } +``` Use this code to READ from an incoming connection and print the incoming text to standard out (the terminal). @@ -20,4 +22,4 @@ When your "ln" line of text is equal to an empty string, break out of the loop. Run your code and go to localhost:8080 in **your browser.** -What do you find? \ No newline at end of file +What do you find? diff --git a/022_hands-on/02/11_hands-on/README.md b/022_hands-on/02/11_hands-on/README.md index 64aaf71c..044f91eb 100644 --- a/022_hands-on/02/11_hands-on/README.md +++ b/022_hands-on/02/11_hands-on/README.md @@ -1,6 +1,6 @@ # Building upon the code from the previous problem: -Before we WRITE our RESPONSE , let's WRITE to our RESPONSE the STATUS LINE and some REPONSE HEADERS. Remember the request line and status line: +Before we WRITE our RESPONSE, let's WRITE to our RESPONSE the STATUS LINE and some RESPONSE HEADERS. Remember the request line and status line: REQUEST LINE GET / HTTP/1.1 @@ -22,4 +22,4 @@ fmt.Fprint(c, "Content-Type: text/plain\r\n") "\r\n" -Look in your browser "developer tools" under the network tab. Compare the RESPONSE HEADERS from the previous file with the RESPONSE HEADERS in your new solution. \ No newline at end of file +Look in your browser "developer tools" under the network tab. Compare the RESPONSE HEADERS from the previous file with the RESPONSE HEADERS in your new solution. diff --git a/023_serving-files/02_serving/04_FileServer/01/main.go b/023_serving-files/02_serving/04_FileServer/01/main.go index 5ff93afd..34a0df2a 100644 --- a/023_serving-files/02_serving/04_FileServer/01/main.go +++ b/023_serving-files/02_serving/04_FileServer/01/main.go @@ -7,11 +7,11 @@ import ( func main() { http.Handle("/", http.FileServer(http.Dir("."))) - http.HandleFunc("/dog", dog) + http.HandleFunc("/dog/", dog) http.ListenAndServe(":8080", nil) } func dog(w http.ResponseWriter, req *http.Request) { w.Header().Set("Content-Type", "text/html; charset=utf-8") - io.WriteString(w, ``) + io.WriteString(w, ``) } diff --git a/023_serving-files/02_serving/04_FileServer/02/main.go b/023_serving-files/02_serving/04_FileServer/02/main.go index 2e190205..3a7a5ff0 100644 --- a/023_serving-files/02_serving/04_FileServer/02/main.go +++ b/023_serving-files/02_serving/04_FileServer/02/main.go @@ -15,3 +15,9 @@ func dog(w http.ResponseWriter, req *http.Request) { w.Header().Set("Content-Type", "text/html; charset=utf-8") io.WriteString(w, ``) } + +/* + +./assets/toby.jpg + +*/ diff --git a/024_hands-on/09_hands-on/starting-files/css/main.css b/024_hands-on/09_hands-on/starting-files/public/css/main.css similarity index 100% rename from 024_hands-on/09_hands-on/starting-files/css/main.css rename to 024_hands-on/09_hands-on/starting-files/public/css/main.css diff --git a/024_hands-on/09_hands-on/starting-files/css/reset.css b/024_hands-on/09_hands-on/starting-files/public/css/reset.css similarity index 100% rename from 024_hands-on/09_hands-on/starting-files/css/reset.css rename to 024_hands-on/09_hands-on/starting-files/public/css/reset.css diff --git a/024_hands-on/09_hands-on/starting-files/pic/surf.jpg b/024_hands-on/09_hands-on/starting-files/public/pic/surf.jpg similarity index 100% rename from 024_hands-on/09_hands-on/starting-files/pic/surf.jpg rename to 024_hands-on/09_hands-on/starting-files/public/pic/surf.jpg diff --git a/024_hands-on/09_hands-on/starting-files/index.html b/024_hands-on/09_hands-on/starting-files/templates/index.gohtml similarity index 95% rename from 024_hands-on/09_hands-on/starting-files/index.html rename to 024_hands-on/09_hands-on/starting-files/templates/index.gohtml index bcbd3c5b..cb39f57e 100644 --- a/024_hands-on/09_hands-on/starting-files/index.html +++ b/024_hands-on/09_hands-on/starting-files/templates/index.gohtml @@ -3,12 +3,12 @@ - - + + -A man surfing a wave +A man surfing a wave diff --git a/024_hands-on/12_solution/main.go b/024_hands-on/12_solution/main.go index 8f8bf128..7e59a3e7 100644 --- a/024_hands-on/12_solution/main.go +++ b/024_hands-on/12_solution/main.go @@ -14,8 +14,8 @@ func init() { func main() { http.HandleFunc("/", index) - http.HandleFunc("/about", about) - http.HandleFunc("/contact", contact) + http.HandleFunc("/about/", about) + http.HandleFunc("/contact/", contact) http.HandleFunc("/apply", apply) http.ListenAndServe(":8080", nil) } diff --git a/026_appengine-deploy/01/app.yaml b/026_appengine-deploy/01/app.yaml index 1dd8f1c6..fb82ec67 100644 --- a/026_appengine-deploy/01/app.yaml +++ b/026_appengine-deploy/01/app.yaml @@ -1,8 +1,6 @@ -application: temp-145415 -version: 1 -runtime: go -api_version: go1 +runtime: go113 handlers: - url: /.* - script: _go_app \ No newline at end of file + script: auto + secure: always \ No newline at end of file diff --git a/026_appengine-deploy/01/main.go b/026_appengine-deploy/01/main.go index 68523458..671d6b1d 100644 --- a/026_appengine-deploy/01/main.go +++ b/026_appengine-deploy/01/main.go @@ -4,6 +4,7 @@ import ( "net/http" ) -func init() { +func main() { http.Handle("/", http.FileServer(http.Dir("."))) + http.ListenAndServe(":8080", nil) } diff --git a/026_appengine-deploy/02/app.yaml b/026_appengine-deploy/02/app.yaml index 1dd8f1c6..fb82ec67 100644 --- a/026_appengine-deploy/02/app.yaml +++ b/026_appengine-deploy/02/app.yaml @@ -1,8 +1,6 @@ -application: temp-145415 -version: 1 -runtime: go -api_version: go1 +runtime: go113 handlers: - url: /.* - script: _go_app \ No newline at end of file + script: auto + secure: always \ No newline at end of file diff --git a/026_appengine-deploy/02/main.go b/026_appengine-deploy/02/main.go index 68523458..671d6b1d 100644 --- a/026_appengine-deploy/02/main.go +++ b/026_appengine-deploy/02/main.go @@ -4,6 +4,7 @@ import ( "net/http" ) -func init() { +func main() { http.Handle("/", http.FileServer(http.Dir("."))) + http.ListenAndServe(":8080", nil) } diff --git a/026_appengine-deploy/README.md b/026_appengine-deploy/README.md index 247ba1be..c256d787 100644 --- a/026_appengine-deploy/README.md +++ b/026_appengine-deploy/README.md @@ -1,30 +1,27 @@ -# buying a domain + # buying a domain https://domains.google/#/ # deploying to [Google Cloud](https://cloud.google.com/) - [install google appengine](https://cloud.google.com/appengine/docs/go/download) -- [make sure python is installed VERSION 2.7.x](https://www.python.org/downloads/release/python-2712/) - - python -V - configure environment PATH variables - google cloud developer console - create a project - get the project ID -- update the app.yaml file with your project ID +- set your go version in the app.yaml file ``` -application: temp-137512 -version: 1 -runtime: go -api_version: go1 - +runtime: go113 handlers: - url: /.* - script: _go_app + script: auto + secure: always ``` -- deploy to that project +- deploy to that project. update --project with your project-id ``` -appcfg.py -A -V v1 update . +gcloud app deploy app.yaml --project= -v 1 +my example: +gcloud app deploy --project temp-137512 ``` - view your project - http://YOUR_PROJECT_ID.appspot.com/ diff --git a/027_passing-data/05_form-file/02_store/main.go b/027_passing-data/05_form-file/02_store/main.go index 1d5ba06f..61b709e0 100644 --- a/027_passing-data/05_form-file/02_store/main.go +++ b/027_passing-data/05_form-file/02_store/main.go @@ -56,6 +56,7 @@ func foo(w http.ResponseWriter, req *http.Request) { _, err = dst.Write(bs) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) + return } } diff --git a/029_cookies/01_set_get/main.go b/029_cookies/01_set_get/main.go index dbc499b6..69873249 100644 --- a/029_cookies/01_set_get/main.go +++ b/029_cookies/01_set_get/main.go @@ -16,6 +16,7 @@ func set(w http.ResponseWriter, req *http.Request) { http.SetCookie(w, &http.Cookie{ Name: "my-cookie", Value: "some value", + 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 e08b2fcc..379d69d2 100644 --- a/029_cookies/02_multiple/main.go +++ b/029_cookies/02_multiple/main.go @@ -18,6 +18,7 @@ func set(w http.ResponseWriter, req *http.Request) { http.SetCookie(w, &http.Cookie{ Name: "my-cookie", Value: "some value", + 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/03_hands-on/README.md b/029_cookies/03_hands-on/README.md index 7319a52d..01b97f4d 100644 --- a/029_cookies/03_hands-on/README.md +++ b/029_cookies/03_hands-on/README.md @@ -1 +1,36 @@ -Using cookies, track how many times a user has been to your website domain. \ No newline at end of file +package main + +import ( + "fmt" + "net/http" +) + +func main() { + http.HandleFunc("/", set) + http.HandleFunc("/read", read) + http.Handle("/favicon.ico", http.NotFoundHandler()) + http.ListenAndServe(":8080", nil) +} + +func set(w http.ResponseWriter, req *http.Request) { + http.SetCookie(w, &http.Cookie{ + Name: "my-cookie", + Value: "some value", + Path: "/", + }) + fmt.Fprintln(w, "COOKIE WRITTEN - CHECK YOUR BROWSER") + fmt.Fprintln(w, "in chrome go to: dev tools / application / cookies") +} + +func read(w http.ResponseWriter, req *http.Request) { + + c, err := req.Cookie("my-cookie") + if err != nil { + http.Error(w, http.StatusText(400), http.StatusBadRequest) + return + } + + fmt.Fprintln(w, "YOUR COOKIE:", c) +} + +// Using cookies, track how many times a user has been to your website domain. \ No newline at end of file diff --git a/029_cookies/04_solution/main.go b/029_cookies/04_solution/main.go index 89b03c44..653ef59b 100644 --- a/029_cookies/04_solution/main.go +++ b/029_cookies/04_solution/main.go @@ -21,6 +21,7 @@ func foo(res http.ResponseWriter, req *http.Request) { cookie = &http.Cookie{ Name: "my-cookie", Value: "0", + Path: "/", } } diff --git a/029_cookies/05_maxage/main.go b/029_cookies/05_maxage/main.go index 0b095752..2c8373c5 100644 --- a/029_cookies/05_maxage/main.go +++ b/029_cookies/05_maxage/main.go @@ -21,6 +21,7 @@ func set(w http.ResponseWriter, req *http.Request) { http.SetCookie(w, &http.Cookie{ Name: "session", Value: "some value", + Path: "/", }) fmt.Fprintln(w, `

read

`) } diff --git a/029_cookies/06_path/02/main.go b/029_cookies/06_path/02/main.go index e9f3a283..efbb7027 100644 --- a/029_cookies/06_path/02/main.go +++ b/029_cookies/06_path/02/main.go @@ -27,7 +27,7 @@ func set(w http.ResponseWriter, req *http.Request) { c := &http.Cookie{ Name: "my-cookie", Value: "some value", - Path: "/", + Path: "/", } http.SetCookie(w, c) fmt.Println(c) diff --git a/029_cookies/06_path/03_templates/01/main.go b/029_cookies/06_path/03_templates/01/main.go index 09d6ac33..ce846b2d 100644 --- a/029_cookies/06_path/03_templates/01/main.go +++ b/029_cookies/06_path/03_templates/01/main.go @@ -1,9 +1,9 @@ package main import ( - "net/http" - "html/template" "fmt" + "html/template" + "net/http" ) var tpl *template.Template @@ -32,9 +32,9 @@ func index(w http.ResponseWriter, r *http.Request) { func bowzer(w http.ResponseWriter, r *http.Request) { c := &http.Cookie{ - Name: "user-cookie", + Name: "user-cookie", Value: "this would be the value", - Path: "/dog/bowzer", + Path: "/dog/bowzer", } http.SetCookie(w, c) tpl.ExecuteTemplate(w, "bowzer.gohtml", c) @@ -56,4 +56,4 @@ func cat(w http.ResponseWriter, r *http.Request) { fmt.Printf("%T\n", c) } tpl.ExecuteTemplate(w, "cat.gohtml", c) -} \ No newline at end of file +} diff --git a/029_cookies/06_path/03_templates/02/main.go b/029_cookies/06_path/03_templates/02/main.go index bc2b7937..b504073a 100644 --- a/029_cookies/06_path/03_templates/02/main.go +++ b/029_cookies/06_path/03_templates/02/main.go @@ -1,9 +1,9 @@ package main import ( - "net/http" - "html/template" "fmt" + "html/template" + "net/http" ) var tpl *template.Template @@ -32,9 +32,10 @@ func index(w http.ResponseWriter, r *http.Request) { func bowzer(w http.ResponseWriter, r *http.Request) { c := &http.Cookie{ - Name: "user-cookie", + Name: "user-cookie", Value: "this would be the value", - Path: "/", + Path: "/", + //Path: "/dog/bowzer/", } http.SetCookie(w, c) tpl.ExecuteTemplate(w, "bowzer.gohtml", c) @@ -56,4 +57,4 @@ func cat(w http.ResponseWriter, r *http.Request) { fmt.Printf("%T\n", c) } tpl.ExecuteTemplate(w, "cat.gohtml", c) -} \ No newline at end of file +} diff --git a/030_sessions/01_uuid/main.go b/030_sessions/01_uuid/main.go index b67b3664..ad36b1f6 100644 --- a/030_sessions/01_uuid/main.go +++ b/030_sessions/01_uuid/main.go @@ -24,7 +24,7 @@ func index(w http.ResponseWriter, req *http.Request) { Value: id.String(), // Secure: true, HttpOnly: true, - Path: "/", + Path: "/", } http.SetCookie(w, cookie) } diff --git a/030_sessions/02_session/main.go b/030_sessions/02_session/main.go index 8a47f068..bda843e9 100644 --- a/030_sessions/02_session/main.go +++ b/030_sessions/02_session/main.go @@ -32,7 +32,7 @@ func index(w http.ResponseWriter, req *http.Request) { // get cookie c, err := req.Cookie("session") if err != nil { - sID := uuid.NewV4() + sID, _ := uuid.NewV4() c = &http.Cookie{ Name: "session", Value: sID.String(), diff --git a/030_sessions/03_signup/main.go b/030_sessions/03_signup/main.go index d086bdfd..48f0d58c 100644 --- a/030_sessions/03_signup/main.go +++ b/030_sessions/03_signup/main.go @@ -1,9 +1,10 @@ package main import ( - "github.com/satori/go.uuid" "html/template" "net/http" + + uuid "github.com/satori/go.uuid" ) type user struct { @@ -30,12 +31,12 @@ func main() { } func index(w http.ResponseWriter, req *http.Request) { - u := getUser(w, req) + u := getUser(req) tpl.ExecuteTemplate(w, "index.gohtml", u) } func bar(w http.ResponseWriter, req *http.Request) { - u := getUser(w, req) + u := getUser(req) if !alreadyLoggedIn(req) { http.Redirect(w, req, "/", http.StatusSeeOther) return @@ -65,7 +66,7 @@ func signup(w http.ResponseWriter, req *http.Request) { } // create session - sID := uuid.NewV4() + sID, _ := uuid.NewV4() c := &http.Cookie{ Name: "session", Value: sID.String(), diff --git a/030_sessions/03_signup/session.go b/030_sessions/03_signup/session.go index d524baac..bda111e9 100644 --- a/030_sessions/03_signup/session.go +++ b/030_sessions/03_signup/session.go @@ -1,25 +1,19 @@ package main import ( - "github.com/satori/go.uuid" "net/http" ) -func getUser(w http.ResponseWriter, req *http.Request) user { +func getUser(req *http.Request) user { + var u user + // get cookie c, err := req.Cookie("session") if err != nil { - sID := uuid.NewV4() - c = &http.Cookie{ - Name: "session", - Value: sID.String(), - } - + return u } - http.SetCookie(w, c) // if the user exists already, get user - var u user if un, ok := dbSessions[c.Value]; ok { u = dbUsers[un] } diff --git a/030_sessions/04_bcrypt/main.go b/030_sessions/04_bcrypt/main.go index 4a6f6dd7..f6d55904 100644 --- a/030_sessions/04_bcrypt/main.go +++ b/030_sessions/04_bcrypt/main.go @@ -68,7 +68,7 @@ func signup(w http.ResponseWriter, req *http.Request) { } // create session - sID := uuid.NewV4() + sID, _ := uuid.NewV4() c := &http.Cookie{ Name: "session", Value: sID.String(), diff --git a/030_sessions/04_bcrypt/session.go b/030_sessions/04_bcrypt/session.go index d524baac..81c2e627 100644 --- a/030_sessions/04_bcrypt/session.go +++ b/030_sessions/04_bcrypt/session.go @@ -9,7 +9,7 @@ func getUser(w http.ResponseWriter, req *http.Request) user { // get cookie c, err := req.Cookie("session") if err != nil { - sID := uuid.NewV4() + sID, _ := uuid.NewV4() c = &http.Cookie{ Name: "session", Value: sID.String(), diff --git a/030_sessions/05_login/main.go b/030_sessions/05_login/main.go index 20928181..9f3b6c56 100644 --- a/030_sessions/05_login/main.go +++ b/030_sessions/05_login/main.go @@ -66,7 +66,7 @@ func signup(w http.ResponseWriter, req *http.Request) { return } // create session - sID := uuid.NewV4() + sID, _ := uuid.NewV4() c := &http.Cookie{ Name: "session", Value: sID.String(), @@ -111,7 +111,7 @@ func login(w http.ResponseWriter, req *http.Request) { return } // create session - sID := uuid.NewV4() + sID, _ := uuid.NewV4() c := &http.Cookie{ Name: "session", Value: sID.String(), diff --git a/030_sessions/05_login/session.go b/030_sessions/05_login/session.go index d524baac..81c2e627 100644 --- a/030_sessions/05_login/session.go +++ b/030_sessions/05_login/session.go @@ -9,7 +9,7 @@ func getUser(w http.ResponseWriter, req *http.Request) user { // get cookie c, err := req.Cookie("session") if err != nil { - sID := uuid.NewV4() + sID, _ := uuid.NewV4() c = &http.Cookie{ Name: "session", Value: sID.String(), diff --git a/030_sessions/06_logout/main.go b/030_sessions/06_logout/main.go index 223db65a..a6cfbd2b 100644 --- a/030_sessions/06_logout/main.go +++ b/030_sessions/06_logout/main.go @@ -67,7 +67,7 @@ func signup(w http.ResponseWriter, req *http.Request) { return } // create session - sID := uuid.NewV4() + sID, _ := uuid.NewV4() c := &http.Cookie{ Name: "session", Value: sID.String(), @@ -112,7 +112,7 @@ func login(w http.ResponseWriter, req *http.Request) { return } // create session - sID := uuid.NewV4() + sID, _ := uuid.NewV4() c := &http.Cookie{ Name: "session", Value: sID.String(), diff --git a/030_sessions/06_logout/session.go b/030_sessions/06_logout/session.go index d524baac..81c2e627 100644 --- a/030_sessions/06_logout/session.go +++ b/030_sessions/06_logout/session.go @@ -9,7 +9,7 @@ func getUser(w http.ResponseWriter, req *http.Request) user { // get cookie c, err := req.Cookie("session") if err != nil { - sID := uuid.NewV4() + sID, _ := uuid.NewV4() c = &http.Cookie{ Name: "session", Value: sID.String(), diff --git a/030_sessions/07_permissions/main.go b/030_sessions/07_permissions/main.go index 24a1d297..6cb1eb32 100644 --- a/030_sessions/07_permissions/main.go +++ b/030_sessions/07_permissions/main.go @@ -71,7 +71,7 @@ func signup(w http.ResponseWriter, req *http.Request) { return } // create session - sID := uuid.NewV4() + sID, _ := uuid.NewV4() c := &http.Cookie{ Name: "session", Value: sID.String(), @@ -116,7 +116,7 @@ func login(w http.ResponseWriter, req *http.Request) { return } // create session - sID := uuid.NewV4() + sID, _ := uuid.NewV4() c := &http.Cookie{ Name: "session", Value: sID.String(), diff --git a/030_sessions/07_permissions/session.go b/030_sessions/07_permissions/session.go index d524baac..81c2e627 100644 --- a/030_sessions/07_permissions/session.go +++ b/030_sessions/07_permissions/session.go @@ -9,7 +9,7 @@ func getUser(w http.ResponseWriter, req *http.Request) user { // get cookie c, err := req.Cookie("session") if err != nil { - sID := uuid.NewV4() + sID, _ := uuid.NewV4() c = &http.Cookie{ Name: "session", Value: sID.String(), diff --git a/030_sessions/08_expire-session/main.go b/030_sessions/08_expire-session/main.go index 0e4972b3..690231d9 100644 --- a/030_sessions/08_expire-session/main.go +++ b/030_sessions/08_expire-session/main.go @@ -83,7 +83,7 @@ func signup(w http.ResponseWriter, req *http.Request) { return } // create session - sID := uuid.NewV4() + sID, _ := uuid.NewV4() c := &http.Cookie{ Name: "session", Value: sID.String(), @@ -130,7 +130,7 @@ func login(w http.ResponseWriter, req *http.Request) { return } // create session - sID := uuid.NewV4() + sID, _ := uuid.NewV4() c := &http.Cookie{ Name: "session", Value: sID.String(), diff --git a/030_sessions/08_expire-session/session.go b/030_sessions/08_expire-session/session.go index 45f272d6..94488c1b 100644 --- a/030_sessions/08_expire-session/session.go +++ b/030_sessions/08_expire-session/session.go @@ -11,7 +11,7 @@ func getUser(w http.ResponseWriter, req *http.Request) user { // get cookie c, err := req.Cookie("session") if err != nil { - sID := uuid.NewV4() + sID, _ := uuid.NewV4() c = &http.Cookie{ Name: "session", Value: sID.String(), diff --git a/030_sessions/09_middleware/README.md b/030_sessions/09_middleware/README.md new file mode 100644 index 00000000..1ea4b86a --- /dev/null +++ b/030_sessions/09_middleware/README.md @@ -0,0 +1,69 @@ +# Concurrency & Race Conditions + +## Could this code cause a race condition? + +``` +go cleanSessions() +``` + +``` +func cleanSessions() { + for k, v := range dbSessions { + if time.Now().Sub(v.lastActivity) > (time.Second * 30) { + delete(dbSessions, k) + } + } + dbSessionsCleaned = time.Now() +} + +``` + +https://golang.org/doc/go1.6 says: + +"The runtime has added lightweight, best-effort detection of concurrent misuse of maps. As always, if one goroutine is writing to a map, no other goroutine should be reading or writing the map concurrently. If the runtime detects this condition, it prints a diagnosis and crashes the program. The best way to find out more about the problem is to run the program under the race detector, which will more reliably identify the race and give more detail." + +When you + +``` +go build -race +``` + +you do not get a race condition reported. + + +![no race condition](norace.png) + +So if you're not writing to a map, you can use the map concurrently without a problem. + +RE: time.Time + +"A Time value can be used by multiple goroutines simultaneously." + +https://godoc.org/time#Time + +## Expanding on maps & goroutines + +Maps are funky. + +Check this out: + +![maps are funky](maps.png) + + +https://play.golang.org/p/62DF4xvPeQ + +**So you can delete something that doesn't exist, and that is not a problem.** + +**And you can ask for something that isn't there, and that is not a problem (gives you the zero value for the map's value).** + +Deleting IS DIFFERENT from writing. + +**If more than 1 goroutine tried to delete that same entry in the map: no problem.** + +**And if you're reading from the map and a value isn't there: no problem.** + +So why is WRITING a problem with concurrency? + +The classic race condition example is two routines READING, pulling the same value, each incrementing the value, and then each WRITING the incremented value back, and the value is incremented only 1, instead of 2. + +Just remember: WRITE TO MAP = concurrency considerations. \ No newline at end of file diff --git a/030_sessions/09_middleware/main.go b/030_sessions/09_middleware/main.go new file mode 100644 index 00000000..21cfaedd --- /dev/null +++ b/030_sessions/09_middleware/main.go @@ -0,0 +1,177 @@ +package main + +import ( + "github.com/satori/go.uuid" + "golang.org/x/crypto/bcrypt" + "html/template" + "net/http" + "time" +) + +type user struct { + UserName string + Password []byte + First string + Last string + Role string +} + +type session struct { + un string + lastActivity time.Time +} + +var tpl *template.Template +var dbUsers = map[string]user{} // user ID, user +var dbSessions = map[string]session{} // session ID, session +var dbSessionsCleaned time.Time + +const sessionLength int = 30 + +func init() { + tpl = template.Must(template.ParseGlob("templates/*")) + dbSessionsCleaned = time.Now() +} + +func main() { + http.HandleFunc("/", index) + http.HandleFunc("/bar", bar) + http.HandleFunc("/signup", signup) + http.HandleFunc("/login", login) + http.HandleFunc("/logout", authorized(logout)) + http.Handle("/favicon.ico", http.NotFoundHandler()) + http.ListenAndServe(":8080", nil) +} + +func index(w http.ResponseWriter, req *http.Request) { + u := getUser(w, req) + showSessions() // for demonstration purposes + tpl.ExecuteTemplate(w, "index.gohtml", u) +} + +func bar(w http.ResponseWriter, req *http.Request) { + u := getUser(w, req) + if !alreadyLoggedIn(w, req) { + http.Redirect(w, req, "/", http.StatusSeeOther) + return + } + if u.Role != "007" { + http.Error(w, "You must be 007 to enter the bar", http.StatusForbidden) + return + } + showSessions() // for demonstration purposes + tpl.ExecuteTemplate(w, "bar.gohtml", u) +} + +func signup(w http.ResponseWriter, req *http.Request) { + if alreadyLoggedIn(w, req) { + http.Redirect(w, req, "/", http.StatusSeeOther) + return + } + var u user + // process form submission + if req.Method == http.MethodPost { + // get form values + un := req.FormValue("username") + p := req.FormValue("password") + f := req.FormValue("firstname") + l := req.FormValue("lastname") + r := req.FormValue("role") + // username taken? + if _, ok := dbUsers[un]; ok { + http.Error(w, "Username already taken", http.StatusForbidden) + return + } + // create session + sID, _ := uuid.NewV4() + c := &http.Cookie{ + Name: "session", + Value: sID.String(), + } + c.MaxAge = sessionLength + http.SetCookie(w, c) + dbSessions[c.Value] = session{un, time.Now()} + // store user in dbUsers + bs, err := bcrypt.GenerateFromPassword([]byte(p), bcrypt.MinCost) + if err != nil { + http.Error(w, "Internal server error", http.StatusInternalServerError) + return + } + u = user{un, bs, f, l, r} + dbUsers[un] = u + // redirect + http.Redirect(w, req, "/", http.StatusSeeOther) + return + } + showSessions() // for demonstration purposes + tpl.ExecuteTemplate(w, "signup.gohtml", u) +} + +func login(w http.ResponseWriter, req *http.Request) { + if alreadyLoggedIn(w, req) { + http.Redirect(w, req, "/", http.StatusSeeOther) + return + } + var u user + // process form submission + if req.Method == http.MethodPost { + un := req.FormValue("username") + p := req.FormValue("password") + // is there a username? + u, ok := dbUsers[un] + if !ok { + http.Error(w, "Username and/or password do not match", http.StatusForbidden) + return + } + // does the entered password match the stored password? + err := bcrypt.CompareHashAndPassword(u.Password, []byte(p)) + if err != nil { + http.Error(w, "Username and/or password do not match", http.StatusForbidden) + return + } + // create session + sID, _ := uuid.NewV4() + c := &http.Cookie{ + Name: "session", + Value: sID.String(), + } + c.MaxAge = sessionLength + http.SetCookie(w, c) + dbSessions[c.Value] = session{un, time.Now()} + http.Redirect(w, req, "/", http.StatusSeeOther) + return + } + showSessions() // for demonstration purposes + tpl.ExecuteTemplate(w, "login.gohtml", u) +} + +func logout(w http.ResponseWriter, req *http.Request) { + c, _ := req.Cookie("session") + // delete the session + delete(dbSessions, c.Value) + // remove the cookie + c = &http.Cookie{ + Name: "session", + Value: "", + MaxAge: -1, + } + http.SetCookie(w, c) + + // clean up dbSessions + if time.Now().Sub(dbSessionsCleaned) > (time.Second * 30) { + go cleanSessions() + } + + http.Redirect(w, req, "/login", http.StatusSeeOther) +} + +func authorized(h http.HandlerFunc) http.HandlerFunc { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if !alreadyLoggedIn(w, r) { + //http.Error(w, "not logged in", http.StatusUnauthorized) + http.Redirect(w, r, "/", http.StatusSeeOther) + return // don't call original handler + } + h.ServeHTTP(w, r) + }) +} diff --git a/030_sessions/09_middleware/notes.md b/030_sessions/09_middleware/notes.md new file mode 100644 index 00000000..60f20260 --- /dev/null +++ b/030_sessions/09_middleware/notes.md @@ -0,0 +1,26 @@ +# Step 1: +Created type session which is a struct with a lastActivity field. This will allow us to know the last time a session was used. + +# Step 2: +Updated dbSessions to be of type map[string]session + +# Step 3: +Updated all reads/writes to dbSessions + +# Step 4: +Apply the MaxAge field to cookie + +# Step 5: +Updated func alreadyLoggedIn to be able to set a cookie, adding the ResponseWriter to its parameters + +``` +func alreadyLoggedIn(w http.ResponseWriter, req *http.Request) bool { +``` + +# Step 6: +Added dbSessionsCleaned time.Time to keep track of the last time we cleaned out our sessions. + +# Step 7: +Added func cleanSessions to remove unused sessions from dbSessions. Set it to run whenever someone logs out and a certain amount of time has elapsed (in production you'd set this to run during a time when the server wasn't busy). + + diff --git a/030_sessions/09_middleware/session.go b/030_sessions/09_middleware/session.go new file mode 100644 index 00000000..94488c1b --- /dev/null +++ b/030_sessions/09_middleware/session.go @@ -0,0 +1,71 @@ +package main + +import ( + "fmt" + "github.com/satori/go.uuid" + "net/http" + "time" +) + +func getUser(w http.ResponseWriter, req *http.Request) user { + // get cookie + c, err := req.Cookie("session") + if err != nil { + sID, _ := uuid.NewV4() + c = &http.Cookie{ + Name: "session", + Value: sID.String(), + } + + } + c.MaxAge = sessionLength + http.SetCookie(w, c) + + // if the user exists already, get user + var u user + if s, ok := dbSessions[c.Value]; ok { + s.lastActivity = time.Now() + dbSessions[c.Value] = s + u = dbUsers[s.un] + } + return u +} + +func alreadyLoggedIn(w http.ResponseWriter, req *http.Request) bool { + c, err := req.Cookie("session") + if err != nil { + return false + } + s, ok := dbSessions[c.Value] + if ok { + s.lastActivity = time.Now() + dbSessions[c.Value] = s + } + _, ok = dbUsers[s.un] + // refresh session + c.MaxAge = sessionLength + http.SetCookie(w, c) + return ok +} + +func cleanSessions() { + fmt.Println("BEFORE CLEAN") // for demonstration purposes + showSessions() // for demonstration purposes + for k, v := range dbSessions { + if time.Now().Sub(v.lastActivity) > (time.Second * 30) { + delete(dbSessions, k) + } + } + dbSessionsCleaned = time.Now() + fmt.Println("AFTER CLEAN") // for demonstration purposes + showSessions() // for demonstration purposes +} + +// for demonstration purposes +func showSessions() { + fmt.Println("********") + for k, v := range dbSessions { + fmt.Println(k, v.un) + } + fmt.Println("") +} diff --git a/030_sessions/09_middleware/templates/bar.gohtml b/030_sessions/09_middleware/templates/bar.gohtml new file mode 100644 index 00000000..ddd56835 --- /dev/null +++ b/030_sessions/09_middleware/templates/bar.gohtml @@ -0,0 +1,20 @@ + + + + + BAR + + + +

Welcome to the bar. What can I get you to drink?

+ +{{if .First}} +USER NAME {{.UserName}}
+PASSWORD {{.Password}}
+FIRST {{.First}}
+LAST {{.Last}}
+

log out

+{{end}} + + + \ No newline at end of file diff --git a/030_sessions/09_middleware/templates/index.gohtml b/030_sessions/09_middleware/templates/index.gohtml new file mode 100644 index 00000000..3bc10844 --- /dev/null +++ b/030_sessions/09_middleware/templates/index.gohtml @@ -0,0 +1,24 @@ + + + + + Document + + + +{{if .First}} +USER NAME {{.UserName}}
+PASSWORD {{.Password}}
+FIRST {{.First}}
+LAST {{.Last}}
+

log out

+{{else}} +

sign up

+

log in

+{{end}} + +
+

Go to the bar

+ + + \ No newline at end of file diff --git a/030_sessions/09_middleware/templates/login.gohtml b/030_sessions/09_middleware/templates/login.gohtml new file mode 100644 index 00000000..eb155256 --- /dev/null +++ b/030_sessions/09_middleware/templates/login.gohtml @@ -0,0 +1,18 @@ + + + + + Document + + + +

LOGIN

+
+ + + +
+

signup

+ + + \ No newline at end of file diff --git a/030_sessions/09_middleware/templates/signup.gohtml b/030_sessions/09_middleware/templates/signup.gohtml new file mode 100644 index 00000000..e9188a11 --- /dev/null +++ b/030_sessions/09_middleware/templates/signup.gohtml @@ -0,0 +1,26 @@ + + + + + Document + + + +
+ +
+
+
+
+ + + + +
+ + + \ No newline at end of file diff --git a/030_sessions/10_temp/README.md b/030_sessions/10_temp/README.md new file mode 100644 index 00000000..1ea4b86a --- /dev/null +++ b/030_sessions/10_temp/README.md @@ -0,0 +1,69 @@ +# Concurrency & Race Conditions + +## Could this code cause a race condition? + +``` +go cleanSessions() +``` + +``` +func cleanSessions() { + for k, v := range dbSessions { + if time.Now().Sub(v.lastActivity) > (time.Second * 30) { + delete(dbSessions, k) + } + } + dbSessionsCleaned = time.Now() +} + +``` + +https://golang.org/doc/go1.6 says: + +"The runtime has added lightweight, best-effort detection of concurrent misuse of maps. As always, if one goroutine is writing to a map, no other goroutine should be reading or writing the map concurrently. If the runtime detects this condition, it prints a diagnosis and crashes the program. The best way to find out more about the problem is to run the program under the race detector, which will more reliably identify the race and give more detail." + +When you + +``` +go build -race +``` + +you do not get a race condition reported. + + +![no race condition](norace.png) + +So if you're not writing to a map, you can use the map concurrently without a problem. + +RE: time.Time + +"A Time value can be used by multiple goroutines simultaneously." + +https://godoc.org/time#Time + +## Expanding on maps & goroutines + +Maps are funky. + +Check this out: + +![maps are funky](maps.png) + + +https://play.golang.org/p/62DF4xvPeQ + +**So you can delete something that doesn't exist, and that is not a problem.** + +**And you can ask for something that isn't there, and that is not a problem (gives you the zero value for the map's value).** + +Deleting IS DIFFERENT from writing. + +**If more than 1 goroutine tried to delete that same entry in the map: no problem.** + +**And if you're reading from the map and a value isn't there: no problem.** + +So why is WRITING a problem with concurrency? + +The classic race condition example is two routines READING, pulling the same value, each incrementing the value, and then each WRITING the incremented value back, and the value is incremented only 1, instead of 2. + +Just remember: WRITE TO MAP = concurrency considerations. \ No newline at end of file diff --git a/030_sessions/10_temp/main.go b/030_sessions/10_temp/main.go new file mode 100644 index 00000000..38998347 --- /dev/null +++ b/030_sessions/10_temp/main.go @@ -0,0 +1,178 @@ +package main + +import ( + "github.com/satori/go.uuid" + "golang.org/x/crypto/bcrypt" + "html/template" + "net/http" + "time" +) + +type user struct { + UserName string + Password []byte + First string + Last string + Role string +} + +type session struct { + un string + lastActivity time.Time +} + +var tpl *template.Template +var dbUsers = map[string]user{} // user ID, user +var dbSessions = map[string]session{} // session ID, session +var dbSessionsCleaned time.Time + +const sessionLength int = 30 + +func init() { + tpl = template.Must(template.ParseGlob("templates/*")) + dbSessionsCleaned = time.Now() +} + +func main() { + http.HandleFunc("/", index) + http.HandleFunc("/bar", bar) + http.HandleFunc("/signup", signup) + http.HandleFunc("/login", login) + http.HandleFunc("/logout", authorized(logout)) + http.Handle("/favicon.ico", http.NotFoundHandler()) + http.ListenAndServe(":8080", nil) +} + +func index(w http.ResponseWriter, req *http.Request) { + u := getUser(w, req) + showSessions() // for demonstration purposes + tpl.ExecuteTemplate(w, "index.gohtml", u) +} + +func bar(w http.ResponseWriter, req *http.Request) { + u := getUser(w, req) + if !alreadyLoggedIn(w, req) { + http.Redirect(w, req, "/", http.StatusSeeOther) + return + } + if u.Role != "007" { + http.Error(w, "You must be 007 to enter the bar", http.StatusForbidden) + return + } + showSessions() // for demonstration purposes + tpl.ExecuteTemplate(w, "bar.gohtml", u) +} + +func signup(w http.ResponseWriter, req *http.Request) { + if alreadyLoggedIn(w, req) { + http.Redirect(w, req, "/", http.StatusSeeOther) + return + } + var u user + // process form submission + if req.Method == http.MethodPost { + // get form values + un := req.FormValue("username") + p := req.FormValue("password") + f := req.FormValue("firstname") + l := req.FormValue("lastname") + r := req.FormValue("role") + // username taken? + if _, ok := dbUsers[un]; ok { + http.Error(w, "Username already taken", http.StatusForbidden) + return + } + // create session + sID, _ := uuid.NewV4() + c := &http.Cookie{ + Name: "session", + Value: sID.String(), + } + c.MaxAge = sessionLength + http.SetCookie(w, c) + dbSessions[c.Value] = session{un, time.Now()} + // store user in dbUsers + bs, err := bcrypt.GenerateFromPassword([]byte(p), bcrypt.MinCost) + if err != nil { + http.Error(w, "Internal server error", http.StatusInternalServerError) + return + } + u = user{un, bs, f, l, r} + dbUsers[un] = u + // redirect + http.Redirect(w, req, "/", http.StatusSeeOther) + return + } + showSessions() // for demonstration purposes + tpl.ExecuteTemplate(w, "signup.gohtml", u) +} + +func login(w http.ResponseWriter, req *http.Request) { + if alreadyLoggedIn(w, req) { + http.Redirect(w, req, "/", http.StatusSeeOther) + return + } + var u user + // process form submission + if req.Method == http.MethodPost { + un := req.FormValue("username") + p := req.FormValue("password") + // is there a username? + u, ok := dbUsers[un] + if !ok { + http.Error(w, "Username and/or password do not match", http.StatusForbidden) + return + } + // does the entered password match the stored password? + err := bcrypt.CompareHashAndPassword(u.Password, []byte(p)) + if err != nil { + http.Error(w, "Username and/or password do not match", http.StatusForbidden) + return + } + // create session + sID, _ := uuid.NewV4() + c := &http.Cookie{ + Name: "session", + Value: sID.String(), + } + c.MaxAge = sessionLength + http.SetCookie(w, c) + dbSessions[c.Value] = session{un, time.Now()} + http.Redirect(w, req, "/", http.StatusSeeOther) + return + } + showSessions() // for demonstration purposes + tpl.ExecuteTemplate(w, "login.gohtml", u) +} + +func logout(w http.ResponseWriter, req *http.Request) { + c, _ := req.Cookie("session") + // delete the session + delete(dbSessions, c.Value) + // remove the cookie + c = &http.Cookie{ + Name: "session", + Value: "", + MaxAge: -1, + } + http.SetCookie(w, c) + + // clean up dbSessions + if time.Now().Sub(dbSessionsCleaned) > (time.Second * 30) { + go cleanSessions() + } + + http.Redirect(w, req, "/login", http.StatusSeeOther) +} + +func authorized(h http.HandlerFunc) http.HandlerFunc { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + // code before + if !alreadyLoggedIn(w, r) { + http.Redirect(w, r, "/", http.StatusSeeOther) + return + } + h.ServeHTTP(w, r) + // code after + }) +} diff --git a/030_sessions/10_temp/maps.png b/030_sessions/10_temp/maps.png new file mode 100644 index 00000000..6544facc Binary files /dev/null and b/030_sessions/10_temp/maps.png differ diff --git a/030_sessions/10_temp/norace.png b/030_sessions/10_temp/norace.png new file mode 100644 index 00000000..e3f6f33e Binary files /dev/null and b/030_sessions/10_temp/norace.png differ diff --git a/030_sessions/10_temp/notes.md b/030_sessions/10_temp/notes.md new file mode 100644 index 00000000..60f20260 --- /dev/null +++ b/030_sessions/10_temp/notes.md @@ -0,0 +1,26 @@ +# Step 1: +Created type session which is a struct with a lastActivity field. This will allow us to know the last time a session was used. + +# Step 2: +Updated dbSessions to be of type map[string]session + +# Step 3: +Updated all reads/writes to dbSessions + +# Step 4: +Apply the MaxAge field to cookie + +# Step 5: +Updated func alreadyLoggedIn to be able to set a cookie, adding the ResponseWriter to its parameters + +``` +func alreadyLoggedIn(w http.ResponseWriter, req *http.Request) bool { +``` + +# Step 6: +Added dbSessionsCleaned time.Time to keep track of the last time we cleaned out our sessions. + +# Step 7: +Added func cleanSessions to remove unused sessions from dbSessions. Set it to run whenever someone logs out and a certain amount of time has elapsed (in production you'd set this to run during a time when the server wasn't busy). + + diff --git a/030_sessions/10_temp/session.go b/030_sessions/10_temp/session.go new file mode 100644 index 00000000..94488c1b --- /dev/null +++ b/030_sessions/10_temp/session.go @@ -0,0 +1,71 @@ +package main + +import ( + "fmt" + "github.com/satori/go.uuid" + "net/http" + "time" +) + +func getUser(w http.ResponseWriter, req *http.Request) user { + // get cookie + c, err := req.Cookie("session") + if err != nil { + sID, _ := uuid.NewV4() + c = &http.Cookie{ + Name: "session", + Value: sID.String(), + } + + } + c.MaxAge = sessionLength + http.SetCookie(w, c) + + // if the user exists already, get user + var u user + if s, ok := dbSessions[c.Value]; ok { + s.lastActivity = time.Now() + dbSessions[c.Value] = s + u = dbUsers[s.un] + } + return u +} + +func alreadyLoggedIn(w http.ResponseWriter, req *http.Request) bool { + c, err := req.Cookie("session") + if err != nil { + return false + } + s, ok := dbSessions[c.Value] + if ok { + s.lastActivity = time.Now() + dbSessions[c.Value] = s + } + _, ok = dbUsers[s.un] + // refresh session + c.MaxAge = sessionLength + http.SetCookie(w, c) + return ok +} + +func cleanSessions() { + fmt.Println("BEFORE CLEAN") // for demonstration purposes + showSessions() // for demonstration purposes + for k, v := range dbSessions { + if time.Now().Sub(v.lastActivity) > (time.Second * 30) { + delete(dbSessions, k) + } + } + dbSessionsCleaned = time.Now() + fmt.Println("AFTER CLEAN") // for demonstration purposes + showSessions() // for demonstration purposes +} + +// for demonstration purposes +func showSessions() { + fmt.Println("********") + for k, v := range dbSessions { + fmt.Println(k, v.un) + } + fmt.Println("") +} diff --git a/030_sessions/10_temp/templates/bar.gohtml b/030_sessions/10_temp/templates/bar.gohtml new file mode 100644 index 00000000..ddd56835 --- /dev/null +++ b/030_sessions/10_temp/templates/bar.gohtml @@ -0,0 +1,20 @@ + + + + + BAR + + + +

Welcome to the bar. What can I get you to drink?

+ +{{if .First}} +USER NAME {{.UserName}}
+PASSWORD {{.Password}}
+FIRST {{.First}}
+LAST {{.Last}}
+

log out

+{{end}} + + + \ No newline at end of file diff --git a/030_sessions/10_temp/templates/index.gohtml b/030_sessions/10_temp/templates/index.gohtml new file mode 100644 index 00000000..3bc10844 --- /dev/null +++ b/030_sessions/10_temp/templates/index.gohtml @@ -0,0 +1,24 @@ + + + + + Document + + + +{{if .First}} +USER NAME {{.UserName}}
+PASSWORD {{.Password}}
+FIRST {{.First}}
+LAST {{.Last}}
+

log out

+{{else}} +

sign up

+

log in

+{{end}} + +
+

Go to the bar

+ + + \ No newline at end of file diff --git a/030_sessions/10_temp/templates/login.gohtml b/030_sessions/10_temp/templates/login.gohtml new file mode 100644 index 00000000..eb155256 --- /dev/null +++ b/030_sessions/10_temp/templates/login.gohtml @@ -0,0 +1,18 @@ + + + + + Document + + + +

LOGIN

+
+ + + +
+

signup

+ + + \ No newline at end of file diff --git a/030_sessions/10_temp/templates/signup.gohtml b/030_sessions/10_temp/templates/signup.gohtml new file mode 100644 index 00000000..e9188a11 --- /dev/null +++ b/030_sessions/10_temp/templates/signup.gohtml @@ -0,0 +1,26 @@ + + + + + Document + + + +
+ +
+
+
+
+ + + + +
+ + + \ No newline at end of file diff --git a/031_aws/01_hello/01_hello b/031_aws/01_hello/01_hello deleted file mode 100644 index 14523717..00000000 Binary files a/031_aws/01_hello/01_hello and /dev/null differ diff --git a/031_aws/01_hello/mybinary b/031_aws/01_hello/mybinary new file mode 100755 index 00000000..19367514 Binary files /dev/null and b/031_aws/01_hello/mybinary differ diff --git a/031_aws/02_hands-on/02_solution/README.md b/031_aws/02_hands-on/02_solution/README.md index 71487cb3..009c7466 100644 --- a/031_aws/02_hands-on/02_solution/README.md +++ b/031_aws/02_hands-on/02_solution/README.md @@ -13,7 +13,7 @@ 1. copy binary to the server -1. copy you "templates" to the server +1. copy your "templates" to the server - scp -i /path/to/[your].pem templates/* ubuntu@[public-DNS]:/home/ubuntu/templates 1. chmod permissions on your binary diff --git a/031_aws/02_hands-on/02_solution/main.go b/031_aws/02_hands-on/02_solution/main.go index 5ee06052..e6e9a4cb 100644 --- a/031_aws/02_hands-on/02_solution/main.go +++ b/031_aws/02_hands-on/02_solution/main.go @@ -83,7 +83,7 @@ func signup(w http.ResponseWriter, req *http.Request) { return } // create session - sID := uuid.NewV4() + sID, _ := uuid.NewV4() c := &http.Cookie{ Name: "session", Value: sID.String(), @@ -130,7 +130,7 @@ func login(w http.ResponseWriter, req *http.Request) { return } // create session - sID := uuid.NewV4() + sID, _ := uuid.NewV4() c := &http.Cookie{ Name: "session", Value: sID.String(), diff --git a/031_aws/02_hands-on/02_solution/session.go b/031_aws/02_hands-on/02_solution/session.go index 45f272d6..94488c1b 100644 --- a/031_aws/02_hands-on/02_solution/session.go +++ b/031_aws/02_hands-on/02_solution/session.go @@ -11,7 +11,7 @@ func getUser(w http.ResponseWriter, req *http.Request) user { // get cookie c, err := req.Cookie("session") if err != nil { - sID := uuid.NewV4() + sID, _ := uuid.NewV4() c = &http.Cookie{ Name: "session", Value: sID.String(), diff --git a/032_rdbms/02_SQL/main.go b/032_rdbms/02_SQL/main.go index cf72e18c..d4722b24 100644 --- a/032_rdbms/02_SQL/main.go +++ b/032_rdbms/02_SQL/main.go @@ -40,6 +40,7 @@ func index(w http.ResponseWriter, req *http.Request) { func amigos(w http.ResponseWriter, req *http.Request) { rows, err := db.Query(`SELECT aName FROM amigos;`) check(err) + defer rows.Close() // data to be used in query var s, name string @@ -58,6 +59,7 @@ func create(w http.ResponseWriter, req *http.Request) { stmt, err := db.Prepare(`CREATE TABLE customer (name VARCHAR(20));`) check(err) + defer stmt.Close() r, err := stmt.Exec() check(err) @@ -72,6 +74,7 @@ func insert(w http.ResponseWriter, req *http.Request) { stmt, err := db.Prepare(`INSERT INTO customer VALUES ("James");`) check(err) + defer stmt.Close() r, err := stmt.Exec() check(err) @@ -85,6 +88,7 @@ func insert(w http.ResponseWriter, req *http.Request) { func read(w http.ResponseWriter, req *http.Request) { rows, err := db.Query(`SELECT * FROM customer;`) check(err) + defer rows.Close() var name string for rows.Next() { @@ -97,6 +101,7 @@ func read(w http.ResponseWriter, req *http.Request) { func update(w http.ResponseWriter, req *http.Request) { stmt, err := db.Prepare(`UPDATE customer SET name="Jimmy" WHERE name="James";`) check(err) + defer stmt.Close() r, err := stmt.Exec() check(err) @@ -110,6 +115,7 @@ func update(w http.ResponseWriter, req *http.Request) { func del(w http.ResponseWriter, req *http.Request) { stmt, err := db.Prepare(`DELETE FROM customer WHERE name="Jimmy";`) check(err) + defer stmt.Close() r, err := stmt.Exec() check(err) @@ -123,6 +129,7 @@ func del(w http.ResponseWriter, req *http.Request) { func drop(w http.ResponseWriter, req *http.Request) { stmt, err := db.Prepare(`DROP TABLE customer;`) check(err) + defer stmt.Close() _, err = stmt.Exec() check(err) diff --git a/034_photo-blog/02_cookie/main.go b/034_photo-blog/02_cookie/main.go index 7f9ba07b..283e0e1d 100644 --- a/034_photo-blog/02_cookie/main.go +++ b/034_photo-blog/02_cookie/main.go @@ -27,7 +27,7 @@ func index(w http.ResponseWriter, req *http.Request) { func getCookie(w http.ResponseWriter, req *http.Request) *http.Cookie { c, err := req.Cookie("session") if err != nil { - sID := uuid.NewV4() + sID, _ := uuid.NewV4() c = &http.Cookie{ Name: "session", Value: sID.String(), diff --git a/034_photo-blog/03_store-values/main.go b/034_photo-blog/03_store-values/main.go index 1e573ce9..3397de56 100644 --- a/034_photo-blog/03_store-values/main.go +++ b/034_photo-blog/03_store-values/main.go @@ -29,7 +29,7 @@ func index(w http.ResponseWriter, req *http.Request) { func getCookie(w http.ResponseWriter, req *http.Request) *http.Cookie { c, err := req.Cookie("session") if err != nil { - sID := uuid.NewV4() + sID, _ := uuid.NewV4() c = &http.Cookie{ Name: "session", Value: sID.String(), diff --git a/034_photo-blog/04_upload-pictures/main.go b/034_photo-blog/04_upload-pictures/main.go index c4a1c6d9..e950302c 100644 --- a/034_photo-blog/04_upload-pictures/main.go +++ b/034_photo-blog/04_upload-pictures/main.go @@ -62,7 +62,7 @@ func index(w http.ResponseWriter, req *http.Request) { func getCookie(w http.ResponseWriter, req *http.Request) *http.Cookie { c, err := req.Cookie("session") if err != nil { - sID := uuid.NewV4() + sID, _ := uuid.NewV4() c = &http.Cookie{ Name: "session", Value: sID.String(), diff --git a/034_photo-blog/05_display-pictures/main.go b/034_photo-blog/05_display-pictures/main.go index 27f1f054..cc8344c4 100644 --- a/034_photo-blog/05_display-pictures/main.go +++ b/034_photo-blog/05_display-pictures/main.go @@ -64,7 +64,7 @@ func index(w http.ResponseWriter, req *http.Request) { func getCookie(w http.ResponseWriter, req *http.Request) *http.Cookie { c, err := req.Cookie("session") if err != nil { - sID := uuid.NewV4() + sID, _ := uuid.NewV4() c = &http.Cookie{ Name: "session", Value: sID.String(), diff --git a/035_hmac/02/main.go b/035_hmac/02/main.go index ace275cc..316fffd0 100644 --- a/035_hmac/02/main.go +++ b/035_hmac/02/main.go @@ -61,7 +61,7 @@ func auth(w http.ResponseWriter, req *http.Request) { xs := strings.Split(c.Value, "|") email := xs[0] codeRcvd := xs[1] - codeCheck := getCode(email) + codeCheck := getCode(email + "s") if codeRcvd != codeCheck { fmt.Println("HMAC codes didn't match") diff --git a/038_context/04/README.md b/038_context/04/README.md index f01071e4..ce43b8b4 100644 --- a/038_context/04/README.md +++ b/038_context/04/README.md @@ -1,3 +1,3 @@ Context makes it possible to manage a chain of calls within the same call path by signaling context’s Done channel. -source: http://golang.rakyll.org/leakingctx/ \ No newline at end of file +source: https://rakyll.org/leakingctx/ \ No newline at end of file diff --git a/040_json/01/main.go b/040_json/01/main.go index 387e87c6..4e76ac7b 100644 --- a/040_json/01/main.go +++ b/040_json/01/main.go @@ -36,23 +36,23 @@ func foo(w http.ResponseWriter, req *http.Request) { func mshl(w http.ResponseWriter, req *http.Request) { w.Header().Set("Content-Type", "application/json") p1 := person{ - "James", - "Bond", - []string{"Suit", "Gun", "Wry sense of humor"}, + Fname: "James", + Lname: "Bond", + Items: []string{"Suit", "Gun", "Wry sense of humor"}, } - json, err := json.Marshal(p1) + j, err := json.Marshal(p1) if err != nil { log.Println(err) } - w.Write(json) + w.Write(j) } func encd(w http.ResponseWriter, req *http.Request) { w.Header().Set("Content-Type", "application/json") p1 := person{ - "James", - "Bond", - []string{"Suit", "Gun", "Wry sense of humor"}, + Fname: "James", + Lname: "Bond", + Items: []string{"Suit", "Gun", "Wry sense of humor"}, } err := json.NewEncoder(w).Encode(p1) if err != nil { diff --git a/041_ajax/01/index.html b/041_ajax/01/index.html index 9f7af194..e4ec0e0a 100644 --- a/041_ajax/01/index.html +++ b/041_ajax/01/index.html @@ -31,7 +31,7 @@

Make a request

var xhr = new XMLHttpRequest(); // open the request - xhr.open('GET', 'http://github.com/test.html', true); + xhr.open('GET', 'http://github.com/test.html'); // handles the response xhr.onreadystatechange = function () { diff --git a/041_ajax/02/01/main.go b/041_ajax/02/01/main.go index 576aa2a5..0244f70d 100644 --- a/041_ajax/02/01/main.go +++ b/041_ajax/02/01/main.go @@ -1,9 +1,9 @@ package main import ( + "fmt" "html/template" "net/http" - "fmt" ) var tpl *template.Template diff --git a/041_ajax/02/02/main.go b/041_ajax/02/02/main.go index 576aa2a5..0244f70d 100644 --- a/041_ajax/02/02/main.go +++ b/041_ajax/02/02/main.go @@ -1,9 +1,9 @@ package main import ( + "fmt" "html/template" "net/http" - "fmt" ) var tpl *template.Template diff --git a/041_ajax/03/main.go b/041_ajax/03/main.go index af7dfe6f..c230c8f6 100644 --- a/041_ajax/03/main.go +++ b/041_ajax/03/main.go @@ -1,13 +1,13 @@ package main import ( + "fmt" "github.com/satori/go.uuid" "golang.org/x/crypto/bcrypt" "html/template" + "io/ioutil" "net/http" "time" - "fmt" - "io/ioutil" ) type user struct { @@ -87,7 +87,7 @@ func signup(w http.ResponseWriter, req *http.Request) { return } // create session - sID := uuid.NewV4() + sID, _ := uuid.NewV4() c := &http.Cookie{ Name: "session", Value: sID.String(), @@ -134,7 +134,7 @@ func login(w http.ResponseWriter, req *http.Request) { return } // create session - sID := uuid.NewV4() + sID, _ := uuid.NewV4() c := &http.Cookie{ Name: "session", Value: sID.String(), @@ -176,8 +176,8 @@ func logout(w http.ResponseWriter, req *http.Request) { func checkUserName(w http.ResponseWriter, req *http.Request) { sampleUsers := map[string]bool{ "test@example.com": true, - "jame@bond.com": true, - "moneyp@uk.gov": true, + "jame@bond.com": true, + "moneyp@uk.gov": true, } bs, err := ioutil.ReadAll(req.Body) diff --git a/041_ajax/03/session.go b/041_ajax/03/session.go index 45f272d6..94488c1b 100644 --- a/041_ajax/03/session.go +++ b/041_ajax/03/session.go @@ -11,7 +11,7 @@ func getUser(w http.ResponseWriter, req *http.Request) user { // get cookie c, err := req.Cookie("session") if err != nil { - sID := uuid.NewV4() + sID, _ := uuid.NewV4() c = &http.Cookie{ Name: "session", Value: sID.String(), diff --git a/042_mongodb/02_json/main.go b/042_mongodb/02_json/main.go index 8ccb283c..b64a1f0b 100644 --- a/042_mongodb/02_json/main.go +++ b/042_mongodb/02_json/main.go @@ -3,9 +3,9 @@ package main import ( "encoding/json" "fmt" + "github.com/GoesToEleven/golang-web-dev/042_mongodb/02_json/models" "github.com/julienschmidt/httprouter" "net/http" - "github.com/GoesToEleven/golang-web-dev/042_mongodb/02_json/models" ) func main() { diff --git a/042_mongodb/02_json/models/user.go b/042_mongodb/02_json/models/user.go index e653f1a7..63a12406 100644 --- a/042_mongodb/02_json/models/user.go +++ b/042_mongodb/02_json/models/user.go @@ -5,4 +5,4 @@ type User struct { Gender string `json:"gender"` Age int `json:"age"` Id string `json:"id"` -} \ No newline at end of file +} diff --git a/042_mongodb/03_post-delete/main.go b/042_mongodb/03_post-delete/main.go index 385d68f9..abc773ac 100644 --- a/042_mongodb/03_post-delete/main.go +++ b/042_mongodb/03_post-delete/main.go @@ -3,9 +3,9 @@ package main import ( "encoding/json" "fmt" + "github.com/GoesToEleven/golang-web-dev/042_mongodb/03_post-delete/models" "github.com/julienschmidt/httprouter" "net/http" - "github.com/GoesToEleven/golang-web-dev/042_mongodb/03_post-delete/models" ) func main() { diff --git a/042_mongodb/04_controllers/controllers/user.go b/042_mongodb/04_controllers/controllers/user.go index 9ecc135e..6721c792 100644 --- a/042_mongodb/04_controllers/controllers/user.go +++ b/042_mongodb/04_controllers/controllers/user.go @@ -3,9 +3,9 @@ package controllers import ( "encoding/json" "fmt" + "github.com/GoesToEleven/golang-web-dev/042_mongodb/04_controllers/models" "github.com/julienschmidt/httprouter" "net/http" - "github.com/GoesToEleven/golang-web-dev/042_mongodb/04_controllers/models" ) type UserController struct{} diff --git a/042_mongodb/04_controllers/main.go b/042_mongodb/04_controllers/main.go index 3705b0c5..2d701eaa 100644 --- a/042_mongodb/04_controllers/main.go +++ b/042_mongodb/04_controllers/main.go @@ -1,9 +1,9 @@ package main import ( + "github.com/GoesToEleven/golang-web-dev/042_mongodb/04_controllers/controllers" "github.com/julienschmidt/httprouter" "net/http" - "github.com/GoesToEleven/golang-web-dev/042_mongodb/04_controllers/controllers" ) func main() { diff --git a/042_mongodb/05_mongodb/01_update-user-controller/controllers/user.go b/042_mongodb/05_mongodb/01_update-user-controller/controllers/user.go index 5ec1a5af..ec99de83 100644 --- a/042_mongodb/05_mongodb/01_update-user-controller/controllers/user.go +++ b/042_mongodb/05_mongodb/01_update-user-controller/controllers/user.go @@ -3,10 +3,10 @@ package controllers import ( "encoding/json" "fmt" + "github.com/GoesToEleven/golang-web-dev/042_mongodb/05_mongodb/01_update-user-controller/models" "github.com/julienschmidt/httprouter" "gopkg.in/mgo.v2" "net/http" - "github.com/GoesToEleven/golang-web-dev/042_mongodb/05_mongodb/01_update-user-controller/models" ) // added session to our userController diff --git a/042_mongodb/05_mongodb/01_update-user-controller/main.go b/042_mongodb/05_mongodb/01_update-user-controller/main.go index 11e580a3..f3fdcfef 100644 --- a/042_mongodb/05_mongodb/01_update-user-controller/main.go +++ b/042_mongodb/05_mongodb/01_update-user-controller/main.go @@ -1,10 +1,10 @@ package main import ( + "github.com/GoesToEleven/golang-web-dev/042_mongodb/05_mongodb/01_update-user-controller/controllers" "github.com/julienschmidt/httprouter" "gopkg.in/mgo.v2" "net/http" - "github.com/GoesToEleven/golang-web-dev/042_mongodb/05_mongodb/01_update-user-controller/controllers" ) func main() { diff --git a/042_mongodb/05_mongodb/02_update-user-model/controllers/user.go b/042_mongodb/05_mongodb/02_update-user-model/controllers/user.go index 8d9a66ed..347fcbf4 100644 --- a/042_mongodb/05_mongodb/02_update-user-model/controllers/user.go +++ b/042_mongodb/05_mongodb/02_update-user-model/controllers/user.go @@ -3,10 +3,10 @@ package controllers import ( "encoding/json" "fmt" + "github.com/GoesToEleven/golang-web-dev/042_mongodb/05_mongodb/02_update-user-model/models" "github.com/julienschmidt/httprouter" "gopkg.in/mgo.v2" "net/http" - "github.com/GoesToEleven/golang-web-dev/042_mongodb/05_mongodb/02_update-user-model/models" ) type UserController struct { diff --git a/042_mongodb/05_mongodb/02_update-user-model/main.go b/042_mongodb/05_mongodb/02_update-user-model/main.go index f3323dbb..bb869598 100644 --- a/042_mongodb/05_mongodb/02_update-user-model/main.go +++ b/042_mongodb/05_mongodb/02_update-user-model/main.go @@ -1,10 +1,10 @@ package main import ( + "github.com/GoesToEleven/golang-web-dev/042_mongodb/05_mongodb/02_update-user-model/controllers" "github.com/julienschmidt/httprouter" "gopkg.in/mgo.v2" "net/http" - "github.com/GoesToEleven/golang-web-dev/042_mongodb/05_mongodb/02_update-user-model/controllers" ) func main() { diff --git a/042_mongodb/05_mongodb/03_update-user-controllers-post/controllers/user.go b/042_mongodb/05_mongodb/03_update-user-controllers-post/controllers/user.go index 13ed9199..ab4b90d4 100644 --- a/042_mongodb/05_mongodb/03_update-user-controllers-post/controllers/user.go +++ b/042_mongodb/05_mongodb/03_update-user-controllers-post/controllers/user.go @@ -3,11 +3,11 @@ package controllers import ( "encoding/json" "fmt" + "github.com/GoesToEleven/golang-web-dev/042_mongodb/05_mongodb/03_update-user-controllers-post/models" "github.com/julienschmidt/httprouter" "gopkg.in/mgo.v2" "gopkg.in/mgo.v2/bson" "net/http" - "github.com/GoesToEleven/golang-web-dev/042_mongodb/05_mongodb/03_update-user-controllers-post/models" ) type UserController struct { diff --git a/042_mongodb/05_mongodb/03_update-user-controllers-post/main.go b/042_mongodb/05_mongodb/03_update-user-controllers-post/main.go index fb69d962..d7ce9016 100644 --- a/042_mongodb/05_mongodb/03_update-user-controllers-post/main.go +++ b/042_mongodb/05_mongodb/03_update-user-controllers-post/main.go @@ -1,10 +1,10 @@ package main import ( + "github.com/GoesToEleven/golang-web-dev/042_mongodb/05_mongodb/03_update-user-controllers-post/controllers" "github.com/julienschmidt/httprouter" "gopkg.in/mgo.v2" "net/http" - "github.com/GoesToEleven/golang-web-dev/042_mongodb/05_mongodb/03_update-user-controllers-post/controllers" ) func main() { diff --git a/042_mongodb/05_mongodb/03_update-user-controllers-post/models/user.go b/042_mongodb/05_mongodb/03_update-user-controllers-post/models/user.go index 28cc93d0..9ae0fb52 100644 --- a/042_mongodb/05_mongodb/03_update-user-controllers-post/models/user.go +++ b/042_mongodb/05_mongodb/03_update-user-controllers-post/models/user.go @@ -7,4 +7,4 @@ type User struct { Gender string `json:"gender" bson:"gender"` Age int `json:"age" bson:"age"` Id bson.ObjectId `json:"id" bson:"_id"` -} \ No newline at end of file +} diff --git a/042_mongodb/05_mongodb/04_update-user-controllers-get/controllers/user.go b/042_mongodb/05_mongodb/04_update-user-controllers-get/controllers/user.go index 89a6c308..cbbc6e75 100644 --- a/042_mongodb/05_mongodb/04_update-user-controllers-get/controllers/user.go +++ b/042_mongodb/05_mongodb/04_update-user-controllers-get/controllers/user.go @@ -3,11 +3,11 @@ package controllers import ( "encoding/json" "fmt" + "github.com/GoesToEleven/golang-web-dev/042_mongodb/05_mongodb/04_update-user-controllers-get/models" "github.com/julienschmidt/httprouter" "gopkg.in/mgo.v2" "gopkg.in/mgo.v2/bson" "net/http" - "github.com/GoesToEleven/golang-web-dev/042_mongodb/05_mongodb/04_update-user-controllers-get/models" ) type UserController struct { diff --git a/042_mongodb/05_mongodb/04_update-user-controllers-get/main.go b/042_mongodb/05_mongodb/04_update-user-controllers-get/main.go index d8011616..336cdf50 100644 --- a/042_mongodb/05_mongodb/04_update-user-controllers-get/main.go +++ b/042_mongodb/05_mongodb/04_update-user-controllers-get/main.go @@ -1,10 +1,10 @@ package main import ( + "github.com/GoesToEleven/golang-web-dev/042_mongodb/05_mongodb/04_update-user-controllers-get/controllers" "github.com/julienschmidt/httprouter" "gopkg.in/mgo.v2" "net/http" - "github.com/GoesToEleven/golang-web-dev/042_mongodb/05_mongodb/04_update-user-controllers-get/controllers" ) func main() { diff --git a/042_mongodb/05_mongodb/04_update-user-controllers-get/models/user.go b/042_mongodb/05_mongodb/04_update-user-controllers-get/models/user.go index 1c608ce2..7892c44f 100644 --- a/042_mongodb/05_mongodb/04_update-user-controllers-get/models/user.go +++ b/042_mongodb/05_mongodb/04_update-user-controllers-get/models/user.go @@ -7,4 +7,4 @@ type User struct { Name string `json:"name" bson:"name"` Gender string `json:"gender" bson:"gender"` Age int `json:"age" bson:"age"` -} \ No newline at end of file +} diff --git a/042_mongodb/05_mongodb/05_update-user-controllers-delete/controllers/user.go b/042_mongodb/05_mongodb/05_update-user-controllers-delete/controllers/user.go index 468a6804..a6a3fbaa 100644 --- a/042_mongodb/05_mongodb/05_update-user-controllers-delete/controllers/user.go +++ b/042_mongodb/05_mongodb/05_update-user-controllers-delete/controllers/user.go @@ -3,11 +3,11 @@ package controllers import ( "encoding/json" "fmt" + "github.com/GoesToEleven/golang-web-dev/042_mongodb/05_mongodb/05_update-user-controllers-delete/models" "github.com/julienschmidt/httprouter" "gopkg.in/mgo.v2" "gopkg.in/mgo.v2/bson" "net/http" - "github.com/GoesToEleven/golang-web-dev/042_mongodb/05_mongodb/05_update-user-controllers-delete/models" ) type UserController struct { diff --git a/042_mongodb/05_mongodb/05_update-user-controllers-delete/models/user.go b/042_mongodb/05_mongodb/05_update-user-controllers-delete/models/user.go index 1c608ce2..7892c44f 100644 --- a/042_mongodb/05_mongodb/05_update-user-controllers-delete/models/user.go +++ b/042_mongodb/05_mongodb/05_update-user-controllers-delete/models/user.go @@ -7,4 +7,4 @@ type User struct { Name string `json:"name" bson:"name"` Gender string `json:"gender" bson:"gender"` Age int `json:"age" bson:"age"` -} \ No newline at end of file +} diff --git a/042_mongodb/07_solution/controllers/user.go b/042_mongodb/07_solution/controllers/user.go index ca805d08..79f65b4a 100644 --- a/042_mongodb/07_solution/controllers/user.go +++ b/042_mongodb/07_solution/controllers/user.go @@ -3,10 +3,10 @@ package controllers import ( "encoding/json" "fmt" + "github.com/GoesToEleven/golang-web-dev/042_mongodb/07_solution/models" "github.com/julienschmidt/httprouter" "github.com/satori/go.uuid" "net/http" - "github.com/GoesToEleven/golang-web-dev/042_mongodb/07_solution/models" ) type UserController struct { diff --git a/042_mongodb/07_solution/main.go b/042_mongodb/07_solution/main.go index 3bb68984..95e80c79 100644 --- a/042_mongodb/07_solution/main.go +++ b/042_mongodb/07_solution/main.go @@ -1,10 +1,10 @@ package main import ( - "github.com/julienschmidt/httprouter" - "net/http" "github.com/GoesToEleven/golang-web-dev/042_mongodb/07_solution/controllers" "github.com/GoesToEleven/golang-web-dev/042_mongodb/07_solution/models" + "github.com/julienschmidt/httprouter" + "net/http" ) func main() { diff --git a/042_mongodb/09_solution/controllers/user.go b/042_mongodb/09_solution/controllers/user.go index 56c9f88b..8d13d344 100644 --- a/042_mongodb/09_solution/controllers/user.go +++ b/042_mongodb/09_solution/controllers/user.go @@ -3,10 +3,10 @@ package controllers import ( "encoding/json" "fmt" + "github.com/GoesToEleven/golang-web-dev/042_mongodb/09_solution/models" "github.com/julienschmidt/httprouter" "github.com/satori/go.uuid" "net/http" - "github.com/GoesToEleven/golang-web-dev/042_mongodb/09_solution/models" ) type UserController struct { diff --git a/042_mongodb/09_solution/main.go b/042_mongodb/09_solution/main.go index 8a4d2c3b..0b712be6 100644 --- a/042_mongodb/09_solution/main.go +++ b/042_mongodb/09_solution/main.go @@ -1,10 +1,10 @@ package main import ( - "github.com/julienschmidt/httprouter" - "net/http" "github.com/GoesToEleven/golang-web-dev/042_mongodb/09_solution/controllers" "github.com/GoesToEleven/golang-web-dev/042_mongodb/09_solution/models" + "github.com/julienschmidt/httprouter" + "net/http" ) func main() { diff --git a/042_mongodb/10_hands-on/starting-code/main.go b/042_mongodb/10_hands-on/starting-code/main.go index 0e4972b3..690231d9 100644 --- a/042_mongodb/10_hands-on/starting-code/main.go +++ b/042_mongodb/10_hands-on/starting-code/main.go @@ -83,7 +83,7 @@ func signup(w http.ResponseWriter, req *http.Request) { return } // create session - sID := uuid.NewV4() + sID, _ := uuid.NewV4() c := &http.Cookie{ Name: "session", Value: sID.String(), @@ -130,7 +130,7 @@ func login(w http.ResponseWriter, req *http.Request) { return } // create session - sID := uuid.NewV4() + sID, _ := uuid.NewV4() c := &http.Cookie{ Name: "session", Value: sID.String(), diff --git a/042_mongodb/10_hands-on/starting-code/session.go b/042_mongodb/10_hands-on/starting-code/session.go index 45f272d6..94488c1b 100644 --- a/042_mongodb/10_hands-on/starting-code/session.go +++ b/042_mongodb/10_hands-on/starting-code/session.go @@ -11,7 +11,7 @@ func getUser(w http.ResponseWriter, req *http.Request) user { // get cookie c, err := req.Cookie("session") if err != nil { - sID := uuid.NewV4() + sID, _ := uuid.NewV4() c = &http.Cookie{ Name: "session", Value: sID.String(), diff --git a/042_mongodb/11_solution/controllers/general.go b/042_mongodb/11_solution/controllers/general.go index 0e08165c..b9236ab5 100644 --- a/042_mongodb/11_solution/controllers/general.go +++ b/042_mongodb/11_solution/controllers/general.go @@ -1,9 +1,9 @@ package controllers import ( + "github.com/GoesToEleven/golang-web-dev/042_mongodb/11_solution/session" "html/template" "net/http" - "github.com/GoesToEleven/golang-web-dev/042_mongodb/11_solution/session" ) type Controller struct { diff --git a/042_mongodb/11_solution/controllers/user.go b/042_mongodb/11_solution/controllers/user.go index 088acf92..82f6dd6b 100644 --- a/042_mongodb/11_solution/controllers/user.go +++ b/042_mongodb/11_solution/controllers/user.go @@ -1,12 +1,12 @@ package controllers import ( + "github.com/GoesToEleven/golang-web-dev/042_mongodb/11_solution/models" + "github.com/GoesToEleven/golang-web-dev/042_mongodb/11_solution/session" "github.com/satori/go.uuid" "golang.org/x/crypto/bcrypt" "net/http" "time" - "github.com/GoesToEleven/golang-web-dev/042_mongodb/11_solution/session" - "github.com/GoesToEleven/golang-web-dev/042_mongodb/11_solution/models" ) func (c Controller) SignUp(w http.ResponseWriter, req *http.Request) { @@ -29,7 +29,7 @@ func (c Controller) SignUp(w http.ResponseWriter, req *http.Request) { return } // create session - sID := uuid.NewV4() + sID, _ := uuid.NewV4() ck := &http.Cookie{ Name: "session", Value: sID.String(), @@ -76,7 +76,7 @@ func (c Controller) Login(w http.ResponseWriter, req *http.Request) { return } // create session - sID := uuid.NewV4() + sID, _ := uuid.NewV4() ck := &http.Cookie{ Name: "session", Value: sID.String(), diff --git a/042_mongodb/11_solution/main.go b/042_mongodb/11_solution/main.go index 2a2d96f0..798b0bbd 100644 --- a/042_mongodb/11_solution/main.go +++ b/042_mongodb/11_solution/main.go @@ -1,9 +1,9 @@ package main import ( + "github.com/GoesToEleven/golang-web-dev/042_mongodb/11_solution/controllers" "html/template" "net/http" - "github.com/GoesToEleven/golang-web-dev/042_mongodb/11_solution/controllers" ) var tpl *template.Template diff --git a/042_mongodb/11_solution/session/session.go b/042_mongodb/11_solution/session/session.go index dee478d2..f7cae192 100644 --- a/042_mongodb/11_solution/session/session.go +++ b/042_mongodb/11_solution/session/session.go @@ -2,10 +2,10 @@ package session import ( "fmt" + "github.com/GoesToEleven/golang-web-dev/042_mongodb/11_solution/models" "github.com/satori/go.uuid" "net/http" "time" - "github.com/GoesToEleven/golang-web-dev/042_mongodb/11_solution/models" ) const Length int = 30 @@ -18,7 +18,7 @@ func GetUser(w http.ResponseWriter, req *http.Request) models.User { // get cookie ck, err := req.Cookie("session") if err != nil { - sID := uuid.NewV4() + sID, _ := uuid.NewV4() ck = &http.Cookie{ Name: "session", Value: sID.String(), diff --git a/043_docker/05_curl/cowboy/Dockerfile b/043_docker/05_curl/cowboy/Dockerfile index 68b7950f..068f6088 100644 --- a/043_docker/05_curl/cowboy/Dockerfile +++ b/043_docker/05_curl/cowboy/Dockerfile @@ -1,3 +1,3 @@ -# This docker file builds and image that runs curl +# This docker file builds an image that runs curl FROM ubuntu:latest -RUN apt-get -y update && apt-get install -y curl \ No newline at end of file +RUN apt-get -y update && apt-get install -y curl diff --git a/043_docker/06_hello-go/main.go b/043_docker/06_hello-go/main.go index de92fcf2..b8e2189d 100644 --- a/043_docker/06_hello-go/main.go +++ b/043_docker/06_hello-go/main.go @@ -1,8 +1,8 @@ package main import ( - "net/http" "io" + "net/http" ) func main() { @@ -10,6 +10,6 @@ func main() { http.ListenAndServe(":80", nil) } -func index(w http.ResponseWriter, r *http.Request){ +func index(w http.ResponseWriter, r *http.Request) { io.WriteString(w, "hello from a docker container") } diff --git a/044_postgres/15_users/README.md b/044_postgres/15_users/README.md index a4e723fb..a944fbee 100644 --- a/044_postgres/15_users/README.md +++ b/044_postgres/15_users/README.md @@ -23,7 +23,7 @@ GRANT ALL PRIVILEGES ON DATABASE company to james; ## revoke privileges ``` -REVOKE ALL PRIVILEGES ON DATABASE company to james; +REVOKE ALL PRIVILEGES ON DATABASE company from james; ``` ## alter @@ -38,4 +38,4 @@ ALTER USER james WITH NOSUPERUSER; ## remove ``` DROP USER james; -``` \ No newline at end of file +``` diff --git a/045-code-organization/02_two-packages/main.go b/045-code-organization/02_two-packages/main.go index 66fc318c..7c3f2bbe 100644 --- a/045-code-organization/02_two-packages/main.go +++ b/045-code-organization/02_two-packages/main.go @@ -2,9 +2,9 @@ package main import ( "database/sql" + "github.com/GoesToEleven/golang-web-dev/045-code-organization/02_two-packages/models" "html/template" "net/http" - "github.com/GoesToEleven/golang-web-dev/045-code-organization/02_two-packages/models" ) var tpl *template.Template diff --git a/045-code-organization/03_multiple-packages/books/handlers.go b/045-code-organization/03_multiple-packages/books/handlers.go index 0a28e95d..79bd49c9 100644 --- a/045-code-organization/03_multiple-packages/books/handlers.go +++ b/045-code-organization/03_multiple-packages/books/handlers.go @@ -2,8 +2,8 @@ package books import ( "database/sql" - "net/http" "github.com/GoesToEleven/golang-web-dev/045-code-organization/03_multiple-packages/config" + "net/http" ) func Index(w http.ResponseWriter, r *http.Request) { diff --git a/045-code-organization/03_multiple-packages/books/models.go b/045-code-organization/03_multiple-packages/books/models.go index 946153e3..c8e09e6c 100644 --- a/045-code-organization/03_multiple-packages/books/models.go +++ b/045-code-organization/03_multiple-packages/books/models.go @@ -2,9 +2,9 @@ package books import ( "errors" + "github.com/GoesToEleven/golang-web-dev/045-code-organization/03_multiple-packages/config" "net/http" "strconv" - "github.com/GoesToEleven/golang-web-dev/045-code-organization/03_multiple-packages/config" ) type Book struct { diff --git a/045-code-organization/03_multiple-packages/main.go b/045-code-organization/03_multiple-packages/main.go index f2320c84..bee5d073 100644 --- a/045-code-organization/03_multiple-packages/main.go +++ b/045-code-organization/03_multiple-packages/main.go @@ -1,12 +1,10 @@ package main import ( - "net/http" "github.com/GoesToEleven/golang-web-dev/045-code-organization/03_multiple-packages/books" + "net/http" ) - - func main() { http.HandleFunc("/", index) http.HandleFunc("/books", books.Index) @@ -21,4 +19,4 @@ func main() { func index(w http.ResponseWriter, r *http.Request) { http.Redirect(w, r, "/books", http.StatusSeeOther) -} \ No newline at end of file +} diff --git a/046_mongodb/15_postgres/books/handlers.go b/046_mongodb/15_postgres/books/handlers.go index 157d82ca..cc4df487 100644 --- a/046_mongodb/15_postgres/books/handlers.go +++ b/046_mongodb/15_postgres/books/handlers.go @@ -2,10 +2,10 @@ package books import ( "database/sql" - "net/http" "encoding/json" - "github.com/GoesToEleven/golang-web-dev/046_mongodb/15_postgres/config" "fmt" + "github.com/GoesToEleven/golang-web-dev/046_mongodb/15_postgres/config" + "net/http" ) func Index(w http.ResponseWriter, r *http.Request) { diff --git a/046_mongodb/15_postgres/books/models.go b/046_mongodb/15_postgres/books/models.go index cbc3ced9..fe7d29d7 100644 --- a/046_mongodb/15_postgres/books/models.go +++ b/046_mongodb/15_postgres/books/models.go @@ -2,9 +2,9 @@ package books import ( "errors" + "github.com/GoesToEleven/golang-web-dev/046_mongodb/15_postgres/config" "net/http" "strconv" - "github.com/GoesToEleven/golang-web-dev/046_mongodb/15_postgres/config" ) type Book struct { diff --git a/046_mongodb/15_postgres/main.go b/046_mongodb/15_postgres/main.go index 8b3eeb31..4bcf3ceb 100644 --- a/046_mongodb/15_postgres/main.go +++ b/046_mongodb/15_postgres/main.go @@ -1,12 +1,10 @@ package main import ( - "net/http" "github.com/GoesToEleven/golang-web-dev/046_mongodb/15_postgres/books" + "net/http" ) - - func main() { http.HandleFunc("/", index) http.HandleFunc("/books", books.Index) @@ -22,4 +20,4 @@ func main() { func index(w http.ResponseWriter, r *http.Request) { http.Redirect(w, r, "/books", http.StatusSeeOther) -} \ No newline at end of file +} diff --git a/047_google-cloud/01_hello-world/02/main.go b/047_google-cloud/01_hello-world/02/main.go index a0014dc8..4d8d35c7 100755 --- a/047_google-cloud/01_hello-world/02/main.go +++ b/047_google-cloud/01_hello-world/02/main.go @@ -2,8 +2,8 @@ package main import ( "fmt" - "net/http" "google.golang.org/appengine" + "net/http" ) func main() { diff --git a/048_memcache/04_increment/main.go b/048_memcache/04_increment/main.go index ec0e73be..29dd33bc 100755 --- a/048_memcache/04_increment/main.go +++ b/048_memcache/04_increment/main.go @@ -36,4 +36,4 @@ func index(w http.ResponseWriter, r *http.Request) { } fmt.Fprintln(w, "Global %v - User %v", globalCount, userCount) -} \ No newline at end of file +} diff --git a/xx045_photo-blog/01_hands-on/starting-files/main.go b/xx045_photo-blog/01_hands-on/starting-files/main.go index 52ae244e..cd594e3e 100644 --- a/xx045_photo-blog/01_hands-on/starting-files/main.go +++ b/xx045_photo-blog/01_hands-on/starting-files/main.go @@ -64,7 +64,7 @@ func index(w http.ResponseWriter, r *http.Request) { func getCookie(w http.ResponseWriter, r *http.Request) *http.Cookie { c, err := r.Cookie("session") if err != nil { - sID := uuid.NewV4() + sID, _ := uuid.NewV4() c = &http.Cookie{ Name: "session", Value: sID.String(), diff --git a/xx045_photo-blog/02_solution/01/main.go b/xx045_photo-blog/02_solution/01/main.go index 8bb08ab3..b3b2415b 100644 --- a/xx045_photo-blog/02_solution/01/main.go +++ b/xx045_photo-blog/02_solution/01/main.go @@ -3,6 +3,7 @@ package main import ( "crypto/sha1" "fmt" + "github.com/julienschmidt/httprouter" "github.com/satori/go.uuid" "html/template" "io" @@ -10,7 +11,6 @@ import ( "os" "path/filepath" "strings" - "github.com/julienschmidt/httprouter" ) var tpl *template.Template @@ -71,7 +71,7 @@ func indexSubmission(w http.ResponseWriter, r *http.Request, _ httprouter.Params func getCookie(w http.ResponseWriter, r *http.Request) *http.Cookie { c, err := r.Cookie("session") if err != nil { - sID := uuid.NewV4() + sID, _ := uuid.NewV4() c = &http.Cookie{ Name: "session", Value: sID.String(), diff --git a/xx045_photo-blog/02_solution/02/controllers/general.go b/xx045_photo-blog/02_solution/02/controllers/general.go index 4378bc90..5fda0c27 100644 --- a/xx045_photo-blog/02_solution/02/controllers/general.go +++ b/xx045_photo-blog/02_solution/02/controllers/general.go @@ -1,19 +1,19 @@ package controllers import ( - "strings" - "io" - "fmt" "crypto/sha1" + "fmt" "github.com/julienschmidt/httprouter" + "html/template" + "io" + "mime/multipart" "net/http" "os" "path/filepath" - "html/template" - "mime/multipart" + "strings" ) -type Controller struct{ +type Controller struct { tpl *template.Template } @@ -21,13 +21,13 @@ func NewController(tpl *template.Template) *Controller { return &Controller{tpl} } -func(ctl Controller) Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { +func (ctl Controller) Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { c := getCookie(w, r) xs := strings.Split(c.Value, "|") ctl.tpl.ExecuteTemplate(w, "index.gohtml", xs[1:]) //only send over images } -func(ctl Controller) IndexSubmission(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { +func (ctl Controller) IndexSubmission(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { c := getCookie(w, r) mf, fh, err := r.FormFile("nf") @@ -63,7 +63,7 @@ func createSHA(mf multipart.File, fh *multipart.FileHeader) string { return fmt.Sprintf("%x", h.Sum(nil)) + "." + ext } -func createNewFile(name string) (*os.File, error) { +func createNewFile(name string) (*os.File, error) { wd, err := os.Getwd() if err != nil { fmt.Println(err) @@ -75,4 +75,4 @@ func createNewFile(name string) (*os.File, error) { fmt.Println(err) } return nf, nil -} \ No newline at end of file +} diff --git a/xx045_photo-blog/02_solution/02/controllers/session.go b/xx045_photo-blog/02_solution/02/controllers/session.go index bec5d5cd..7dc08d4d 100644 --- a/xx045_photo-blog/02_solution/02/controllers/session.go +++ b/xx045_photo-blog/02_solution/02/controllers/session.go @@ -1,15 +1,15 @@ package controllers import ( - "net/http" "github.com/satori/go.uuid" + "net/http" "strings" ) func getCookie(w http.ResponseWriter, r *http.Request) *http.Cookie { c, err := r.Cookie("session") if err != nil { - sID := uuid.NewV4() + sID, _ := uuid.NewV4() c = &http.Cookie{ Name: "session", Value: sID.String(), diff --git a/xx045_photo-blog/02_solution/02/main.go b/xx045_photo-blog/02_solution/02/main.go index 0313b7ad..1bfec7fd 100644 --- a/xx045_photo-blog/02_solution/02/main.go +++ b/xx045_photo-blog/02_solution/02/main.go @@ -1,10 +1,10 @@ package main import ( + "github.com/GoesToEleven/golang-web-dev/045_photo-blog/02_solution/02/controllers" + "github.com/julienschmidt/httprouter" "html/template" "net/http" - "github.com/julienschmidt/httprouter" - "github.com/GoesToEleven/golang-web-dev/045_photo-blog/02_solution/02/controllers" ) var tpl *template.Template @@ -22,4 +22,4 @@ func main() { r.Handler("GET", "/favicon.ico", http.NotFoundHandler()) r.ServeFiles("/public/*filepath", http.Dir("./public")) http.ListenAndServe(":8080", r) -} \ No newline at end of file +} diff --git a/xx045_photo-blog/02_solution/03/packages/controllers/general.go b/xx045_photo-blog/02_solution/03/packages/controllers/general.go index c535b0f8..44e5e96b 100644 --- a/xx045_photo-blog/02_solution/03/packages/controllers/general.go +++ b/xx045_photo-blog/02_solution/03/packages/controllers/general.go @@ -1,24 +1,24 @@ package controllers import ( - "strings" - "io" - "fmt" "crypto/sha1" + "encoding/json" + "fmt" + "github.com/GoesToEleven/golang-web-dev/045_photo-blog/02_solution/03/packages/errors" + "github.com/GoesToEleven/golang-web-dev/045_photo-blog/02_solution/03/packages/memcache" "github.com/julienschmidt/httprouter" + "google.golang.org/appengine" + "google.golang.org/appengine/log" + "html/template" + "io" + "mime/multipart" "net/http" "os" "path/filepath" - "html/template" - "mime/multipart" - "github.com/GoesToEleven/golang-web-dev/045_photo-blog/02_solution/03/packages/memcache" - "github.com/GoesToEleven/golang-web-dev/045_photo-blog/02_solution/03/packages/errors" - "google.golang.org/appengine/log" - "google.golang.org/appengine" - "encoding/json" + "strings" ) -type Controller struct{ +type Controller struct { tpl *template.Template } @@ -26,7 +26,7 @@ func NewController(tpl *template.Template) *Controller { return &Controller{tpl} } -func(ctl Controller) Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { +func (ctl Controller) Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { ctx := appengine.NewContext(r) log.Infof(ctx, "IN INDEX") // fyi c := GetCookie(w, r) @@ -41,7 +41,7 @@ func(ctl Controller) Index(w http.ResponseWriter, r *http.Request, _ httprouter. ctl.tpl.ExecuteTemplate(w, "index.gohtml", xs) } -func(ctl Controller) IndexSubmission(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { +func (ctl Controller) IndexSubmission(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { c := GetCookie(w, r) @@ -75,7 +75,7 @@ func createSHA(mf multipart.File, fh *multipart.FileHeader) string { return fmt.Sprintf("%x", h.Sum(nil)) + "." + ext } -func createNewFile(r *http.Request, name string) (*os.File, error) { +func createNewFile(r *http.Request, name string) (*os.File, error) { ctx := appengine.NewContext(r) wd, err := os.Getwd() if err != nil { @@ -88,4 +88,4 @@ func createNewFile(r *http.Request, name string) (*os.File, error) { return nil, err } return nf, nil -} \ No newline at end of file +} diff --git a/xx045_photo-blog/02_solution/03/packages/controllers/session.go b/xx045_photo-blog/02_solution/03/packages/controllers/session.go index 6ec2d100..d76d56ea 100644 --- a/xx045_photo-blog/02_solution/03/packages/controllers/session.go +++ b/xx045_photo-blog/02_solution/03/packages/controllers/session.go @@ -1,14 +1,14 @@ package controllers import ( - "net/http" "github.com/satori/go.uuid" + "net/http" ) func GetCookie(w http.ResponseWriter, r *http.Request) *http.Cookie { c, err := r.Cookie("session") if err != nil { - sID := uuid.NewV4() + sID, _ := uuid.NewV4() c = &http.Cookie{ Name: "session", Value: sID.String(), @@ -16,4 +16,4 @@ func GetCookie(w http.ResponseWriter, r *http.Request) *http.Cookie { http.SetCookie(w, c) } return c -} \ No newline at end of file +} diff --git a/xx045_photo-blog/02_solution/03/packages/memcache/picture.go b/xx045_photo-blog/02_solution/03/packages/memcache/picture.go index bab5193a..9f9f9316 100644 --- a/xx045_photo-blog/02_solution/03/packages/memcache/picture.go +++ b/xx045_photo-blog/02_solution/03/packages/memcache/picture.go @@ -1,11 +1,11 @@ package memcache import ( - "net/http" - "google.golang.org/appengine" - "google.golang.org/appengine/memcache" "encoding/json" "github.com/GoesToEleven/golang-web-dev/045_photo-blog/02_solution/03/packages/errors" + "google.golang.org/appengine" + "google.golang.org/appengine/memcache" + "net/http" ) func SetPictures(r *http.Request, c *http.Cookie, xs []string) error { @@ -20,7 +20,7 @@ func SetPictures(r *http.Request, c *http.Cookie, xs []string) error { return err } -func GetPictures(r *http.Request, c *http.Cookie) ([]string, error) { +func GetPictures(r *http.Request, c *http.Cookie) ([]string, error) { ctx := appengine.NewContext(r) item, err := memcache.Get(ctx, c.Value) @@ -30,4 +30,4 @@ func GetPictures(r *http.Request, c *http.Cookie) ([]string, error) { var xs []string json.Unmarshal(item.Value, &xs) return xs, nil -} \ No newline at end of file +} diff --git a/xx045_photo-blog/02_solution/03/project/main.go b/xx045_photo-blog/02_solution/03/project/main.go index ec42774c..dd1a2827 100644 --- a/xx045_photo-blog/02_solution/03/project/main.go +++ b/xx045_photo-blog/02_solution/03/project/main.go @@ -1,11 +1,11 @@ package main import ( - "html/template" - "net/http" + "github.com/GoesToEleven/golang-web-dev/045_photo-blog/02_solution/03/packages/controllers" "github.com/julienschmidt/httprouter" "google.golang.org/appengine" - "github.com/GoesToEleven/golang-web-dev/045_photo-blog/02_solution/03/packages/controllers" + "html/template" + "net/http" ) var tpl *template.Template @@ -28,4 +28,4 @@ func main() { r.ServeFiles("/public/*filepath", http.Dir("./public")) // call appengine's main function appengine.Main() -} \ No newline at end of file +}