Skip to content

Commit 9d0b11a

Browse files
committed
end of class
1 parent 7dd6c03 commit 9d0b11a

File tree

8 files changed

+178
-0
lines changed

8 files changed

+178
-0
lines changed

000_temp/46_sp17/19/main.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package main
2+
3+
import (
4+
"net/http"
5+
"io"
6+
)
7+
8+
func main() {
9+
http.HandleFunc("/", index)
10+
http.HandleFunc("/foo", foo)
11+
http.HandleFunc("/bar/", bar)
12+
http.Handle("/favicon.ico", http.NotFoundHandler())
13+
http.ListenAndServe(":8080", nil)
14+
}
15+
16+
func index(w http.ResponseWriter, r *http.Request) {
17+
io.WriteString(w, "hello from index")
18+
}
19+
20+
func foo(w http.ResponseWriter, r *http.Request) {
21+
io.WriteString(w, "hello from foo")
22+
}
23+
24+
func bar(w http.ResponseWriter, r *http.Request) {
25+
io.WriteString(w, "hello from bar")
26+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package main
2+
3+
import (
4+
"net/http"
5+
"html/template"
6+
)
7+
8+
var tpl *template.Template
9+
10+
func init() {
11+
tpl = template.Must(template.ParseGlob("templates/*.gohtml"))
12+
}
13+
14+
func main() {
15+
http.HandleFunc("/", index)
16+
http.HandleFunc("/about", about)
17+
http.Handle("/favicon.ico", http.NotFoundHandler())
18+
http.ListenAndServe(":8080", nil)
19+
}
20+
21+
func index(w http.ResponseWriter, r *http.Request) {
22+
tpl.ExecuteTemplate(w, "index.gohtml", "Heeeeeelllooo world, yeeeee-ha!")
23+
}
24+
25+
func about(w http.ResponseWriter, r *http.Request) {
26+
tpl.ExecuteTemplate(w, "about.gohtml", 42)
27+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>About</title>
6+
</head>
7+
<body>
8+
9+
<h1>You are at about</h1>
10+
11+
<h1>{{.}}</h1>
12+
13+
</body>
14+
</html>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Index</title>
6+
</head>
7+
<body>
8+
9+
<h1>You are at index</h1>
10+
11+
<h1>{{.}}</h1>
12+
13+
</body>
14+
</html>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package main
2+
3+
import (
4+
"net/http"
5+
"html/template"
6+
)
7+
8+
type person struct {
9+
First string
10+
Last string
11+
}
12+
13+
var tpl *template.Template
14+
15+
func init() {
16+
tpl = template.Must(template.ParseGlob("templates/*.gohtml"))
17+
}
18+
19+
func main() {
20+
http.HandleFunc("/", index)
21+
http.HandleFunc("/about", about)
22+
http.HandleFunc("/contact", contact)
23+
http.Handle("/favicon.ico", http.NotFoundHandler())
24+
http.ListenAndServe(":8080", nil)
25+
}
26+
27+
func index(w http.ResponseWriter, r *http.Request) {
28+
p1 := person{
29+
"James",
30+
"Bond",
31+
}
32+
33+
tpl.ExecuteTemplate(w, "index.gohtml", p1)
34+
}
35+
36+
func about(w http.ResponseWriter, r *http.Request) {
37+
xi := []int{3,5,7,9,17,749}
38+
tpl.ExecuteTemplate(w, "about.gohtml", xi)
39+
}
40+
41+
func contact(w http.ResponseWriter, r *http.Request) {
42+
m := map[string]int{
43+
"James":32,
44+
"Moneypenny":24,
45+
}
46+
tpl.ExecuteTemplate(w, "contact.gohtml", m)
47+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>About</title>
6+
</head>
7+
<body>
8+
9+
<h1>You are at about</h1>
10+
11+
<h1>CURRENT DATA: {{.}}</h1>
12+
{{range .}}
13+
<h1>IN LOOP DATA: {{.}}</h1>
14+
{{end}}
15+
16+
</body>
17+
</html>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Contact</title>
6+
</head>
7+
<body>
8+
9+
<h1>You are at contact</h1>
10+
11+
<h1>CURRENT DATA: {{.}}</h1>
12+
{{range $name, $age := .}}
13+
<h1>IN LOOP DATA: {{$name}} {{$age}}</h1>
14+
{{end}}
15+
16+
</body>
17+
</html>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Index</title>
6+
</head>
7+
<body>
8+
9+
<h1>You are at index</h1>
10+
11+
<h1>{{.}}</h1>
12+
<h1>{{.First}}</h1>
13+
<h1>{{.Last}}</h1>
14+
15+
</body>
16+
</html>

0 commit comments

Comments
 (0)