Last active
August 18, 2016 06:19
-
-
Save tmitz/e1f99dcfa88cb4c98795bd2749069896 to your computer and use it in GitHub Desktop.
Effective GoのWebServerをhtml/templateで http://golang.jp/effective_go#web_server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
flag使う必要性なかった...