Skip to content

Instantly share code, notes, and snippets.

@up1
Last active August 29, 2015 14:01
Show Gist options
  • Save up1/a2fd996235dcf32652be to your computer and use it in GitHub Desktop.
Save up1/a2fd996235dcf32652be to your computer and use it in GitHub Desktop.
Demo :: Go with html template
package main
import (
"html/template"
"os"
)
func main() {
t, err := template.New("foo").Parse(`{{define "T"}}Hello, {{.}}!{{end}}`)
err = t.ExecuteTemplate(os.Stdout, "T", "Up1")
if err != nil {
}
}
#!/usr/bin/env bash
CURDIR=`pwd`
OLDGOPATH="$GOPATH"
export GOPATH="$CURDIR"
gofmt -w src/server
go install server
export GOPATH="$OLDGOPATH"
echo 'finished'
<h1>{{.project_name}}</h1>
<div>
<h1>List of data</h1>
<ul>
{{range .results}}
<li>{{.TopicID}} :: {{.Name}}</li>
{{end}}
</ul>
</div>
import (
"html/template"
"net/http"
)
type Topic struct {
TopicID int
Name string
}
func homeHandler(w http.ResponseWriter, r *http.Request) {
t, _ := template.ParseFiles("templates/demo.html")
var results []Topic
results = []Topic{
Topic{1, "Title1"},
Topic{2, "Title2"},
}
t.Execute(w, map[string]interface{}{
"project_name": "MY DATA",
"results": results,
})
}
package main
import (
"example"
)
func main() {
example.StartServer()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment