Getting started with Go's net/http Package
Last updated: February 23, 2024
net/http
Package in Go
The The net/http
package in Go is a powerful tool for building HTTP servers and clients. It provides a rich set of functionalities for handling HTTP requests and responses, making it a versatile package for a wide range of web-related tasks.
Setting Up a Simple HTTP Server
Let's delve into a simple example of how to set up an HTTP server using the net/http
package.
package main
import (
"fmt"
"net/http"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello, World!")
})
http.ListenAndServe(":8080", nil)
}
This program sets up a server that listens on port 8080 and responds with "Hello, World!" for any request to the root ("/") path.
Handling HTTP Requests
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello, World!")
})
The http.HandleFunc
function is used to define how the server responds to specific routes. The provided function takes two parameters: http.ResponseWriter
and *http.Request
. The response writer is used to send the response back to the client, and the request contains information about the incoming request.
Starting the Server
http.ListenAndServe(":8080", nil)
This line starts the HTTP server. It listens on port 8080, and the nil
argument means that it will use the default ServeMux (multiplexer) provided by the http
package. The server will handle incoming HTTP requests based on the registered handlers.
Working with Query Parameters
You can easily handle query parameters using the r.URL.Query()
method. Let's modify our server to greet a user based on a provided name parameter:
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
name := r.URL.Query().Get("name")
if name == "" {
name = "Guest"
}
fmt.Fprintf(w, "Hello, %s!", name)
})
// ... rest of the code
}
Now, you can visit http://localhost:8080/?name=John
and the server will respond with "Hello, John!"
Serving Static Files
staticDir := http.Dir("./static")
fileServer := http.FileServer(staticDir)
http.Handle("/static/", http.StripPrefix("/static/", fileServer))
Create a 'static' folder alongside your main.go
file and add any static files like HTML, CSS, and JavaScript. For example, you created hello.html
inside the 'static' folder.
Now, when you visit http://localhost:8080/static/hello.html
, you will be able to see your HTML served.
Explanation:
The http.Dir
function is used to convert a string (representing a directory path) into a type that implements the http.FileSystem
interface.
The http.FileServer
function is used to serve static files in the directory.
The http.StripPrefix
function is used to remove the "/static/" prefix from the request URL before passing it to the file server. This is useful to avoid exposing the actual directory structure in the URL.