Skip to content

Instantly share code, notes, and snippets.

@IndianGuru
Last active September 5, 2015 02:53
Show Gist options
  • Select an option

  • Save IndianGuru/7e222492d217bdc96ee1 to your computer and use it in GitHub Desktop.

Select an option

Save IndianGuru/7e222492d217bdc96ee1 to your computer and use it in GitHub Desktop.
quote.go for quotes
package quote
import (
"fmt"
"html/template"
"net/http"
)
func init() {
http.HandleFunc("/", root)
http.HandleFunc("/quote", quote)
}
func root(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, rootForm)
}
const rootForm = `
<html>
<body>
<h1>Enter Author's Quote</h1>
<form action="/quote" method="post" accept-charset="utf-8">
<input type="text" name="aut" value="Author's Name: " id="aut">
<input type="text" name="quo" value="Type a string..." id="quo">
<input type="submit" value="Submit Quote">
</form>
</body>
</html>
`
var quoteTemplate = template.Must(template.New("quote").Parse(quoteTemplateHTML))
func quote(w http.ResponseWriter, r *http.Request) {
youWrote := "Author: " + r.FormValue("aut") + "Quote: " + r.FormValue("quo")
err := quoteTemplate.Execute(w, youWrote)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
const quoteTemplateHTML = `
<html>
<body>
<p>You wrote:</p>
<pre>{{html .}}</pre>
</body>
</html>
`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment