From 6153bc61357dd94686d0b3f3915df81ed9fcb93a Mon Sep 17 00:00:00 2001
From: Levi Olson As a starting point our
main.go
file looks like this: package main
+
package main
import (
"fmt"
@@ -42,25 +42,25 @@
http.HandleFunc
method.
It is very simple to use and its signature is easy to understand.
- func HandleFunc(pattern string, handler func(ResponseWriter, *Request))
+ func HandleFunc(pattern string, handler func(ResponseWriter, *Request))
Which basically means,
http.HandleFunc("/url", routingFunction)
where
routingFunction
looks like this:
- func routingFunction(w http.ResponseWriter, req *http.Request) {
+ func routingFunction(w http.ResponseWriter, req *http.Request) {
fmt.Fprint(w, "Hello HTTP")
}
With
- fmt.Fprint()
we can pass an
- http.ResponseWriter
and a message to display. Our browser will now look like this when we visit the
- /url
endpoint.
+ fmt.Fprint()
we can pass an
+ http.ResponseWriter
and a message to display. Our browser will now look like this when we visit the
+ /url
endpoint.
Here is what
- main.go
looks like at this point:
- package main
+ main.go
looks like at this point:
+ package main
import (
"fmt"
@@ -83,13 +83,13 @@
So let's add a
NotFound
page when we don't match a pattern in
HandleFunc
. It's as simple as:
- func notFound(w http.ResponseWriter, req *http.Request) {
+ func notFound(w http.ResponseWriter, req *http.Request) {
http.NotFound(w, req)
}
Here is what
main.go
looks like after that:
- package main
+ package main
import (
"fmt"
@@ -124,7 +124,7 @@
:id
is an identifier used to get the user profile from our persistance layer (i.e. our database).
We'll start by creating a new method for this GET request called
userProfile
:
- func userProfile(w http.ResponseWriter, req *http.Request) {
+ func userProfile(w http.ResponseWriter, req *http.Request) {
userID := req.URL.Path[len("/user/"):]
fmt.Fprintf(w, "User Profile: %q", userID)
}
@@ -140,7 +140,7 @@
Let's add this new route in our
main
function:
- func main() {
+ func main() {
http.HandleFunc("/hello", helloHTTP)
http.HandleFunc("/user/", userProfile)
http.HandleFunc("/", notFound)
@@ -158,7 +158,7 @@
Ok, so we have introduced some pretty severe holes in the security of our new HTTP router. As mentioned in a note above,
treating the
req.URL.Path
as a byte slice and just taking the last half is a terrible idea. So let's fix this:
- var validPath = regexp.MustCompile("^/(user)/([0-9]+)$")
+ var validPath = regexp.MustCompile("^/(user)/([0-9]+)$")
func getID(w http.ResponseWriter, req *http.Request) (string, error) {
m := validPath.FindStringSubmatch(req.URL.Path)
@@ -170,7 +170,7 @@
}
Now we can use this method in our code:
- func userProfile(w http.ResponseWriter, req *http.Request) {
+ func userProfile(w http.ResponseWriter, req *http.Request) {
userID, err := getID(w, req)
if err != nil {
return
@@ -182,4 +182,4 @@
For now, I'm calling this "Basic HTTP Routing in Golang" article finished. But I do plan to add more to it as time
allows. Additionally, I'd like to create a more advanced article that discusses the ability to respond to not only GET
requests, but also POST, PUT, and DELETE HTTP methods. Look for an "Advanced HTTP routing in Golang" article
- in the future. Thanks for reading this far. I wish you well in your Go endeavors.
\ No newline at end of file
+ in the future. Thanks for reading this far. I wish you well in your Go endeavors.