Last active
September 21, 2017 18:28
-
-
Save suhanlee/63e60df47114fc33691c8651bb2d9558 to your computer and use it in GitHub Desktop.
template1.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "net/http" | |
| "html/template" | |
| ) | |
| func process(w http.ResponseWriter, r *http.Request) { | |
| t, _ := template.ParseFiles("tmpl.html") | |
| t.Execute(w, "Hello World!") | |
| } | |
| func main() { | |
| server := http.Server{ | |
| Addr: "127.0.0.1:8080", | |
| } | |
| http.HandleFunc("/process", process) | |
| server.ListenAndServe() | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <html> | |
| <head></head> | |
| <body> | |
| {{ . }} | |
| </body> | |
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "net/http" | |
| "html/template" | |
| "time" | |
| "math/rand" | |
| ) | |
| func process(w http.ResponseWriter, r *http.Request) { | |
| t, _ := template.ParseFiles("tmpl2.html") | |
| rand.Seed(time.Now().Unix()) | |
| t.Execute(w, rand.Intn(10) > 5 ) | |
| } | |
| func main() { | |
| server := http.Server{ | |
| Addr: "127.0.0.1:8080", | |
| } | |
| http.HandleFunc("/process", process) | |
| server.ListenAndServe() | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <html> | |
| <body> | |
| {{ if . }} | |
| Number is greater than 5! | |
| {{ else }} | |
| Number is 5 or less! | |
| {{ end }} | |
| </body> | |
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "net/http" | |
| "html/template" | |
| ) | |
| func process(w http.ResponseWriter, r *http.Request) { | |
| t, _ := template.ParseFiles("tmpl3.html") | |
| daysOfWeek := []string{"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"} | |
| t.Execute(w, daysOfWeek) | |
| } | |
| func main() { | |
| server := http.Server{ | |
| Addr: "127.0.0.1:8080", | |
| } | |
| http.HandleFunc("/process", process) | |
| server.ListenAndServe() | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <html> | |
| <body> | |
| {{ range . }} | |
| <li> {{ . }}</li> | |
| {{ end }} | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment