Skip to content

Commit b5198a5

Browse files
Fixed example to build and compile properly.
1 parent 2e53dc5 commit b5198a5

4 files changed

Lines changed: 17 additions & 12 deletions

File tree

‎chapter9/listing04/handlers/handlers.go‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ import (
88
"strconv"
99
)
1010

11+
// Routes sets the routes for the web service.
12+
func Routes() {
13+
http.HandleFunc("/sendjson", SendJSON)
14+
}
15+
1116
// SendJSON returns a simple JSON document.
1217
func SendJSON(rw http.ResponseWriter, r *http.Request) {
1318
u := struct {
@@ -20,10 +25,9 @@ func SendJSON(rw http.ResponseWriter, r *http.Request) {
2025

2126
data, err := json.Marshal(&u)
2227
if err != nil {
23-
// We want this error condition to panic so we get a stack
24-
// trace. This should never happen. The http package will
25-
// catch the panic and provide logging and return a 500 back
26-
// to the caller.
28+
// We want this error condition to panic so we get a stack trace. This should
29+
// never happen. The http package will catch the panic and provide logging
30+
// and return a 500 back to the caller.
2731
log.Panic("Unable to unmarshal response", err)
2832
}
2933

‎chapter9/listing04/handlers/handlers_example_test.go‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,14 @@ import (
77
"log"
88
"net/http"
99
"net/http/httptest"
10+
11+
"github.com/ardanstudios/gotraining/09-testing/01-testing/example4/handlers"
1012
)
1113

14+
func init() {
15+
handlers.Routes()
16+
}
17+
1218
// ExampleSendJSON provides a basic example test example.
1319
func ExampleSendJSON() {
1420
r, _ := http.NewRequest("GET", "/sendjson", nil)

‎chapter9/listing04/listing04.go‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,8 @@ import (
1010

1111
// main is the entry point for the application.
1212
func main() {
13-
Routes()
13+
handlers.Routes()
1414

1515
log.Println("listener : Started : Listening on :4000")
1616
http.ListenAndServe(":4000", nil)
1717
}
18-
19-
// Routes sets the routes for the web service.
20-
func Routes() {
21-
http.HandleFunc("/sendjson", handlers.SendJSON)
22-
}

‎chapter9/listing04/listing04_test.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ import (
88
"net/http/httptest"
99
"testing"
1010

11-
ex "github.com/goinaction/code/chapter9/listing04"
11+
"github.com/goinaction/code/chapter9/listing04/handlers"
1212
)
1313

1414
const checkMark = "\u2713"
1515
const ballotX = "\u2717"
1616

1717
func init() {
18-
ex.Routes()
18+
handlers.Routes()
1919
}
2020

2121
// TestSendJSON testing the sendjson internal endpoint.

0 commit comments

Comments
 (0)