http.Request Type - Go
This is just some additional notes while going through the Go Web Dev course:
“r” is http.Request type, then we accessed its field called “URL”, which happens to have also a field called “Path”.
package main
import (
"fmt"
"net/http"
)
func pathHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, r.URL.Path)
}
func main() {
http.HandleFunc("/", pathHandler)
fmt.Println("Starting the server on :3000...")
http.ListenAndServe(":3000", nil)
}
So, what this program will do is just to print the URL path that is being accessed.
❯ curl localhost:3000/
/
❯ curl localhost:3000/contact
/contact
❯ curl localhost:3000/whateverpath
/whateverpath