package
0.0.0-20220718095237-46e3e1afaaee
Repository: https://github.com/yiting-tom/lf_golang.git
Documentation: pkg.go.dev

# README

01 Introduction

Project Structure

.
├── main.go +
└── README.md +

Run the code

  • Server
$ go run main.go
  • Client
$ curl localhost:9090
$ curl -d 'test' localhost:9090/test
sequenceDiagram
    participant C
    participant S
    C ->> S: curl localhost:9090
    Note right of S: Client access

    C ->> S: curl -d 'test' localhost:9090/test
    Note right of S: Get 'test'

    S ->> C: Hello test data
    Note left of C: Hello test

http.HandleFunc

// Define
func HandleFunc(pattern string, handler func(ResponseWriter, *Request))

// Example: L18-L20
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    ...
})

Registers the handler function for the given pattern / in the DefaultServeMux, which explains how patterns are matched.

http.ResponseWriter

  • Used by an HTTP handler to construct an HTTP response.
  • May not be used after the Handler.ServeHTTP method has returned.

http.Request

A Request represents an HTTP request reveived by a server or to be sent by a client

http.ListenAndServe

// Define
func ListenAndServe(addr string, handler Handler) error

// Example: L43
http.ListenAndServe(":9090", nil)

Listen on the TCP network address addr and then calls Serve with the handler to handle requests on incoming connections.