Browse Source

Updates to basic http routing in golang example

post_vjs2
Levi Olson 6 years ago
parent
commit
ebab1b691e
2 changed files with 39 additions and 0 deletions
  1. +39
    -0
      posts/basic-http-routing-in-golang.md
  2. BIN
      posts/step2-browser-output.png

+ 39
- 0
posts/basic-http-routing-in-golang.md View File

@ -12,6 +12,8 @@ I assume you have basic knowledge of the Go language at this point, so if not, i
## Let's begin ## Let's begin
### Step 1
Here is our basic folder structure for this basic http routing example: Here is our basic folder structure for this basic http routing example:
basic-http-routing-in-golang/ basic-http-routing-in-golang/
@ -30,3 +32,40 @@ As a starting point our `main.go` file looks like this:
fmt.Println("Hello HTTP") fmt.Println("Hello HTTP")
} }
### Step 2
Now starting at a very basic level, we can leverage the [`http.HandleFunc`](https://golang.org/pkg/net/http/#HandleFunc) method.
It is very simple to use and its signature is easy to understand.
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) {
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.
![](step2-browser-output.png)
Here is what `main.go` looks like at this point:
package main
import (
"fmt"
"log"
"net/http"
)
func main() {
http.HandleFunc("/hello", HelloHTTP)
log.Fatal(http.ListenAndServe(":8080", nil))
}
func HelloHTTP(w http.ResponseWriter, req *http.Request) {
fmt.Fprint(w, "Hello HTTP")
}

BIN
posts/step2-browser-output.png View File

Before After
Width: 300  |  Height: 200  |  Size: 7.1 KiB

Loading…
Cancel
Save