This is a creation in Article, where the information may have evolved or changed.
The use of the Go language Net/http package is very simple and elegant
(1) Service side
Package Mainimport ("Flag" "FMT" "Net/http") func main () {host: = flag. String ("Host", "127.0.0.1", "Listen Host") Port: = Flag. String ("Port", "Listen", "Port") http. Handlefunc ("/hello", hello) Err: = http. Listenandserve (*host+ ":" +*port, nil) if err! = Nil {panic (err)}}func Hello (w http. Responsewriter, req *http. Request) {<p>w.write ([]byte ("" Hello World ")] </p>}
http. Handlefunc is used to register a path handler function, which invokes different functions depending on the given path.
http. Listenandsercer Listen IP and port, native IP can omit not to write, only write colon plus port, such as HTTP. Listenandsercer (": 8080", nil)
The path handler function, the parameter must be W http. Responsewriter and req *http. Request and cannot have a return value
Test Result: Success
(2) Client
Package Mainimport ("FMT" "Io/ioutil" "Net/http") func main () {response, _: = http. Get ("Http://localhost:80/hello") defer response. Body.close () body, _: = Ioutil. ReadAll (response. Body) fmt. Println (String (body))}
Test Result: Success