Skip to content

Instantly share code, notes, and snippets.

@ninnemana
Created May 17, 2012 18:53
Show Gist options
  • Save ninnemana/2720883 to your computer and use it in GitHub Desktop.
Save ninnemana/2720883 to your computer and use it in GitHub Desktop.
package hello
import (
"html/template"
"net/http"
"fmt"
)
func init() {
http.HandleFunc("/", hello)
http.HandleFunc("/add_person", add_person)
}
type Employee struct{
First string
Last string
}
func hello(w http.ResponseWriter, r *http.Request){
person := &Employee{First:"Alex",Last:"Ninneman"}
displayTemplate("new_person", "new_person.html", w, person)
}
func add_person(w http.ResponseWriter, r *http.Request){
first := r.FormValue("first")
fmt.Println("Hello " + first + " " + r.FormValue("last"))
}
func displayTemplate(name string, filename string, w http.ResponseWriter, x interface{}){
t, err := template.New(name).ParseFiles(filename)
err = t.ExecuteTemplate(w, filename, x)
if err != nil {
fmt.Println(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment