HTTP servers and clients are a common part of many web applications. In Go, the “net/http” package provides support for creating HTTP servers and clients. Here is an overview of how to use the “net/http” package in Go:
1. Import the “net/http” package: Import the “net/http” package into your Go program.
2. Create an HTTP server: Use the “http.NewServeMux” function to create a new HTTP server. This function returns a new “ServeMux” object that can be used to handle incoming HTTP requests.
3. Register handlers: Use the “HandleFunc” method on the “ServeMux” object to register handlers for specific HTTP routes. Handlers are functions that take a “http.ResponseWriter” object and a “http.Request” object as arguments, and return no values.
4. Listen and serve: Use the “http.ListenAndServe” function to start the HTTP server and listen for incoming HTTP requests. This function takes a network address (“host:port”) and a “ServeMux” object as arguments.
5. Create an HTTP client: Use the “http.Client” type to create an HTTP client that can be used to send HTTP requests.
6. Send HTTP requests: Use the “http.NewRequest” function to create a new HTTP request, and use the “Do” method on the HTTP client to send the request. This method returns a “http.Response” object that contains the response from the server.
Here isan example of using the “net/http” package to create an HTTP server in Go:
import ( "fmt" "net/http" ) func helloHandler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello, World!") } func main() { mux := http.NewServeMux() mux.HandleFunc("/", helloHandler) fmt.Println("Listening on :8080") err := http.ListenAndServe(":8080", mux) if err != nil { fmt.Println("Error starting server:", err.Error()) } }
In this example, we create an HTTP server using the “http.NewServeMux” function and the “http.HandleFunc” method to handle incoming HTTP requests. We use the “http.ListenAndServe” function to start the HTTP server and listen for incoming requests. In the “helloHandler” function, we write “Hello, World!” to the HTTP response.
Here is an example of using the “net/http” package to create an HTTP client in Go:
import ( "fmt" "net/http" "io/ioutil" ) func main() { url := "http://example.com" client := &http.Client{} req, err := http.NewRequest("GET", url, nil) if err != nil { fmt.Println("Error creating request:", err.Error()) return } resp, err := client.Do(req) if err !=nil { fmt.Println("Error sending request:", err.Error()) return } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { fmt.Println("Error reading response:", err.Error()) return } fmt.Println(string(body)) }
In this example, we create an HTTP client using the “http.Client” type and the “http.NewRequest” function to create a new HTTP request. We use the “client.Do” method to send the request and receive the response from the server. We use the “ioutil.ReadAll” function to read the response body.
Overall, the “net/http” package in Go provides an easy-to-use API for creating HTTP servers and clients. By using this package, developers can easily handle incoming HTTP requests, send HTTP requests, and receive HTTP responses in their Go applications.