This is a creation in Article, where the information may have evolved or changed.
Using the go language to implement one of the simplest HTTP server side, the main use of the package Io, log, net/http this 3 library.
The functions used include:
http. Handle ()
http. Handlerfunc ()
http. Listenandserve ()
Directory:
1. Code
2. Operation
1. Code
$ cat Helloserver.go
1 Package Main2 3 Import (4 "io"5 "Log"6 "net/http"7 "StrConv"8 "FMT"9 )Ten One varIcntint=0; A -Func Hellohandler (w http. Responsewriter, R *http. Request) { -icnt++; theSTR: ="Hello world! Friend ("+ StrConv. Itoa (icnt) +")" - io. WriteString (w, str) - FMT. Println (str) - } + - Func Main () { +HT: =http. Handlerfunc (Hellohandler) A ifHT! =Nil { athttp. Handle ("/hello", HT) - } -ERR: = http. Listenandserve (": 8090", nil) - ifErr! =Nil { -Log. Fatal ("Listenandserve:", Err. Error ()) - } in}
2. Operation
2.1) Server-side
2.2) Client (browser)