diff --git a/007_data-structures/03_struct/01/tpl.gohtml b/007_data-structures/03_struct/01/tpl.gohtml index fb59b454..60fb186d 100644 --- a/007_data-structures/03_struct/01/tpl.gohtml +++ b/007_data-structures/03_struct/01/tpl.gohtml @@ -5,8 +5,10 @@ My Peeps +{{$sage := .}} +

Sage: {{$sage}}

\ No newline at end of file diff --git a/012_hands-on/01_hands-on/main.go b/012_hands-on/01_hands-on/main.go new file mode 100644 index 00000000..9b69e11e --- /dev/null +++ b/012_hands-on/01_hands-on/main.go @@ -0,0 +1,79 @@ +package main + +import ( + "log" + "os" + "text/template" +) + +type course struct { + Number string + Name string + Units string +} + +type semester struct { + Term string + Courses []course +} + +type year struct { + AcaYear string + Fall semester + Spring semester + Summer semester +} + +var tpl *template.Template + +func init() { + tpl = template.Must(template.ParseFiles("tpl.gohtml")) +} + +func main() { + years := []year{ + year{ + AcaYear: "2020-2021", + Fall: semester{ + Term: "Fall", + Courses: []course{ + course{"CSCI-40", "Introduction to Programming in Go", "4"}, + course{"CSCI-130", "Introduction to Web Programming with Go", "4"}, + course{"CSCI-140", "Mobile Apps Using Go", "4"}, + }, + }, + Spring: semester{ + Term: "Spring", + Courses: []course{ + course{"CSCI-50", "Advanced Go", "5"}, + course{"CSCI-190", "Advanced Web Programming with Go", "5"}, + course{"CSCI-191", "Advanced Mobile Apps With Go", "5"}, + }, + }, + }, + year{ + AcaYear: "2021-2022", + Fall: semester{ + Term: "Fall", + Courses: []course{ + course{"CSCI-40", "Introduction to Programming in Go", "4"}, + course{"CSCI-130", "Introduction to Web Programming with Go", "4"}, + course{"CSCI-140", "Mobile Apps Using Go", "4"}, + }, + }, + Spring: semester{ + Term: "Spring", + Courses: []course{ + course{"CSCI-50", "Advanced Go", "5"}, + course{"CSCI-190", "Advanced Web Programming with Go", "5"}, + course{"CSCI-191", "Advanced Mobile Apps With Go", "5"}, + }, + }, + }, + } + + err := tpl.Execute(os.Stdout, years) + if err != nil { + log.Fatalln(err) + } +} diff --git a/012_hands-on/01_hands-on/tpl.gohtml b/012_hands-on/01_hands-on/tpl.gohtml new file mode 100644 index 00000000..51817a32 --- /dev/null +++ b/012_hands-on/01_hands-on/tpl.gohtml @@ -0,0 +1,26 @@ + + + + + Document + + + +{{range .}} + +
{{.AcaYear}}
+
{{template "semester" .Fall}}
+
{{template "semester" .Spring}}
+ +{{end}} + + + +{{define "semester"}} +
{{.Term}} courses
+
    + {{range .Courses}} +
  1. {{.Number}} - {{.Name}} - {{.Units}}
  2. + {{end}} +
+{{end}} \ No newline at end of file diff --git a/012_hands-on/03_hands-on/main.go b/012_hands-on/03_hands-on/main.go new file mode 100644 index 00000000..803909b5 --- /dev/null +++ b/012_hands-on/03_hands-on/main.go @@ -0,0 +1,54 @@ +package main + +import ( + "log" + "os" + "text/template" +) + +var tpl *template.Template + +type hotel struct { + Name string + Address string + City string + Zip string +} + +type region struct { + Name string + Hotels []hotel +} + +func init() { + tpl = template.Must(template.ParseFiles("tpl.gohtml")) +} + +func main() { + c := []region{ + { + Name: "Southern", + Hotels: []hotel{ + {"Hotel 1", "10th, Southern ave.", "S. City 1", "123456"}, + {"Hotel 2", "10th, Southern ave.", "S. City 2", "123457"}, + }, + }, { + Name: "Central", + Hotels: []hotel{ + {"Hotel 1", "10th, Central ave.", "C. City 1", "234567"}, + {"Hotel 2", "10th, Central ave.", "C. City 2", "234568"}, + }, + }, { + Name: "Northern", + Hotels: []hotel{ + {"Hotel 1", "10th, Northern ave.", "N. City 1", "345678"}, + {"Hotel 2", "10th, Northern ave.", "N. City 2", "345679"}, + }, + }, + } + + err := tpl.Execute(os.Stdout, c) + if err != nil { + log.Fatal(err) + } +} diff --git a/012_hands-on/03_hands-on/tpl.gohtml b/012_hands-on/03_hands-on/tpl.gohtml new file mode 100644 index 00000000..3bf143a5 --- /dev/null +++ b/012_hands-on/03_hands-on/tpl.gohtml @@ -0,0 +1,27 @@ + + + + + Document + + + +{{$regions := .}} + +{{range $regions}} + +
{{.Region}}
+{{template "hotel" .}} + +{{end}} + + + +{{define "hotel"}} +
Hotels
+
    + {{range .Hotels}} +
  1. {{.Name}} - {{.Address}} - {{.City}} - {{.Zip}}
  2. + {{end}} +
