Skip to content

Instantly share code, notes, and snippets.

@suhanlee
Last active September 21, 2017 18:28
Show Gist options
  • Select an option

  • Save suhanlee/63e60df47114fc33691c8651bb2d9558 to your computer and use it in GitHub Desktop.

Select an option

Save suhanlee/63e60df47114fc33691c8651bb2d9558 to your computer and use it in GitHub Desktop.
template1.go
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()
}
<html>
<head></head>
<body>
{{ . }}
</body>
</html>
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()
}
<html>
<body>
{{ if . }}
Number is greater than 5!
{{ else }}
Number is 5 or less!
{{ end }}
</body>
</html>
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()
}
<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