Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Routes ¶
func Routes()
Routes sets the routes for the web service. It has 1 route call sendjson. When that route is executed, it will call the SendJSON function.
func SendJSON ¶
func SendJSON(rw http.ResponseWriter, r *http.Request)
SendJSON returns a simple JSON document. This has the same signature that we had before using ResponseWriter and Request. We create an anonymous struct, initialize it and unmarshall it into JSON and pass it down the line.
Example ¶
ExampleSendJSON provides a basic example example. Notice that we are binding that Example to our SendJSON function.
package main import ( "encoding/json" "fmt" "log" "net/http" "net/http/httptest" ) func main() { r := httptest.NewRequest("GET", "/sendjson", nil) w := httptest.NewRecorder() http.DefaultServeMux.ServeHTTP(w, r) var u struct { Name string Email string } if err := json.NewDecoder(w.Body).Decode(&u); err != nil { log.Println("ERROR:", err) } fmt.Println(u) }
Output: {Hoanh An hoanhan101@gmail.com}
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.