@@ -38,16 +38,33 @@ var (
3838)
3939
4040func main () {
41- resp , err := http .Get (url )
41+ // -------------------------------------------
42+ // Send http GET request
43+ // -------------------------------------------
44+
45+ res , err := http .Get (url )
4246 if err != nil {
4347 errLog .Println (err )
4448 return
4549 }
46- defer resp .Body .Close ()
50+ defer res .Body .Close ()
51+
52+ // -------------------------------------------
53+ // Check http status code
54+ // -------------------------------------------
55+
56+ if res .StatusCode != http .StatusOK {
57+ errLog .Printf ("http status code: %d" , res .StatusCode )
58+ return
59+ }
60+
61+ // -------------------------------------------
62+ // Decode response to JSON
63+ // -------------------------------------------
4764
4865 var (
4966 post = & Post {}
50- decoder = json .NewDecoder (resp .Body )
67+ decoder = json .NewDecoder (res .Body )
5168 )
5269
5370 err = decoder .Decode (post )
@@ -56,5 +73,9 @@ func main() {
5673 return
5774 }
5875
59- appLog .Println (post )
76+ // -------------------------------------------
77+ // Show results
78+ // -------------------------------------------
79+
80+ appLog .Printf ("status: %d, resonse: %s\n " , res .StatusCode , post )
6081}
0 commit comments