This is a creation in Article, where the information may have evolved or changed.
Package Mainimport ("bytes" "Expvar" "Flag" "FMT" "io" "Log" "Net/http" "OS" "Os/exec" "StrConv" "sync")//Hello World, the Web Servervar hellorequests = Expvar. Newint ("Hello-requests") Func HelloServer (w http. Responsewriter, req *http. Request) {hellorequests.add (1) Io. WriteString (W, "Hello, world!\n")}//simple counter server. POSTing to it would set the Value.Type Counter struct {mu sync. Mutex//Protects NN int}//this makes Counter satisfy the Expvar. Var interface, so we can export//it Directly.func (Ctr *counter) string () string {Ctr.mu.Lock () defer ctr.mu.Unlock () retur N FMT. Sprintf ("%d", CTR.N)}func (Ctr *counter) servehttp (w http.) Responsewriter, req *http. Request) {Ctr.mu.Lock () defer ctr.mu.Unlock () switch req. Method {case "GET": Ctr.n++case "POST": buf: = new (bytes). Buffer) io. Copy (BUF, req. Body) Body: = buf. String () if n, err: = StrConv. Atoi (body); Err! = Nil {fmt. fprintf (W, "Bad POST:%v\nbody: [%v]\n", Err, body)} else {CTR.N = nfmt. Fprint (W, "Counter reset\n")}}fmt. fprintf (W, "counter =%d\n", CTR.N)}//simple flag Servervar booleanflag = flag. Bool ("Boolean", True, "another flag for testing") Func Flagserver (w http. Responsewriter, req *http. Request) {W.header (). Set ("Content-type", "Text/plain; Charset=utf-8 ") fmt. Fprint (W, "flags:\n") flag. Visitall (func (f *flag). Flag) {if f.value.string ()! = f.defvalue {fmt. fprintf (W, "%s =%s [default =%s]\n", F.name, F.value.string (), F.defvalue)} else {fmt. fprintf (W, "%s =%s\n", F.name, F.value.string ())})}//simple argument serverfunc argserver (w http. Responsewriter, req *http. Request) {For _, S: = Range OS. Args {fmt. Fprint (W, S, "")}}//a channel (just for the fun of it) type Chan chan intfunc chancreate () chan {c: = make (Chan) go func (c Chan) {for x: = 0;; x + + {c <-x}} (c) return C}func (ch Chan) servehttp (w http. Responsewriter, req *http. Request) {io. WriteString (W, FMT. SPRINTF ("Channel Send #%d\n", <-ch))}//exec a program, redirecting Outputfunc dateserver (rw http. Responsewriter, req *http. Request) {rw. Header (). Set ("Content-typE "," Text/plain; Charset=utf-8 ") Date, err: = Exec.command ("/bin/date "). Output () if err! = Nil {http. Error (rw, err. Error (), RETURN}RW). Write (date)}func Logger (w http. Responsewriter, req *http. Request) {log. Print (req. URL) http. Error (W, "oops", 404)}var Webroot = flag. String ("root", OS. Getenv ("HOME"), "Web root directory") func main () {flag. Parse ()//The counter is published as a variable directly.ctr: = new (counter) Expvar. Publish ("Counter", CTR) http. Handle ("/counter", CTR) http. Handle ("/", HTTP. Handlerfunc (Logger)) http. Handle ("/go/", http. Stripprefix ("/go/", http. Fileserver (http. Dir (*webroot))) HTTP. Handle ("/chan", Chancreate ()) HTTP. Handlefunc ("/flags", Flagserver) http. Handlefunc ("/args", Argserver) http. Handlefunc ("/go/hello", HelloServer) http. Handlefunc ("/date", dateserver) Err: = http. Listenandserve (": 12345", nil) if err! = Nil {log. PANICLN ("Listenandserve:", Err)}}
Curl Http://localhost:12345/counter
Curl http://localhost:12345/
Curl Http://localhost:12345/go
Curl Http://localhost:12345/chan
Curl Http://localhost:12345/flags
Curl Http://localhost:12345/args
Curl Http://localhost:12345/go/hello
Curl Http://localhost:12345/date
Curl Http://localhost:12345/xx