Here, we will learn about net/http package of go golang. The net/http package provides a set of tools to build HTTP servers, clients and middleware.
HTTP Servers
The net/http
package provides the tools to build HTTP servers. We can create a basic HTTP server with the following code:
package main import ( "fmt" "net/http" ) func handler_api(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello, %s!", r.URL.Path[1:]) } func main() { http.HandleFunc("/", handler_api) http.ListenAndServe(":8000", nil) }
In the above code, we have created a handler function that takes two arguments – http.ResponseWriter
and *http.Request
. The http.ResponseWriter
is used to send the response to the client, and the *http.Request
contains information about the incoming request.
The http.HandleFunc()
function is used to register the handler_api function with the default ServeMux
. The ServeMux
is a HTTP request multiplexer that matches the URL of each incoming request against a list of registered patterns and calls the handler for the pattern that matches the URL.
Finally, we use the http.ListenAndServe()
function to start the HTTP server on port 8000.
HTTP Clients
The net/http
package also provides the tools to build HTTP clients. We can create a basic HTTP client with the following code:
package main import ( "fmt" "io/ioutil" "net/http" ) func main() { resp, err := http.Get("https://jsonplaceholder.typicode.com/todos/1") if err != nil { fmt.Println("Error: ", err) } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { fmt.Println("Error: ", err) } fmt.Println(string(body)) }
In the above code, we have used the http.Get()
function to send a GET request to the URL https://jsonplaceholder.typicode.com/todos/1
. The http.Get()
function returns a http.Response
object that contains information about the response.
We use the defer
keyword to ensure that the resp.Body
is closed after we have read the entire response body. Finally, we use the ioutil.ReadAll()
function to read the response body, and we print it to the console.
Middleware
The net/http
package also provides the tools to build middleware for HTTP servers. Middleware is a function that takes an HTTP handler function as input and returns another HTTP handler function as output. Middleware can be used to modify the behavior of the HTTP server, for example, by adding authentication, logging, or rate limiting.
Here’s an example of how to use middleware in the net/http
package:
package main import ( "fmt" "net/http" "time" ) func main() { // Create a new ServeMux mux := http.NewServeMux() // Register a handler function mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintln(w, "Hello, World!") }) // Create a new middleware function middleware := func(next http.HandlerFunc) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { start := time.Now() next.ServeHTTP(w, r) fmt.Printf("Request took %s\n", time.Since(start)) } } // Wrap the handler function with the middleware function
Details of net/http package is present in given below link:
To learn more about golang, Please refer given below link.
https://techieindoor.com/go-lang-tutorial/
References: