The example in this article describes how the go language implements a simple HTTP service. Share to everyone for your reference. The implementation methods are as follows:
Copy Code code as follows:
Package Main
Import (
"Flag"
"Log"
"Net/http"
"Text/template"
)
var addr = flag. String ("addr", ": 1718", "HTTP Service address")//Q=17, r=18
var Templ = template. Must (template. New ("QR"). Parse (TEMPLATESTR))
Func Main () {
Flag. Parse ()
http. Handle ("/", HTTP. Handlerfunc (QR))
ERR: = http. Listenandserve (*addr, nil)
If Err!= nil {
Log. Fatal ("Listenandserve:", err)
}
}
Func QR (w http. Responsewriter, req *http. Request) {
Templ. Execute (W, req. Formvalue ("s"))
}
Const TEMPLATESTR = '
<TITLE>QR Link generator</title>
<body>
{{If.}}}
<br>
{{html.}}
<br>
<br>
{{End}}}
<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>
I hope this article will help you with your go language program.