+{{end}} \ No newline at end of file diff --git a/012_hands-on/09_hands-on/main.go b/012_hands-on/09_hands-on/main.go new file mode 100644 index 00000000..dae57221 --- /dev/null +++ b/012_hands-on/09_hands-on/main.go @@ -0,0 +1,78 @@ +package main + +import ( + "encoding/csv" + "log" + "os" + "strconv" + "text/template" + "time" +) + +type TableRow struct { + Date time.Time + Open float64 + High float64 + Low float64 + Close float64 + Volume int + AdjClose float64 +} + +var tpl *template.Template + +func init() { + tpl = template.Must(template.ParseFiles("tpl.gohtml")) +} + +func main() { + data := loadData() + + err := tpl.Execute(os.Stdout, data) + if err != nil { + log.Fatal(err) + } +} + +func loadData() []TableRow { + r, err := readCsv("table.csv") + if err != nil { + log.Fatal("Error reading csv", err) + } + + tr := []TableRow{} + for _, row := range r[1:] { + r0, _ := time.Parse("2006-01-02", row[0]) + r1, _ := strconv.ParseFloat(row[1], 64) + r2, _ := strconv.ParseFloat(row[2], 64) + r3, _ := strconv.ParseFloat(row[3], 64) + r4, _ := strconv.ParseFloat(row[4], 64) + r5, _ := strconv.ParseInt(row[5], 10, 64) + r6, _ := strconv.ParseFloat(row[6], 32) + tr = append(tr, TableRow{ + Date: r0, + Open: r1, + High: r2, + Low: r3, + Close: r4, + Volume: int(r5), + AdjClose: r6, + }) + } + return tr +} + +func readCsv(f string) ([][]string, error) { + fh, err := os.Open(f) + if err != nil { + return [][]string{}, err + } + defer fh.Close() + + rows, err := csv.NewReader(fh).ReadAll() + if err != nil { + return [][]string{}, err + } + + return rows, nil +} diff --git a/012_hands-on/09_hands-on/tpl.gohtml b/012_hands-on/09_hands-on/tpl.gohtml new file mode 100644 index 00000000..58dae003 --- /dev/null +++ b/012_hands-on/09_hands-on/tpl.gohtml @@ -0,0 +1,21 @@ + + + + + Document + + +{{$rows := .}} +{{ range $i, $r := $rows}} +
+ # {{$i}} + Date: {{$r.Date}} + Open {{$r.Open}} + High {{$r.High}} + Low {{$r.Low}} + Volume {{$r.Volume}} + AdjClose {{$r.AdjClose}} +
+{{end}} + + diff --git a/016_building-a-tcp-server-for-http/02_hands-on/main.go b/016_building-a-tcp-server-for-http/02_hands-on/main.go new file mode 100644 index 00000000..c3a37abb --- /dev/null +++ b/016_building-a-tcp-server-for-http/02_hands-on/main.go @@ -0,0 +1,69 @@ +package main + +import ( + "bufio" + "fmt" + "log" + "net" + "strings" +) + +func main() { + li, err := net.Listen("tcp", ":8080") + if err != nil { + log.Fatalln(err.Error()) + } + defer li.Close() + + for { + conn, err := li.Accept() + if err != nil { + log.Fatalln(err.Error()) + continue + } + go handle(conn) + } +} + +func handle(conn net.Conn) { + defer conn.Close() + + // read request + u := request(conn) + + // write response + respond(conn, u) +} + +func request(conn net.Conn) string { + i := 0 + var url string + scanner := bufio.NewScanner(conn) + for scanner.Scan() { + ln := scanner.Text() + fmt.Println(ln) + if i == 0 { + // request line + r := strings.Fields(ln) + fmt.Println("***METHOD", r[0]) + url = r[1] + } + if ln == "" { + // headers are done + break + } + i++ + } + return url +} + +func respond(conn net.Conn, u string) { + + body := fmt.Sprintf(`Hello World %s`, u) + + fmt.Fprint(conn, "HTTP/1.1 200 OK\r\n") + fmt.Fprintf(conn, "Content-Length: %d\r\n", len(body)) + fmt.Fprint(conn, "Content-Type: text/html\r\n") + fmt.Fprint(conn, "\r\n") + fmt.Fprint(conn, body) +} diff --git a/016_building-a-tcp-server-for-http/04_hands-on/main.go b/016_building-a-tcp-server-for-http/04_hands-on/main.go new file mode 100644 index 00000000..462c1683 --- /dev/null +++ b/016_building-a-tcp-server-for-http/04_hands-on/main.go @@ -0,0 +1,78 @@ +package main + +import ( + "bufio" + "fmt" + "log" + "net" + "strings" +) + +type request struct { + method, url, body string + headers map[string]string +} + +func main() { + li, err := net.Listen("tcp", ":8080") + if err != nil { + log.Fatal(err) + } + defer li.Close() + + for { + conn, err := li.Accept() + if err != nil { + log.Fatalln(err.Error()) + continue + } + + go handle(conn) + } +} + +func handle(conn net.Conn) { + defer conn.Close() + + r := read(conn) + + fmt.Println(r) +} + +func read(conn net.Conn) request { + var m, u, b string + h := make(map[string]string) + i := 0 + doneHeaders := false + + scanner := bufio.NewScanner(conn) + for scanner.Scan() { + line := scanner.Text() + log.Println(line) + if i == 0 { + l := strings.Fields(line) + m = l[0] + u = l[1] + } + if !doneHeaders { + if line == "" { + doneHeaders = true + break + } else { + l := strings.Fields(line) + h[l[0]] = l[1] + } + } else { + b += line + } + + i++ + } + + return request{ + method: m, + url: u, + body: b, + headers: h, + } +} diff --git a/022_hands-on/01/01_hands-on/main.go b/022_hands-on/01/01_hands-on/main.go new file mode 100644 index 00000000..2f7590fb --- /dev/null +++ b/022_hands-on/01/01_hands-on/main.go @@ -0,0 +1,32 @@ +package main + +import ( + "fmt" + "io" + "net/http" +) + +func home(res http.ResponseWriter, req *http.Request) { + io.WriteString(res, "Hello from home handler!") +} + +func dog(res http.ResponseWriter, req *http.Request) { + io.WriteString(res, "Dog!") +} + +func me(res http.ResponseWriter, req *http.Request) { + req.ParseForm() + name := req.Form.Get("name") + if name == "" { + name = "you" + } + io.WriteString(res, fmt.Sprintf("Hello %s!", name)) +} + +func main() { + + http.Handle("/", http.HandlerFunc(home)) + http.Handle("/dog/", http.HandlerFunc(dog)) + http.Handle("/me/", http.HandlerFunc(me)) + http.ListenAndServe(":8080", nil) +} diff --git a/022_hands-on/01/03_hands-on/main.go b/022_hands-on/01/03_hands-on/main.go new file mode 100644 index 00000000..a68bdc6f --- /dev/null +++ b/022_hands-on/01/03_hands-on/main.go @@ -0,0 +1,42 @@ +package main + +import ( + "fmt" + "html/template" + "net/http" + "net/url" +) + +var tpl *template.Template + +func init() { + tpl = template.Must(template.ParseGlob("templates/*")) +} + +func index(res http.ResponseWriter, req *http.Request) { + tpl.ExecuteTemplate(res, "index.gohtml", nil) +} + +func dog(res http.ResponseWriter, req *http.Request) { + tpl.ExecuteTemplate(res, "dog.gohtml", nil) +} + +func me(res http.ResponseWriter, req *http.Request) { + var postForm url.Values + fmt.Println(req.Method) + if req.Method == "POST" { + req.ParseForm() + postForm = req.PostForm + fmt.Print(postForm) + } + + tpl.ExecuteTemplate(res, "me.gohtml", postForm) +} + +func main() { + + http.Handle("/", http.HandlerFunc(index)) + http.Handle("/dog/", http.HandlerFunc(dog)) + http.Handle("/me/", http.HandlerFunc(me)) + http.ListenAndServe(":8080", nil) +} diff --git a/022_hands-on/01/03_hands-on/templates/dog.gohtml b/022_hands-on/01/03_hands-on/templates/dog.gohtml new file mode 100644 index 00000000..d3df6493 --- /dev/null +++ b/022_hands-on/01/03_hands-on/templates/dog.gohtml @@ -0,0 +1,15 @@ + + + + + DOG + + + +DOG
+index
+dog
+me
+ + + \ No newline at end of file diff --git a/022_hands-on/01/03_hands-on/templates/index.gohtml b/022_hands-on/01/03_hands-on/templates/index.gohtml new file mode 100644 index 00000000..b360997a --- /dev/null +++ b/022_hands-on/01/03_hands-on/templates/index.gohtml @@ -0,0 +1,15 @@ + + + + + INDEX + + + +INDEX
+index
+dog
+me
+ + + \ No newline at end of file diff --git a/022_hands-on/01/03_hands-on/templates/me.gohtml b/022_hands-on/01/03_hands-on/templates/me.gohtml new file mode 100644 index 00000000..026f9e40 --- /dev/null +++ b/022_hands-on/01/03_hands-on/templates/me.gohtml @@ -0,0 +1,23 @@ + + + + + ME + + + +ME
+index
+dog
+me
+{{ if .}} +
Hello {{ .name }}
+{{ else }} +
+ Name: + +
+{{ end }} + + + \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 00000000..401b793d --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module udemy.com/go-web-dev + +go 1.16