> For the complete documentation index, see [llms.txt](https://ineelhere.gitbook.io/code-with-go/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ineelhere.gitbook.io/code-with-go/75-http-server.md).

# HTTP Server

This program is a simple HTTP server written in Go. It defines two HTTP handlers: `/hello` and `/headers`. When the server receives a request to either of these paths, it will respond accordingly.

Here's a breakdown of the code:

```go
package main

import (
	"fmt"
	"net/http"
)

// hello is an HTTP handler function that responds with "hello\n".
func hello(w http.ResponseWriter, req *http.Request) {
	fmt.Fprintf(w, "hello\n")
}

// headers is an HTTP handler function that responds with the request headers.
func headers(w http.ResponseWriter, req *http.Request) {
	// Iterate over request headers and write them to the response.
	for name, headers := range req.Header {
		for _, h := range headers {
			fmt.Fprintf(w, "%v: %v\n", name, h)
		}
	}
}

func main() {
	// Register the /hello and /headers handlers with the default ServeMux.
	http.HandleFunc("/hello", hello)
	http.HandleFunc("/headers", headers)

	// Start the HTTP server on port 8090.
	http.ListenAndServe(":8090", nil)
}
```

Explanation:

1. `hello` is an HTTP handler function that writes the string "hello\n" to the response writer (`w`).
2. `headers` is an HTTP handler function that iterates over the request headers and writes them to the response writer.
3. In the `main` function:
   * The `/hello` and `/headers` paths are registered with their corresponding handler functions using `http.HandleFunc`.
   * The server is started using `http.ListenAndServe` on port `8090`.

To test the server, you can make requests to `http://localhost:8090/hello` and `http://localhost:8090/headers`. The `/hello` path will respond with "hello\n", and the `/headers` path will respond with the headers of the incoming request.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://ineelhere.gitbook.io/code-with-go/75-http-server.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
