Skip to content

Instantly share code, notes, and snippets.

@tmitz
Last active August 18, 2016 06:19
Show Gist options
  • Save tmitz/e1f99dcfa88cb4c98795bd2749069896 to your computer and use it in GitHub Desktop.
Save tmitz/e1f99dcfa88cb4c98795bd2749069896 to your computer and use it in GitHub Desktop.
Effective GoのWebServerをhtml/templateで http://golang.jp/effective_go#web_server
package main
import (
"flag"
"html/template"
"log"
"net/http"
)
const (
tpl = `
<!DOCTYPE html>
<html>
<head>
<title>QR Link Generator</title>
</head>
<body>
<img src="http://chart.apis.google.com/chart?chs=300x300&cht=qr&choe=UTF-8&chl={{.}}" />
<br>
{{.}}
<br>
<br>
<form action="/" name="f" method="GET">
<input maxLength="1024" size="70" name="s" value="" title="Text to QR Encode">
<input type="submit" value="Show QR" name="qr">
</form>
</body>
</html>`
)
var (
addr = flag.String("addr", ":1718", "http service address")
t = template.Must(template.New("master").Parse(tpl))
)
func QR(w http.ResponseWriter, req *http.Request) {
t.Execute(w, req.FormValue("s"))
}
func check(err error) {
if err != nil {
log.Fatal(err)
}
}
func main() {
flag.Parse()
http.HandleFunc("/", QR)
err := http.ListenAndServe(*addr, nil)
check(err)
}
@tmitz
Copy link
Author

tmitz commented Aug 18, 2016

flag使う必要性なかった...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment